Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Wrong preselected user in dynamic form #1316

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,16 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
field.newValue = [];
for (let index = 0; index < newValue.length; index++) {
const element = newValue[index];
let retrivedItem = false;
if (field.fieldDefaultValue !== null) {
if (field.fieldDefaultValue.join(',').indexOf(element.text) !== -1)
field.fieldDefaultValue.forEach(item => {
if (item.split('/')[1] === element.text) {
retrivedItem = true;
field.newValue.push(item.split('/')[0]);
}
});
}
if (!retrivedItem) {
if (element.id === undefined || parseInt(element.id, 10).toString() === "NaN") {
let user: string = element.secondaryText;
if (user.indexOf('@') === -1) {
user = element.loginName;
}
const result = await sp.web.ensureUser(user);
field.newValue.push(result.data.Id);
}
else {
field.newValue.push(element.id);
if (element.id === undefined || parseInt(element.id, 10).toString() === "NaN") {
let user: string = element.secondaryText;
if (user.indexOf('@') === -1) {
user = element.loginName;
}
const result = await sp.web.ensureUser(user);
field.newValue.push(result.data.Id);
}
else {
field.newValue.push(element.id);
}
}
}
Expand Down Expand Up @@ -455,7 +443,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
else if (fieldType === "User") {
if (item !== null) {
const userEmails: string[] = [];
userEmails.push(await this._spService.getUserUPNById(parseInt(item[field.InternalName + "Id"])) + '');
userEmails.push(await this._spService.getUserUPNFromFieldValue(listId, listItemId, field.InternalName, this.webURL) + '');
defaultValue = userEmails;
}
else {
Expand Down
17 changes: 10 additions & 7 deletions src/services/SPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,16 @@ export default class SPService implements ISPService {
public async getUsersUPNFromFieldValue(listId: string, listItemId: number, fieldName: string, webUrl?: string): Promise<any[]> { // eslint-disable-line @typescript-eslint/no-explicit-any
try {
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id&$expand=${fieldName}`;
const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id,${fieldName}/Name&$expand=${fieldName}`;

const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
if (data.ok) {
const result = await data.json();
if (result && result[fieldName]) {
const emails = [];
result[fieldName].forEach(element => {
emails.push(element.Id + "/" + element.Title);
const loginNameWithoutClaimsToken = element.Name.split("|").pop();
emails.push(loginNameWithoutClaimsToken + "/" + element.Title);
});
return emails;
}
Expand All @@ -596,16 +597,18 @@ export default class SPService implements ISPService {
}
}

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

const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
if (data.ok) {
const results = await data.json();
if (results) {
return userId + "/" + results.Title;
const result = await data.json();
if (result && result[fieldName]) {
const element = result[fieldName]
const loginNameWithoutClaimsToken = element.Name.split("|").pop();
return loginNameWithoutClaimsToken + "/" + element.Title;
}
}

Expand Down