Skip to content

Commit 350646a

Browse files
authored
Fix - Wrong preselected user in dynamic form (pnp#1316)
* Removed parsing the user ID from default value, because the ID is no longer in the peoplePicker default value * changed the 2 methods so that they do what the name intends - they return the default value for the peoplepicker with the UPN and not the ID * Get Single User Field the same way as the multiple users * rewrite getUserUPNById to getUserUPNFromFieldValue for single user values
1 parent 3672060 commit 350646a

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -281,28 +281,16 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
281281
field.newValue = [];
282282
for (let index = 0; index < newValue.length; index++) {
283283
const element = newValue[index];
284-
let retrivedItem = false;
285-
if (field.fieldDefaultValue !== null) {
286-
if (field.fieldDefaultValue.join(',').indexOf(element.text) !== -1)
287-
field.fieldDefaultValue.forEach(item => {
288-
if (item.split('/')[1] === element.text) {
289-
retrivedItem = true;
290-
field.newValue.push(item.split('/')[0]);
291-
}
292-
});
293-
}
294-
if (!retrivedItem) {
295-
if (element.id === undefined || parseInt(element.id, 10).toString() === "NaN") {
296-
let user: string = element.secondaryText;
297-
if (user.indexOf('@') === -1) {
298-
user = element.loginName;
299-
}
300-
const result = await sp.web.ensureUser(user);
301-
field.newValue.push(result.data.Id);
302-
}
303-
else {
304-
field.newValue.push(element.id);
284+
if (element.id === undefined || parseInt(element.id, 10).toString() === "NaN") {
285+
let user: string = element.secondaryText;
286+
if (user.indexOf('@') === -1) {
287+
user = element.loginName;
305288
}
289+
const result = await sp.web.ensureUser(user);
290+
field.newValue.push(result.data.Id);
291+
}
292+
else {
293+
field.newValue.push(element.id);
306294
}
307295
}
308296
}
@@ -456,7 +444,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
456444
else if (fieldType === "User") {
457445
if (item !== null) {
458446
const userEmails: string[] = [];
459-
userEmails.push(await this._spService.getUserUPNById(parseInt(item[field.InternalName + "Id"])) + '');
447+
userEmails.push(await this._spService.getUserUPNFromFieldValue(listId, listItemId, field.InternalName, this.webURL) + '');
460448
defaultValue = userEmails;
461449
}
462450
else {

src/services/SPService.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,16 @@ export default class SPService implements ISPService {
575575
public async getUsersUPNFromFieldValue(listId: string, listItemId: number, fieldName: string, webUrl?: string): Promise<any[]> { // eslint-disable-line @typescript-eslint/no-explicit-any
576576
try {
577577
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
578-
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id&$expand=${fieldName}`;
578+
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id,${fieldName}/Name&$expand=${fieldName}`;
579579

580580
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
581581
if (data.ok) {
582582
const result = await data.json();
583583
if (result && result[fieldName]) {
584584
const emails = [];
585585
result[fieldName].forEach(element => {
586-
emails.push(element.Id + "/" + element.Title);
586+
const loginNameWithoutClaimsToken = element.Name.split("|").pop();
587+
emails.push(loginNameWithoutClaimsToken + "/" + element.Title);
587588
});
588589
return emails;
589590
}
@@ -596,16 +597,18 @@ export default class SPService implements ISPService {
596597
}
597598
}
598599

599-
public async getUserUPNById(userId: number, webUrl?: string): Promise<string> {
600+
public async getUserUPNFromFieldValue(listId: string, listItemId: number, fieldName: string, webUrl?: string): Promise<any> {
600601
try {
601602
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
602-
const apiUrl = `${webAbsoluteUrl}/_api/web/getuserbyid(${userId})?$select=UserPrincipalName,Title`;
603+
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id,${fieldName}/Name&$expand=${fieldName}`;
603604

604605
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
605606
if (data.ok) {
606-
const results = await data.json();
607-
if (results) {
608-
return userId + "/" + results.Title;
607+
const result = await data.json();
608+
if (result && result[fieldName]) {
609+
const element = result[fieldName]
610+
const loginNameWithoutClaimsToken = element.Name.split("|").pop();
611+
return loginNameWithoutClaimsToken + "/" + element.Title;
609612
}
610613
}
611614

0 commit comments

Comments
 (0)