/
types.ts
697 lines (608 loc) · 21.5 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import {
_SharePointQueryableInstance,
_SharePointQueryableCollection,
spInvokableFactory,
deleteable,
IDeleteable,
} from "../sharepointqueryable.js";
import { assign, ITypedHash } from "@pnp/common";
import { metadata } from "../utils/metadata.js";
import { body, headers } from "@pnp/odata";
import { defaultPath } from "../decorators.js";
import { spPost } from "../operations.js";
import { tag } from "../telemetry.js";
@defaultPath("fields")
export class _Fields extends _SharePointQueryableCollection<IFieldInfo[]> {
/**
* Creates a field based on the specified schema
*
* @param xml A string or XmlSchemaFieldCreationInformation instance descrbing the field to create
*/
@tag("fs.createFieldAsXml")
public async createFieldAsXml(xml: string | IXmlSchemaFieldCreationInformation): Promise<IFieldAddResult> {
if (typeof xml === "string") {
xml = { SchemaXml: xml };
}
const postBody = body({
"parameters":
assign(metadata("SP.XmlSchemaFieldCreationInformation"), xml),
});
const data = await spPost<{ Id: string }>(this.clone(Fields, "createfieldasxml"), postBody);
return {
data,
field: this.getById(data.Id),
};
}
/**
* Gets a field from the collection by id
*
* @param id The Id of the list
*/
public getById(id: string): IField {
return tag.configure(Field(this).concat(`('${id}')`), "fs.getById");
}
/**
* Gets a field from the collection by title
*
* @param title The case-sensitive title of the field
*/
public getByTitle(title: string): IField {
return tag.configure(Field(this, `getByTitle('${title}')`), "fs.getByTitle");
}
/**
* Gets a field from the collection by using internal name or title
*
* @param name The case-sensitive internal name or title of the field
*/
public getByInternalNameOrTitle(name: string): IField {
return tag.configure(Field(this, `getByInternalNameOrTitle('${name}')`), "fs.getByInternalNameOrTitle");
}
/**
* Adds a new field to the collection
*
* @param title The new field's title
* @param fieldType The new field's type (ex: SP.FieldText)
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
public async add(title: string, fieldType: string, properties: IFieldCreationProperties & { FieldTypeKind: number }): Promise<IFieldAddResult> {
const postBody = body(Object.assign(metadata(fieldType), {
"Title": title,
}, properties));
if (!tag.isTagged(this)) {
tag.configure(this, "fs.add");
}
const data = await spPost<{ Id: string }>(this.clone(Fields, null), postBody);
return {
data,
field: this.getById(data.Id),
};
}
/**
* Adds a new SP.FieldText to the collection
*
* @param title The field title
* @param maxLength The maximum number of characters allowed in the value of the field.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addText")
public addText(title: string, maxLength = 255, properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props: { FieldTypeKind: number; MaxLength: number } = {
FieldTypeKind: 2,
MaxLength: maxLength,
};
return this.add(title, "SP.FieldText", assign(props, properties));
}
/**
* Adds a new SP.FieldCalculated to the collection
*
* @param title The field title.
* @param formula The formula for the field.
* @param dateFormat The date and time format that is displayed in the field.
* @param outputType Specifies the output format for the field. Represents a FieldType value.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addCalculated")
public addCalculated(
title: string,
formula: string,
dateFormat: DateTimeFieldFormatType,
outputType: FieldTypes = FieldTypes.Text,
properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props: {
DateFormat: DateTimeFieldFormatType;
FieldTypeKind: number;
Formula: string;
OutputType: FieldTypes;
} = {
DateFormat: dateFormat,
FieldTypeKind: 17,
Formula: formula,
OutputType: outputType,
};
return this.add(title, "SP.FieldCalculated", assign(props, properties));
}
/**
* Adds a new SP.FieldDateTime to the collection
*
* @param title The field title
* @param displayFormat The format of the date and time that is displayed in the field.
* @param calendarType Specifies the calendar type of the field.
* @param friendlyDisplayFormat The type of friendly display format that is used in the field.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addDateTime")
public addDateTime(
title: string,
displayFormat: DateTimeFieldFormatType = DateTimeFieldFormatType.DateOnly,
calendarType: CalendarType = CalendarType.Gregorian,
friendlyDisplayFormat: DateTimeFieldFriendlyFormatType = DateTimeFieldFriendlyFormatType.Unspecified,
properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
DateTimeCalendarType: calendarType,
DisplayFormat: displayFormat,
FieldTypeKind: 4,
FriendlyDisplayFormat: friendlyDisplayFormat,
};
return this.add(title, "SP.FieldDateTime", assign(props, properties));
}
/**
* Adds a new SP.FieldNumber to the collection
*
* @param title The field title
* @param minValue The field's minimum value
* @param maxValue The field's maximum value
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addNumber")
public addNumber(
title: string,
minValue?: number,
maxValue?: number,
properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
let props: { FieldTypeKind: number } = { FieldTypeKind: 9 };
if (minValue !== undefined) {
props = assign({ MinimumValue: minValue }, props);
}
if (maxValue !== undefined) {
props = assign({ MaximumValue: maxValue }, props);
}
return this.add(title, "SP.FieldNumber", assign(props, properties));
}
/**
* Adds a new SP.FieldCurrency to the collection
*
* @param title The field title
* @param minValue The field's minimum value
* @param maxValue The field's maximum value
* @param currencyLocalId Specifies the language code identifier (LCID) used to format the value of the field
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addCurrency")
public addCurrency(
title: string,
minValue?: number,
maxValue?: number,
currencyLocalId = 1033,
properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
let props: { CurrencyLocaleId: number; FieldTypeKind: number } = {
CurrencyLocaleId: currencyLocalId,
FieldTypeKind: 10,
};
if (minValue !== undefined) {
props = assign({ MinimumValue: minValue }, props);
}
if (maxValue !== undefined) {
props = assign({ MaximumValue: maxValue }, props);
}
return this.add(title, "SP.FieldCurrency", assign(props, properties));
}
/**
* Adds a new SP.FieldMultiLineText to the collection
*
* @param title The field title
* @param numberOfLines Specifies the number of lines of text to display for the field.
* @param richText Specifies whether the field supports rich formatting.
* @param restrictedMode Specifies whether the field supports a subset of rich formatting.
* @param appendOnly Specifies whether all changes to the value of the field are displayed in list forms.
* @param allowHyperlink Specifies whether a hyperlink is allowed as a value of the field.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*
*/
@tag("fs.addMultilineText")
public addMultilineText(
title: string,
numberOfLines = 6,
richText = true,
restrictedMode = false,
appendOnly = false,
allowHyperlink = true,
properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
AllowHyperlink: allowHyperlink,
AppendOnly: appendOnly,
FieldTypeKind: 3,
NumberOfLines: numberOfLines,
RestrictedMode: restrictedMode,
RichText: richText,
};
return this.add(title, "SP.FieldMultiLineText", assign(props, properties));
}
/**
* Adds a new SP.FieldUrl to the collection
*
* @param title The field title
*/
@tag("fs.addUrl")
public addUrl(title: string, displayFormat: UrlFieldFormatType = UrlFieldFormatType.Hyperlink, properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
DisplayFormat: displayFormat,
FieldTypeKind: 11,
};
return this.add(title, "SP.FieldUrl", assign(props, properties));
}
/** Adds a user field to the colleciton
*
* @param title The new field's title
* @param selectionMode The selection mode of the field
* @param selectionGroup Value that specifies the identifier of the SharePoint group whose members can be selected as values of the field
* @param properties
*/
@tag("fs.addUser")
public addUser(title: string, selectionMode: FieldUserSelectionMode, properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
FieldTypeKind: 20,
SelectionMode: selectionMode,
};
return this.add(title, "SP.FieldUser", assign(props, properties));
}
/**
* Adds a SP.FieldLookup to the collection
*
* @param title The new field's title
* @param lookupListId The guid id of the list where the source of the lookup is found
* @param lookupFieldName The internal name of the field in the source list
* @param properties Set of additional properties to set on the new field
*/
@tag("fs.addLookup")
public async addLookup(
title: string,
lookupListId: string,
lookupFieldName: string,
properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = assign({
FieldTypeKind: 7,
LookupFieldName: lookupFieldName,
LookupListId: lookupListId,
Title: title,
}, properties);
const postBody = body({
"parameters":
assign(metadata("SP.FieldCreationInformation"), props),
});
const data = await spPost<{ Id: string }>(this.clone(Fields, "addfield"), postBody);
return {
data,
field: this.getById(data.Id),
};
}
/**
* Adds a new SP.FieldChoice to the collection
*
* @param title The field title.
* @param choices The choices for the field.
* @param format The display format of the available options for the field.
* @param fillIn Specifies whether the field allows fill-in values.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addChoice")
public addChoice(
title: string,
choices: string[],
format: ChoiceFieldFormatType = ChoiceFieldFormatType.Dropdown,
fillIn?: boolean,
properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
Choices: {
results: choices,
},
EditFormat: format,
FieldTypeKind: 6,
FillInChoice: fillIn,
};
return this.add(title, "SP.FieldChoice", assign(props, properties));
}
/**
* Adds a new SP.FieldMultiChoice to the collection
*
* @param title The field title.
* @param choices The choices for the field.
* @param fillIn Specifies whether the field allows fill-in values.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addMultiChoice")
public addMultiChoice(title: string, choices: string[], fillIn?: boolean, properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
Choices: {
results: choices,
},
FieldTypeKind: 15,
FillInChoice: fillIn,
};
return this.add(title, "SP.FieldMultiChoice", assign(props, properties));
}
/**
* Adds a new SP.FieldBoolean to the collection
*
* @param title The field title.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addBoolean")
public addBoolean(title: string, properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
FieldTypeKind: 8,
};
return this.add(title, "SP.Field", assign(props, properties));
}
/**
* Creates a secondary (dependent) lookup field, based on the Id of the primary lookup field.
*
* @param displayName The display name of the new field.
* @param primaryLookupFieldId The guid of the primary Lookup Field.
* @param showField Which field to show from the lookup list.
*/
@tag("fs.addDependentLookupField")
public async addDependentLookupField(displayName: string, primaryLookupFieldId: string, showField: string): Promise<IFieldAddResult> {
const path = `adddependentlookupfield(displayName='${displayName}', primarylookupfieldid='${primaryLookupFieldId}', showfield='${showField}')`;
const data = await spPost(this.clone(Fields, path));
return {
data,
field: this.getById(data.Id),
};
}
/**
* Adds a new SP.FieldLocation to the collection
*
* @param title The field title.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addLocation")
public addLocation(title: string, properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = { FieldTypeKind: 33 };
return this.add(title, "SP.FieldLocation", assign(props, properties));
}
/**
* Adds a new SP.FieldLocation to the collection
*
* @param title The field title.
* @param properties Differ by type of field being created (see: https://msdn.microsoft.com/en-us/library/office/dn600182.aspx)
*/
@tag("fs.addImage")
public addImageField(title: string, properties?: IFieldCreationProperties): Promise<IFieldAddResult> {
const props = {
FieldTypeKind: 34,
};
return this.add(title, "SP.FieldMultiLineText", assign(props, properties));
}
}
export interface IFields extends _Fields { }
export const Fields = spInvokableFactory<IFields>(_Fields);
export class _Field extends _SharePointQueryableInstance<IFieldInfo> {
public delete = deleteable("f");
/**
* Updates this field instance with the supplied properties
*
* @param properties A plain object hash of values to update for the list
* @param fieldType The type value such as SP.FieldLookup. Optional, looked up from the field if not provided
*/
@tag("f.update")
public async update(properties: any, fieldType?: string): Promise<IFieldUpdateResult> {
if (typeof fieldType === "undefined" || fieldType === null) {
const info = await Field(this).select("FieldTypeKind").configure({
headers: {
"Accept": "application/json",
},
})();
fieldType = info["odata.type"];
}
const req = body(assign(metadata(fieldType), properties), headers({ "X-HTTP-Method": "MERGE" }));
const data = await spPost(this, req);
return {
data,
field: <any>this,
};
}
/**
* Sets the value of the ShowInDisplayForm property for this field.
*/
@tag("f.setShowInDisplayForm")
public setShowInDisplayForm(show: boolean): Promise<void> {
return spPost(this.clone(Field, `setshowindisplayform(${show})`));
}
/**
* Sets the value of the ShowInEditForm property for this field.
*/
@tag("f.setShowInEditForm")
public setShowInEditForm(show: boolean): Promise<void> {
return spPost(this.clone(Field, `setshowineditform(${show})`));
}
/**
* Sets the value of the ShowInNewForm property for this field.
*/
@tag("f.setShowInNewForm")
public setShowInNewForm(show: boolean): Promise<void> {
return spPost(this.clone(Field, `setshowinnewform(${show})`));
}
}
export interface IField extends _Field, IDeleteable { }
export const Field = spInvokableFactory<IField>(_Field);
/**
* This interface defines the result of adding a field
*/
export interface IFieldAddResult {
data: Partial<IFieldInfo>;
field: IField;
}
/**
* This interface defines the result of updating a field
*/
export interface IFieldUpdateResult {
data: Partial<IFieldInfo>;
field: IField;
}
/**
* Specifies the type of the field.
*/
export enum FieldTypes {
Invalid = 0,
Integer = 1,
Text = 2,
Note = 3,
DateTime = 4,
Counter = 5,
Choice = 6,
Lookup = 7,
Boolean = 8,
Number = 9,
Currency = 10,
URL = 11,
Computed = 12,
Threading = 13,
Guid = 14,
MultiChoice = 15,
GridChoice = 16,
Calculated = 17,
File = 18,
Attachments = 19,
User = 20,
Recurrence = 21,
CrossProjectLink = 22,
ModStat = 23,
Error = 24,
ContentTypeId = 25,
PageSeparator = 26,
ThreadIndex = 27,
WorkflowStatus = 28,
AllDayEvent = 29,
WorkflowEventType = 30,
}
export enum DateTimeFieldFormatType {
DateOnly = 0,
DateTime = 1,
}
export enum DateTimeFieldFriendlyFormatType {
Unspecified = 0,
Disabled = 1,
Relative = 2,
}
/**
* Specifies the control settings while adding a field.
*/
export enum AddFieldOptions {
/**
* Specify that a new field added to the list must also be added to the default content type in the site collection
*/
DefaultValue = 0,
/**
* Specify that a new field added to the list must also be added to the default content type in the site collection.
*/
AddToDefaultContentType = 1,
/**
* Specify that a new field must not be added to any other content type
*/
AddToNoContentType = 2,
/**
* Specify that a new field that is added to the specified list must also be added to all content types in the site collection
*/
AddToAllContentTypes = 4,
/**
* Specify adding an internal field name hint for the purpose of avoiding possible database locking or field renaming operations
*/
AddFieldInternalNameHint = 8,
/**
* Specify that a new field that is added to the specified list must also be added to the default list view
*/
AddFieldToDefaultView = 16,
/**
* Specify to confirm that no other field has the same display name
*/
AddFieldCheckDisplayName = 32,
}
export interface IXmlSchemaFieldCreationInformation {
Options?: AddFieldOptions;
SchemaXml: string;
}
export enum CalendarType {
Gregorian = 1,
Japan = 3,
Taiwan = 4,
Korea = 5,
Hijri = 6,
Thai = 7,
Hebrew = 8,
GregorianMEFrench = 9,
GregorianArabic = 10,
GregorianXLITEnglish = 11,
GregorianXLITFrench = 12,
KoreaJapanLunar = 14,
ChineseLunar = 15,
SakaEra = 16,
UmAlQura = 23,
}
export enum UrlFieldFormatType {
Hyperlink = 0,
Image = 1,
}
export enum FieldUserSelectionMode {
PeopleAndGroups = 1,
PeopleOnly = 0,
}
export interface IFieldCreationProperties extends ITypedHash<string | number | boolean> {
DefaultFormula?: string;
Description?: string;
EnforceUniqueValues?: boolean;
FieldTypeKind?: number;
Group?: string;
Hidden?: boolean;
Indexed?: boolean;
Required?: boolean;
Title?: string;
ValidationFormula?: string;
ValidationMessage?: string;
}
export enum ChoiceFieldFormatType {
Dropdown,
RadioButtons,
}
export interface IFieldInfo {
DefaultFormula: string | null;
DefaultValue: string | null;
Description: string;
Direction: string;
EnforceUniqueValues: boolean;
EntityPropertyName: string;
FieldTypeKind: FieldTypes;
Filterable: boolean;
FromBaseType: boolean;
Group: string;
Hidden: boolean;
Id: string;
Indexed: boolean;
IndexStatus: number;
InternalName: string;
JSLink: string;
PinnedToFiltersPane: boolean;
ReadOnlyField: boolean;
Required: boolean;
SchemaXml: string;
Scope: string;
Sealed: boolean;
ShowInFiltersPane: number;
Sortable: boolean;
StaticName: string;
Title: string;
TypeAsString: string;
TypeDisplayName: string;
TypeShortDescription: string;
ValidationFormula: string | null;
ValidationMessage: string | null;
}