@kwiz/common 1.0.42 → 1.0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -401,10 +401,11 @@ export function UserHasPermissionsSync(siteUrlOrId: string, listIdOrTitle: strin
401
401
  }
402
402
 
403
403
  /** create a new column and try to add it to default view. Send either Title and Type, or SchemaXml. Create with SchemaXml also adds to all content types */
404
- export function CreateField(siteUrl: string, listIdOrTitle: string, options: {
404
+ export async function CreateField(siteUrl: string, listIdOrTitle: string, options: {
405
405
  Title?: string;
406
406
  Type?: FieldTypes;
407
407
  Required?: boolean;
408
+ Indexed?: boolean;
408
409
  SchemaXml?: string;
409
410
  SchemaXmlSpecificInternalName?: boolean;
410
411
  SkipAddToDefaultView?: boolean;
@@ -415,32 +416,33 @@ export function CreateField(siteUrl: string, listIdOrTitle: string, options: {
415
416
  }): Promise<IFieldInfoEX> {
416
417
  siteUrl = GetSiteUrl(siteUrl);
417
418
 
418
- let finish = async (result: { d: IFieldInfo; }) => {
419
- if (result && result.d) {
420
- let internalName = result.d.InternalName;
421
- //we need to clear and reload the list fields cache, so call it and return our field from that collection.
422
- let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
419
+ let finish = async (result: IFieldInfo) => {
420
+ if (!result) {
421
+ return null;
422
+ }
423
423
 
424
- try {
425
- if (options.SkipAddToDefaultView !== true) {
426
- //try to add it to default view, don't wait for it
427
- GetListViews(siteUrl, listIdOrTitle).then(views => {
428
- let defaultView = firstOrNull(views, v => v.DefaultView);
429
- if (defaultView)
430
- GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/views('${defaultView.Id}')/ViewFields/addViewField('${internalName}')`, null, { method: "POST", spWebUrl: siteUrl });
431
- });
432
- }
433
- }
434
- catch (e) { }
424
+ let internalName = result.InternalName;
425
+ //we need to clear and reload the list fields cache, so call it and return our field from that collection.
426
+ let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
435
427
 
436
- return firstOrNull(fields, f => f.InternalName === internalName);
428
+ try {
429
+ if (options.SkipAddToDefaultView !== true) {
430
+ //try to add it to default view, don't wait for it
431
+ GetListViews(siteUrl, listIdOrTitle).then(views => {
432
+ let defaultView = firstOrNull(views, v => v.DefaultView);
433
+ if (defaultView)
434
+ GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/views('${defaultView.Id}')/ViewFields/addViewField('${internalName}')`, null, { method: "POST", spWebUrl: siteUrl });
435
+ });
436
+ }
437
437
  }
438
- return null;
438
+ catch (e) { }
439
+
440
+ return firstOrNull(fields, f => f.InternalName === internalName);
439
441
  };
440
442
 
441
443
  if (!isNullOrEmptyString(options.SchemaXml)) {
442
- return GetJson<{ d: IFieldInfo; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/fields/createFieldAsXml`,
443
- JSON.stringify({
444
+ try {
445
+ let updateObject: IDictionary<any> = {
444
446
  'parameters': {
445
447
  '__metadata': { 'type': 'SP.XmlSchemaFieldCreationInformation' },
446
448
  'SchemaXml': options.SchemaXml,
@@ -448,32 +450,51 @@ export function CreateField(siteUrl: string, listIdOrTitle: string, options: {
448
450
  4 ://SP.AddFieldOptions.addToAllContentTypes
449
451
  4 | 8//SP.AddFieldOptions.addToAllContentTypes | addFieldInternalNameHint
450
452
  }
451
- }))
452
- .then(r => {
453
- return finish(r);
454
- })
455
- .catch<IFieldInfoEX>(() => null);
456
- }
457
- else if (!isNullOrEmptyString(options.Title) && !isNullOrUndefined(options.Type)) {
453
+ };
454
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/fields/createFieldAsXml`;
455
+ let newFieldResult = await GetJson<{ d: IFieldInfo; }>(url, JSON.stringify(updateObject));
456
+
457
+ if (!isNullOrUndefined(newFieldResult)
458
+ && !isNullOrUndefined(newFieldResult.d)) {
459
+ if ((!isNullOrEmptyString(options.Title) && options.Title !== newFieldResult.d.Title)
460
+ || (isBoolean(options.Indexed) && options.Indexed !== newFieldResult.d.Indexed)) {
461
+ let updatedField = await UpdateField(siteUrl, listIdOrTitle, newFieldResult.d.InternalName, {
462
+ Title: options.Title,
463
+ Indexed: options.Indexed === true
464
+ });
465
+ return finish(updatedField);
466
+ }
467
+ }
468
+
469
+ return finish(newFieldResult && newFieldResult.d);
470
+ } catch {
471
+ }
472
+ return null;
473
+ } else if (!isNullOrEmptyString(options.Title) && !isNullOrUndefined(options.Type)) {
458
474
  let updateObject: IDictionary<any> = {
459
475
  '__metadata': { 'type': 'SP.Field' },
460
476
  'Title': options.Title,
461
477
  'FieldTypeKind': options.Type,
462
- 'Required': options.Required === true
478
+ 'Required': options.Required === true,
479
+ 'Indexed': options.Indexed === true
463
480
  };
464
- if (!isNullOrEmptyString(options.ClientSideComponentId))
481
+ if (!isNullOrEmptyString(options.ClientSideComponentId)) {
465
482
  updateObject.ClientSideComponentId = options.ClientSideComponentId;
466
- if (!isNullOrEmptyString(options.ClientSideComponentProperties))
483
+ }
484
+ if (!isNullOrEmptyString(options.ClientSideComponentProperties)) {
467
485
  updateObject.ClientSideComponentProperties = options.ClientSideComponentProperties;
468
- if (!isNullOrEmptyString(options.JSLink))
486
+ }
487
+ if (!isNullOrEmptyString(options.JSLink)) {
469
488
  updateObject.JSLink = options.JSLink;
489
+ }
470
490
 
471
- return GetJson<{ d: IFieldInfo; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/fields`,
472
- JSON.stringify(updateObject))
473
- .then(r => {
474
- return finish(r);
475
- })
476
- .catch<IFieldInfoEX>(() => null);
491
+ try {
492
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/fields`;
493
+ let newFieldResult = await GetJson<{ d: IFieldInfo; }>(url, JSON.stringify(updateObject));
494
+ return finish(newFieldResult && newFieldResult.d);
495
+ } catch {
496
+ }
497
+ return null;
477
498
  }
478
499
  else {
479
500
  console.error("You must send either SchemaXml or Title and Type");
@@ -483,6 +504,9 @@ export function CreateField(siteUrl: string, listIdOrTitle: string, options: {
483
504
  /** Update field SchemaXml OR Title, only 1 update at a time supported. */
484
505
  export async function UpdateField(siteUrlOrId: string, listIdOrTitle: string, fieldInternalName: string, options: {
485
506
  Title?: string;
507
+ Indexed?: boolean;
508
+ /** Update 'Choices' propertry on 'Choice' and 'MultiChoice' field types. */
509
+ Choices?: string[];
486
510
  SchemaXml?: string;
487
511
  FieldType?: FieldTypeAsString;
488
512
  Required?: boolean;
@@ -499,6 +523,9 @@ export async function UpdateField(siteUrlOrId: string, listIdOrTitle: string, fi
499
523
  return firstOrNull(fields, f => f.InternalName === fieldInternalName);
500
524
  };
501
525
 
526
+ let fields = await GetListFieldsAsHash(siteUrl, listIdOrTitle, true);
527
+ let thisField = fields[fieldInternalName];
528
+
502
529
  //updates can either be SchemaXml, or others. Cannot be both.
503
530
  let updates: IDictionary<any> = {
504
531
  '__metadata': { 'type': 'SP.Field' }
@@ -509,7 +536,6 @@ export async function UpdateField(siteUrlOrId: string, listIdOrTitle: string, fi
509
536
  }
510
537
  else {
511
538
  //cannot send schema updates with other updates.
512
-
513
539
  if (!isNullOrEmptyString(options.Title)) {
514
540
  updates.Title = options.Title;
515
541
  }
@@ -519,6 +545,18 @@ export async function UpdateField(siteUrlOrId: string, listIdOrTitle: string, fi
519
545
  if (isBoolean(options.Required)) {
520
546
  updates.Required = options.Required === true;
521
547
  }
548
+ if (isBoolean(options.Indexed)) {
549
+ updates.Indexed = options.Indexed === true;
550
+ }
551
+ if (!isNullOrEmptyArray(options.Choices)) {
552
+ let choiceType = options.FieldType || thisField.TypeAsString;
553
+ if (choiceType === "Choice" || choiceType === "MultiChoice") {
554
+ updates["__metadata"]["type"] = choiceType === "Choice" ? "SP.FieldChoice" : "SP.FieldMultiChoice"
555
+ updates.Choices = { "results": options.Choices };
556
+ } else {
557
+ logger.warn("Can only update 'Choices' property on 'Choice' and 'MultiChoice' field types.");
558
+ }
559
+ }
522
560
  if (isBoolean(options.Hidden)) {
523
561
  //this requries the CanToggleHidden to be in the schema... if not - we will need to add it before we can update this.
524
562
  let fields = await GetListFieldsAsHash(siteUrl, listIdOrTitle, false);