From ec2c1017662e09f10ac93d9d51e8ca4bdf50f4a9 Mon Sep 17 00:00:00 2001 From: Ravichandran Krishnasamy Date: Sun, 11 Jul 2021 22:30:47 +0530 Subject: [PATCH] Principal Types support The principal type will be retrieved from the people column also can support any type of group include SharePoint group --- src/controls/dynamicForm/DynamicForm.tsx | 86 +++++++++++++------ .../dynamicForm/dynamicField/DynamicField.tsx | 7 +- .../dynamicField/IDynamicFieldProps.ts | 1 + src/services/SPService.ts | 10 +-- 4 files changed, 71 insertions(+), 33 deletions(-) diff --git a/src/controls/dynamicForm/DynamicForm.tsx b/src/controls/dynamicForm/DynamicForm.tsx index 35ff01631..50c7e7c05 100644 --- a/src/controls/dynamicForm/DynamicForm.tsx +++ b/src/controls/dynamicForm/DynamicForm.tsx @@ -227,35 +227,65 @@ export class DynamicForm extends React.Component { - try { - let fieldCol = (this.state.fieldCollection || []).slice(); - let field = fieldCol.filter((element, i) => { return element.columnInternalName === internalName; })[0]; - field.newValue = newValue; - field.additionalData = additionalData; - if (field.fieldType === "User" && newValue.length !== 0) { - let result = await sp.web.ensureUser(newValue[0].secondaryText); + // try { + let fieldCol = (this.state.fieldCollection || []).slice(); + let field = fieldCol.filter((element, i) => { return element.columnInternalName === internalName; })[0]; + field.newValue = newValue; + field.additionalData = additionalData; + if (field.fieldType === "User" && newValue.length !== 0) { + // let result = await sp.web.ensureUser(newValue[0].secondaryText); + // field.newValue = result.data.Id; + + if (newValue[0].id === undefined || parseInt(newValue[0].id, 10).toString() === "NaN") { + let user: string = newValue[0].secondaryText; + if (user.indexOf('@') === -1) { + user = newValue[0].loginName; + } + let result = await sp.web.ensureUser(user); field.newValue = result.data.Id; } - else if (field.fieldType === "UserMulti" && newValue.length !== 0) { - field.newValue = []; - for (let index = 0; index < newValue.length; index++) { - const element = newValue[index]; - let user: string = element.secondaryText; - if (user.indexOf('@') === -1) { - user = element.loginName; - } - let result = await sp.web.ensureUser(user); - field.newValue.push(result.data.Id); - } + else { + field.newValue = newValue[0].id; } - this.setState({ - fieldCollection: fieldCol - }); - } catch (error) { - console.log(`Error onchange`, error); - return null; } + else if (field.fieldType === "UserMulti" && newValue.length !== 0) { + field.newValue = []; + for (let index = 0; index < newValue.length; index++) { + const element = newValue[index]; + var retrivedItem: boolean = 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; + } + let result = await sp.web.ensureUser(user); + field.newValue.push(result.data.Id); + } + else { + field.newValue.push(element.id); + } + } + } + } + this.setState({ + fieldCollection: fieldCol + }); + // } catch (error) { + + // console.log(`Error onchange`, error); + // return null; + // } } //getting all the fields information as part of get ready process @@ -291,6 +321,7 @@ export class DynamicForm extends React.Component a.Order - b.Order); } diff --git a/src/controls/dynamicForm/dynamicField/DynamicField.tsx b/src/controls/dynamicForm/dynamicField/DynamicField.tsx index 1b551f519..7e24b45c3 100644 --- a/src/controls/dynamicForm/dynamicField/DynamicField.tsx +++ b/src/controls/dynamicForm/dynamicField/DynamicField.tsx @@ -74,7 +74,8 @@ export class DynamicField extends React.Component { this.onChange(items); }} disabled={disabled} @@ -357,7 +358,7 @@ export class DynamicField extends React.Component { this.onChange(items); }} disabled={disabled} diff --git a/src/controls/dynamicForm/dynamicField/IDynamicFieldProps.ts b/src/controls/dynamicForm/dynamicField/IDynamicFieldProps.ts index aeaa6cf31..7a203eaac 100644 --- a/src/controls/dynamicForm/dynamicField/IDynamicFieldProps.ts +++ b/src/controls/dynamicForm/dynamicField/IDynamicFieldProps.ts @@ -32,4 +32,5 @@ export interface IDynamicFieldProps { //bingAPIKey?: string; dateFormat?: DateFormat; additionalData?: FieldChangeAdditionalData; + principalType?:string; } diff --git a/src/services/SPService.ts b/src/services/SPService.ts index a5cdad5d7..187349e40 100644 --- a/src/services/SPService.ts +++ b/src/services/SPService.ts @@ -427,7 +427,7 @@ export default class SPService implements ISPService { public async getUsersUPNFromFieldValue(listId: string, listItemId: number, fieldName: string, webUrl?: string): Promise { try { const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl; - let apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/UserName&$expand=${fieldName}`; + let apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id&$expand=${fieldName}`; const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1); if (data.ok) { @@ -435,7 +435,7 @@ export default class SPService implements ISPService { if (result && result[fieldName]) { let emails = []; result[fieldName].forEach(element => { - emails.push(element.UserName); + emails.push(element.Id + "/" + element.Title); }); return emails; } @@ -448,16 +448,16 @@ export default class SPService implements ISPService { } } - public async getUserUPNById(userId: number, webUrl?: string): Promise { + public async getUserUPNById(userId: number, webUrl?: string): Promise { try { const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl; - let apiUrl = `${webAbsoluteUrl}/_api/web/getuserbyid(${userId})?$select=UserPrincipalName`; + let apiUrl = `${webAbsoluteUrl}/_api/web/getuserbyid(${userId})?$select=UserPrincipalName,Title`; const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1); if (data.ok) { const results = await data.json(); if (results) { - return results.UserPrincipalName; + return userId + "/" + results.Title; } }