Skip to content

Commit 79cd3e6

Browse files
authored
Merge pull request #956 from ravichandran-blog/v2-dev
Principal Types support
2 parents 678ab89 + ec2c101 commit 79cd3e6

File tree

4 files changed

+71
-33
lines changed

4 files changed

+71
-33
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -227,35 +227,65 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
227227

228228
// trigger when the user change any value in the form
229229
private onChange = async (internalName: string, newValue: any, additionalData?: FieldChangeAdditionalData) => {
230-
try {
231-
let fieldCol = (this.state.fieldCollection || []).slice();
232-
let field = fieldCol.filter((element, i) => { return element.columnInternalName === internalName; })[0];
233-
field.newValue = newValue;
234-
field.additionalData = additionalData;
235-
if (field.fieldType === "User" && newValue.length !== 0) {
236-
let result = await sp.web.ensureUser(newValue[0].secondaryText);
230+
// try {
231+
let fieldCol = (this.state.fieldCollection || []).slice();
232+
let field = fieldCol.filter((element, i) => { return element.columnInternalName === internalName; })[0];
233+
field.newValue = newValue;
234+
field.additionalData = additionalData;
235+
if (field.fieldType === "User" && newValue.length !== 0) {
236+
// let result = await sp.web.ensureUser(newValue[0].secondaryText);
237+
// field.newValue = result.data.Id;
238+
239+
if (newValue[0].id === undefined || parseInt(newValue[0].id, 10).toString() === "NaN") {
240+
let user: string = newValue[0].secondaryText;
241+
if (user.indexOf('@') === -1) {
242+
user = newValue[0].loginName;
243+
}
244+
let result = await sp.web.ensureUser(user);
237245
field.newValue = result.data.Id;
238246
}
239-
else if (field.fieldType === "UserMulti" && newValue.length !== 0) {
240-
field.newValue = [];
241-
for (let index = 0; index < newValue.length; index++) {
242-
const element = newValue[index];
243-
let user: string = element.secondaryText;
244-
if (user.indexOf('@') === -1) {
245-
user = element.loginName;
246-
}
247-
let result = await sp.web.ensureUser(user);
248-
field.newValue.push(result.data.Id);
249-
}
247+
else {
248+
field.newValue = newValue[0].id;
250249
}
251-
this.setState({
252-
fieldCollection: fieldCol
253-
});
254-
} catch (error) {
255250

256-
console.log(`Error onchange`, error);
257-
return null;
258251
}
252+
else if (field.fieldType === "UserMulti" && newValue.length !== 0) {
253+
field.newValue = [];
254+
for (let index = 0; index < newValue.length; index++) {
255+
const element = newValue[index];
256+
var retrivedItem: boolean = false;
257+
if (field.fieldDefaultValue != null) {
258+
if (field.fieldDefaultValue.join(',').indexOf(element.text) !== -1)
259+
field.fieldDefaultValue.forEach(item => {
260+
if (item.split('/')[1] === element.text) {
261+
retrivedItem = true;
262+
field.newValue.push(item.split('/')[0]);
263+
}
264+
});
265+
}
266+
if (!retrivedItem) {
267+
if (element.id === undefined || parseInt(element.id, 10).toString() === "NaN") {
268+
let user: string = element.secondaryText;
269+
if (user.indexOf('@') === -1) {
270+
user = element.loginName;
271+
}
272+
let result = await sp.web.ensureUser(user);
273+
field.newValue.push(result.data.Id);
274+
}
275+
else {
276+
field.newValue.push(element.id);
277+
}
278+
}
279+
}
280+
}
281+
this.setState({
282+
fieldCollection: fieldCol
283+
});
284+
// } catch (error) {
285+
286+
// console.log(`Error onchange`, error);
287+
// return null;
288+
// }
259289
}
260290

261291
//getting all the fields information as part of get ready process
@@ -291,6 +321,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
291321
let selectedTags: any = [];
292322
let richText = false;
293323
let dateFormat: DateFormat | undefined;
324+
let principalType = "";
294325
if (item !== null) {
295326
defaultValue = item[field.InternalName];
296327
}
@@ -387,6 +418,8 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
387418
else {
388419
defaultValue = [];
389420
}
421+
principalType = field.SchemaXml.split('UserSelectionMode="')[1];
422+
principalType = principalType.substring(0, principalType.indexOf('"'));
390423
}
391424
else if (fieldType === "Thumbnail") {
392425
if (defaultValue !== null) {
@@ -402,6 +435,8 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
402435
else {
403436
defaultValue = [];
404437
}
438+
principalType = field.SchemaXml.split('UserSelectionMode="')[1];
439+
principalType = principalType.substring(0, principalType.indexOf('"'));
405440
}
406441
else if (fieldType === "Location") {
407442
defaultValue = JSON.parse(defaultValue);
@@ -428,7 +463,8 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
428463
Order: field.order,
429464
isRichText: richText,
430465
dateFormat: dateFormat,
431-
listItemId: listItemId
466+
listItemId: listItemId,
467+
principalType: principalType
432468
});
433469
tempFields.sort((a, b) => a.Order - b.Order);
434470
}

src/controls/dynamicForm/dynamicField/DynamicField.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export class DynamicField extends React.Component<IDynamicFieldProps, IDynamicFi
7474
isRichText,
7575
//bingAPIKey,
7676
dateFormat,
77-
columnInternalName
77+
columnInternalName,
78+
principalType
7879
} = this.props;
7980

