Skip to content

Commit

Permalink
Merge pull request #956 from ravichandran-blog/v2-dev
Browse files Browse the repository at this point in the history
Principal Types support
  • Loading branch information
AJIXuMuK authored Jul 28, 2021
2 parents 678ab89 + ec2c101 commit 79cd3e6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 33 deletions.
86 changes: 61 additions & 25 deletions src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,35 +227,65 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm

// trigger when the user change any value in the form
private onChange = async (internalName: string, newValue: any, additionalData?: FieldChangeAdditionalData) => {
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
Expand Down Expand Up @@ -291,6 +321,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
let selectedTags: any = [];
let richText = false;
let dateFormat: DateFormat | undefined;
let principalType = "";
if (item !== null) {
defaultValue = item[field.InternalName];
}
Expand Down Expand Up @@ -387,6 +418,8 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
else {
defaultValue = [];
}
principalType = field.SchemaXml.split('UserSelectionMode="')[1];
principalType = principalType.substring(0, principalType.indexOf('"'));
}
else if (fieldType === "Thumbnail") {
if (defaultValue !== null) {
Expand All @@ -402,6 +435,8 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
else {
defaultValue = [];
}
principalType = field.SchemaXml.split('UserSelectionMode="')[1];
principalType = principalType.substring(0, principalType.indexOf('"'));
}
else if (fieldType === "Location") {
defaultValue = JSON.parse(defaultValue);
Expand All @@ -428,7 +463,8 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
Order: field.order,
isRichText: richText,
dateFormat: dateFormat,
listItemId: listItemId
listItemId: listItemId,
principalType: principalType
});
tempFields.sort((a, b) => a.Order - b.Order);
}
Expand Down
7 changes: 4 additions & 3 deletions src/controls/dynamicForm/dynamicField/DynamicField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export class DynamicField extends React.Component<IDynamicFieldProps, IDynamicFi
isRichText,
//bingAPIKey,
dateFormat,
columnInternalName
columnInternalName,
principalType
} = this.props;

const {
Expand Down Expand Up @@ -335,7 +336,7 @@ export class DynamicField extends React.Component<IDynamicFieldProps, IDynamicFi
personSelectionLimit={1}
showtooltip={false}
showHiddenInUI={false}
principalTypes={[PrincipalType.User]} // TODO: principal types should be read from the column settings
principalTypes={principalType === 'PeopleOnly' ? [PrincipalType.User] : [PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.DistributionList, PrincipalType.SecurityGroup]}
resolveDelay={1000}
onChange={(items) => { this.onChange(items); }}
disabled={disabled}
Expand All @@ -357,7 +358,7 @@ export class DynamicField extends React.Component<IDynamicFieldProps, IDynamicFi
personSelectionLimit={30}
showtooltip={false}
showHiddenInUI={false}
principalTypes={[PrincipalType.User]} // TODO: principal types should be read from the column settings
principalTypes={principalType === 'PeopleOnly' ? [PrincipalType.User] : [PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.DistributionList, PrincipalType.SecurityGroup]}
resolveDelay={1000}
onChange={(items) => { this.onChange(items); }}
disabled={disabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export interface IDynamicFieldProps {
//bingAPIKey?: string;
dateFormat?: DateFormat;
additionalData?: FieldChangeAdditionalData;
principalType?:string;
}
10 changes: 5 additions & 5 deletions src/services/SPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,15 @@ export default class SPService implements ISPService {
public async getUsersUPNFromFieldValue(listId: string, listItemId: number, fieldName: string, webUrl?: string): Promise<any[]> {
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) {
const result = await data.json();
if (result && result[fieldName]) {
let emails = [];
result[fieldName].forEach(element => {
emails.push(element.UserName);
emails.push(element.Id + "/" + element.Title);
});
return emails;
}
Expand All @@ -448,16 +448,16 @@ export default class SPService implements ISPService {
}
}

public async getUserUPNById(userId: number, webUrl?: string): Promise<any[]> {
public async getUserUPNById(userId: number, webUrl?: string): Promise<string> {
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;
}
}

Expand Down

0 comments on commit 79cd3e6

Please sign in to comment.