8081
const {
@@ -335,7 +336,7 @@ export class DynamicField extends React.Component<IDynamicFieldProps, IDynamicFi
335336
personSelectionLimit={1}
336337
showtooltip={false}
337338
showHiddenInUI={false}
338-
principalTypes={[PrincipalType.User]} // TODO: principal types should be read from the column settings
339+
principalTypes={principalType === 'PeopleOnly' ? [PrincipalType.User] : [PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.DistributionList, PrincipalType.SecurityGroup]}
339340
resolveDelay={1000}
340341
onChange={(items) => { this.onChange(items); }}
341342
disabled={disabled}
@@ -357,7 +358,7 @@ export class DynamicField extends React.Component<IDynamicFieldProps, IDynamicFi
357358
personSelectionLimit={30}
358359
showtooltip={false}
359360
showHiddenInUI={false}
360-
principalTypes={[PrincipalType.User]} // TODO: principal types should be read from the column settings
361+
principalTypes={principalType === 'PeopleOnly' ? [PrincipalType.User] : [PrincipalType.User, PrincipalType.SharePointGroup, PrincipalType.DistributionList, PrincipalType.SecurityGroup]}
361362
resolveDelay={1000}
362363
onChange={(items) => { this.onChange(items); }}
363364
disabled={disabled}

src/controls/dynamicForm/dynamicField/IDynamicFieldProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ export interface IDynamicFieldProps {
3232
//bingAPIKey?: string;
3333
dateFormat?: DateFormat;
3434
additionalData?: FieldChangeAdditionalData;
35+
principalType?:string;
3536
}

src/services/SPService.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,15 @@ export default class SPService implements ISPService {
427427
public async getUsersUPNFromFieldValue(listId: string, listItemId: number, fieldName: string, webUrl?: string): Promise<any[]> {
428428
try {
429429
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
430-
let apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/UserName&$expand=${fieldName}`;
430+
let apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${listItemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=${fieldName}/Title,${fieldName}/Id&$expand=${fieldName}`;
431431

432432
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
433433
if (data.ok) {
434434
const result = await data.json();
435435
if (result && result[fieldName]) {
436436
let emails = [];
437437
result[fieldName].forEach(element => {
438-
emails.push(element.UserName);
438+
emails.push(element.Id + "/" + element.Title);
439439
});
440440
return emails;
441441
}
@@ -448,16 +448,16 @@ export default class SPService implements ISPService {
448448
}
449449
}
450450

451-
public async getUserUPNById(userId: number, webUrl?: string): Promise<any[]> {
451+
public async getUserUPNById(userId: number, webUrl?: string): Promise<string> {
452452
try {
453453
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
454-
let apiUrl = `${webAbsoluteUrl}/_api/web/getuserbyid(${userId})?$select=UserPrincipalName`;
454+
let apiUrl = `${webAbsoluteUrl}/_api/web/getuserbyid(${userId})?$select=UserPrincipalName,Title`;
455455

456456
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
457457
if (data.ok) {
458458
const results = await data.json();
459459
if (results) {
460-
return results.UserPrincipalName;
460+
return userId + "/" + results.Title;
461461
}
462462
}
463463

0 commit comments

Comments
 (0)