@kwiz/common 1.0.78 → 1.0.80

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.
Files changed (118) hide show
  1. package/.github/workflows/npm-publish.yml +24 -0
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/sharepoint.js +5 -1
  6. package/lib/cjs/helpers/sharepoint.js.map +1 -1
  7. package/lib/cjs/helpers/typecheckers.js +5 -1
  8. package/lib/cjs/helpers/typecheckers.js.map +1 -1
  9. package/lib/cjs/types/libs/msal.types.js +26 -26
  10. package/lib/cjs/utils/sharepoint.rest/list.js +1 -1
  11. package/lib/cjs/utils/sharepoint.rest/list.js.map +1 -1
  12. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  13. package/lib/esm/helpers/sharepoint.js +3 -0
  14. package/lib/esm/helpers/sharepoint.js.map +1 -1
  15. package/lib/esm/helpers/typecheckers.js +3 -0
  16. package/lib/esm/helpers/typecheckers.js.map +1 -1
  17. package/lib/esm/types/libs/msal.types.js +26 -26
  18. package/lib/esm/utils/sharepoint.rest/list.js +2 -2
  19. package/lib/esm/utils/sharepoint.rest/list.js.map +1 -1
  20. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  21. package/lib/types/helpers/sharepoint.d.ts +1 -0
  22. package/lib/types/helpers/typecheckers.d.ts +1 -0
  23. package/package.json +77 -77
  24. package/readme.md +17 -17
  25. package/src/_dependencies.ts +12 -12
  26. package/src/config.ts +17 -17
  27. package/src/helpers/Guid.ts +181 -181
  28. package/src/helpers/base64.ts +173 -173
  29. package/src/helpers/browser.test.js +13 -13
  30. package/src/helpers/browser.ts +1348 -1348
  31. package/src/helpers/browserinfo.ts +292 -292
  32. package/src/helpers/collections.base.test.js +25 -25
  33. package/src/helpers/collections.base.ts +437 -437
  34. package/src/helpers/collections.ts +107 -107
  35. package/src/helpers/color.ts +54 -54
  36. package/src/helpers/cookies.ts +59 -59
  37. package/src/helpers/date.test.js +119 -119
  38. package/src/helpers/date.ts +188 -188
  39. package/src/helpers/debug.ts +186 -186
  40. package/src/helpers/emails.ts +6 -6
  41. package/src/helpers/eval.ts +5 -5
  42. package/src/helpers/file.test.js +50 -50
  43. package/src/helpers/file.ts +58 -58
  44. package/src/helpers/flatted.ts +149 -149
  45. package/src/helpers/functions.ts +16 -16
  46. package/src/helpers/graph/calendar.types.ts +10 -10
  47. package/src/helpers/http.ts +69 -69
  48. package/src/helpers/images.ts +22 -22
  49. package/src/helpers/json.ts +38 -38
  50. package/src/helpers/md5.ts +189 -189
  51. package/src/helpers/objects.test.js +33 -33
  52. package/src/helpers/objects.ts +270 -270
  53. package/src/helpers/promises.test.js +37 -37
  54. package/src/helpers/promises.ts +165 -165
  55. package/src/helpers/random.ts +27 -27
  56. package/src/helpers/scheduler/scheduler.test.js +103 -103
  57. package/src/helpers/scheduler/scheduler.ts +131 -131
  58. package/src/helpers/sharepoint.ts +776 -772
  59. package/src/helpers/strings.test.js +101 -101
  60. package/src/helpers/strings.ts +317 -317
  61. package/src/helpers/typecheckers.test.js +34 -34
  62. package/src/helpers/typecheckers.ts +266 -262
  63. package/src/helpers/url.test.js +43 -43
  64. package/src/helpers/url.ts +207 -207
  65. package/src/helpers/urlhelper.ts +111 -111
  66. package/src/index.ts +6 -6
  67. package/src/types/auth.ts +54 -54
  68. package/src/types/common.types.ts +15 -15
  69. package/src/types/flatted.types.ts +59 -59
  70. package/src/types/globals.types.ts +6 -6
  71. package/src/types/graph/calendar.types.ts +80 -80
  72. package/src/types/knownscript.types.ts +18 -18
  73. package/src/types/libs/datajs.types.ts +28 -28
  74. package/src/types/libs/ics.types.ts +30 -30
  75. package/src/types/libs/msal.types.ts +49 -49
  76. package/src/types/locales.ts +124 -124
  77. package/src/types/localstoragecache.types.ts +8 -8
  78. package/src/types/location.types.ts +27 -27
  79. package/src/types/moment.ts +11 -11
  80. package/src/types/regex.types.ts +16 -16
  81. package/src/types/rest.types.ts +95 -95
  82. package/src/types/sharepoint.types.ts +1465 -1465
  83. package/src/types/sharepoint.utils.types.ts +287 -287
  84. package/src/utils/auth/common.ts +74 -74
  85. package/src/utils/auth/discovery.test.js +12 -12
  86. package/src/utils/auth/discovery.ts +132 -132
  87. package/src/utils/base64.ts +27 -27
  88. package/src/utils/consolelogger.ts +320 -320
  89. package/src/utils/date.ts +35 -35
  90. package/src/utils/emails.ts +24 -24
  91. package/src/utils/knownscript.ts +286 -286
  92. package/src/utils/localstoragecache.ts +441 -441
  93. package/src/utils/rest.ts +501 -501
  94. package/src/utils/script.ts +170 -170
  95. package/src/utils/sharepoint.rest/common.ts +154 -154
  96. package/src/utils/sharepoint.rest/date.ts +62 -62
  97. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  98. package/src/utils/sharepoint.rest/item.ts +547 -547
  99. package/src/utils/sharepoint.rest/list.ts +1388 -1388
  100. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  101. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  102. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  103. package/src/utils/sharepoint.rest/location.ts +141 -141
  104. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  105. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  106. package/src/utils/sharepoint.rest/user.ts +491 -491
  107. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  108. package/src/utils/sod.ts +194 -194
  109. package/lib/cjs/helpers/_dependencies.js +0 -21
  110. package/lib/cjs/helpers/_dependencies.js.map +0 -1
  111. package/lib/cjs/utils/_dependencies.js +0 -24
  112. package/lib/cjs/utils/_dependencies.js.map +0 -1
  113. package/lib/esm/helpers/_dependencies.js +0 -3
  114. package/lib/esm/helpers/_dependencies.js.map +0 -1
  115. package/lib/esm/utils/_dependencies.js +0 -4
  116. package/lib/esm/utils/_dependencies.js.map +0 -1
  117. package/lib/types/helpers/_dependencies.d.ts +0 -2
  118. package/lib/types/utils/_dependencies.d.ts +0 -3
@@ -1,1389 +1,1389 @@
1
- import { PushNoDuplicate, firstOrNull, makeUniqueArray, toHash } from "../../helpers/collections.base";
2
- import { jsonStringify } from "../../helpers/json";
3
- import { NormalizeListName, SPBasePermissions, SchemaXmlToJson, extendFieldInfo } from "../../helpers/sharepoint";
4
- import { normalizeGuid } from "../../helpers/strings";
5
- import { SafeIfElse, isBoolean, isNotEmptyArray, isNullOrEmptyArray, isNullOrEmptyString, isNullOrUndefined, isNumber, isPromise, isString, isValidGuid } from "../../helpers/typecheckers";
6
- import { makeServerRelativeUrl, normalizeUrl } from "../../helpers/url";
7
- import { IDictionary } from "../../types/common.types";
8
- import { IRestOptions, contentTypes, jsonTypes } from "../../types/rest.types";
9
- import { BaseTypes, FieldTypeAsString, FieldTypes, IFieldInfo, IFieldInfoEX, IFieldInfoExHash, IFieldJsonSchema, IFieldLookupInfo, ISPEventReceiver, ListTemplateTypes, PageType, SPBasePermissionKind } from "../../types/sharepoint.types";
10
- import { GeListItemsFoldersBehaviour, IListWorkflowAssociation, IRestItem, ListExperienceOptions, iContentType, iList, iListView } from "../../types/sharepoint.utils.types";
11
- import { ConsoleLogger } from "../consolelogger";
12
- import { GetJson, GetJsonSync, longLocalCache, shortLocalCache } from "../rest";
13
- import { GetRestBaseUrl, GetSiteUrl, LIST_EXPAND, LIST_SELECT } from "./common";
14
- import { __fixGetListItemsResults } from "./listutils/common";
15
- import { GetContentTypes, GetContentTypesSync, GetListsSync, IGetContentTypesOptions } from "./web";
16
-
17
- const logger = ConsoleLogger.get("SharePoint.Rest.List");
18
-
19
- /** returns /_api/web/lists/getById() or /_api/web/lists/getByTitle() */
20
- export function GetListRestUrl(siteUrl: string, listIdOrTitle: string): string {
21
- siteUrl = GetSiteUrl(siteUrl);
22
-
23
- let listId = GetListId(siteUrl, listIdOrTitle);
24
-
25
- let listPart = isValidGuid(listId) ? `getById('${normalizeGuid(listId)}')` : `getByTitle('${encodeURIComponent(listIdOrTitle)}')`;
26
- return GetRestBaseUrl(siteUrl) + `/web/lists/${listPart}`;
27
- }
28
-
29
- export function GetListId(siteUrl: string, listIdOrTitle: string): string {
30
- if (isNullOrEmptyString(listIdOrTitle)) return null;
31
- if (isValidGuid(listIdOrTitle)) return listIdOrTitle;
32
- //Issue 7508
33
- //When translation is enabled, and user changes list title but he is not on the same language as the site
34
- //he translates the list, but not changing its title
35
- //so REST api /lists/getByTitle will not work
36
- //instead, we need to get the list id from the web's lists collection.
37
- let lists = GetListsSync(siteUrl);
38
- var lower = listIdOrTitle.toLowerCase();
39
- var list = firstOrNull(lists, l => l.Title.toLowerCase() === lower);
40
- return list && list.Id || null;
41
- }
42
-
43
- /** get the list ID from a list page, such as a list view or an item form */
44
- export function GetListIdFromPageSync(siteUrl: string, listPageUrl: string): string {
45
- let url = `${GetRestBaseUrl(siteUrl)}/web/getlist('${makeServerRelativeUrl(listPageUrl.split('?')[0].split('#')[0])}')?$select=id`;
46
- let response = GetJsonSync<{ Id: string; }>(url, null, {
47
- ...longLocalCache,
48
- jsonMetadata: jsonTypes.nometadata
49
- });
50
- if (!isNullOrUndefined(response) && response.success) {
51
- let listId = response.result.Id;
52
- return normalizeGuid(listId);
53
- }
54
- return null;
55
- }
56
-
57
- interface IGetSiteAssetLibraryResult { Id: string, Name: string, ServerRelativeUrl: string }
58
- interface IGetSiteAssetLibraryReturnValue {
59
- value: {
60
- Id: string;
61
- RootFolder: {
62
- Name: string;
63
- ServerRelativeUrl: string;
64
- Exists: boolean;
65
- };
66
- }[];
67
- }
68
-
69
- /** ensures the site assets library exists and return its info. on errors - it will return null. */
70
- export function EnsureAssetLibrary(siteUrl: string): Promise<IGetSiteAssetLibraryResult> {
71
- siteUrl = GetSiteUrl(siteUrl);
72
-
73
- let url = GetRestBaseUrl(siteUrl) +
74
- "/web/lists/EnsureSiteAssetsLibrary?$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists&$expand=RootFolder";
75
- return GetJson<{
76
- d: { Id: string; RootFolder: { Name: string; ServerRelativeUrl: string; Exists: boolean; }; };
77
- }>(url, null, { method: "POST", spWebUrl: siteUrl, ...longLocalCache }).then(result => {
78
- if (result && result.d) {
79
- return {
80
- Id: result.d.Id,
81
- Name: result.d.RootFolder.Name,
82
- ServerRelativeUrl: result.d.RootFolder.ServerRelativeUrl
83
- };
84
- } else return null;
85
- }).catch<IGetSiteAssetLibraryResult>(() => null);
86
- }
87
-
88
- interface IGetSitePagesLibrarResult { Id: string, Name: string, ServerRelativeUrl: string }
89
-
90
- /** ensures the site pages library exists and return its info. on errors - it will return null. */
91
- export async function EnsureSitePagesLibrary(siteUrl: string): Promise<IGetSitePagesLibrarResult> {
92
- let url = `${GetRestBaseUrl(siteUrl)}/web/lists/EnsureSitePagesLibrary`
93
- + `?$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists&$expand=RootFolder`;
94
- let response = await GetJson<iList>(url, null, {
95
- method: "POST",
96
- jsonMetadata: jsonTypes.nometadata,
97
- includeDigestInPost: true,
98
- ...longLocalCache
99
- });
100
-
101
- if (!isNullOrUndefined(response) && !isNullOrUndefined(response.RootFolder)) {
102
- return {
103
- Id: response.Id,
104
- Name: response.RootFolder.Name,
105
- ServerRelativeUrl: response.RootFolder.ServerRelativeUrl
106
- };
107
- }
108
- return null;
109
- }
110
-
111
- export function GetSiteAssetLibrary(siteUrl: string, sync?: false): Promise<IGetSiteAssetLibraryResult>;
112
- export function GetSiteAssetLibrary(siteUrl: string, sync: true): IGetSiteAssetLibraryResult;
113
- export function GetSiteAssetLibrary(siteUrl: string, sync?: boolean): IGetSiteAssetLibraryResult | Promise<IGetSiteAssetLibraryResult> {
114
- let reqUrl = `${GetRestBaseUrl(siteUrl)}/web/lists?`
115
- //Issue 1492: isSiteAssetsLibrary eq true does not work for reader users.
116
- //+ `$filter=isSiteAssetsLibrary eq true&$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists`
117
- + `$filter=EntityTypeName%20eq%20%27SiteAssets%27&$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists`
118
- + `&$expand=RootFolder`;
119
-
120
- let caller = sync ? GetJsonSync : GetJson;
121
-
122
- let result = caller<IGetSiteAssetLibraryReturnValue>(reqUrl, null, { ...longLocalCache, jsonMetadata: jsonTypes.nometadata });
123
-
124
- let transform: (v: IGetSiteAssetLibraryReturnValue) => IGetSiteAssetLibraryResult = (v) => {
125
- if (isNotEmptyArray(v && v.value)) {
126
- let assetLibrary = v.value[0];
127
- return {
128
- Id: assetLibrary.Id,
129
- Name: assetLibrary.RootFolder.Name,
130
- ServerRelativeUrl: assetLibrary.RootFolder.ServerRelativeUrl
131
- };
132
- }
133
- return null;
134
- };
135
-
136
- if (isPromise(result))
137
- return result.then(r => transform(r), () => null);
138
- else
139
- return result.success ? transform(result.result) : null;
140
- }
141
-
142
- /** Return the list Title */
143
- export function GetListTitle(siteUrl: string, listIdOrTitle: string): Promise<string> {
144
- siteUrl = GetSiteUrl(siteUrl);
145
-
146
- return GetJson<{ d: { Title: string; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/Title`, null, { allowCache: true })
147
- .then(r => {
148
- return r.d.Title;
149
- })
150
- .catch<string>(() => null);
151
- }
152
-
153
- /** Return the list */
154
- export function GetList(siteUrlOrId: string, listIdOrTitle: string, options?: {
155
- includeViews?: boolean;
156
- viewOptions?: IListViewOptions;
157
- includeContentTypes?: boolean;
158
- includeRootFolder?: boolean;
159
- includeEventReceivers?: boolean;
160
- }): Promise<iList> {
161
- let siteUrl = GetSiteUrl(siteUrlOrId);
162
-
163
- if (isNullOrEmptyString(listIdOrTitle)) return null;
164
-
165
- return GetJson<{ d: iList; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `?$select=${LIST_SELECT}&$expand=${LIST_EXPAND}`, null, { allowCache: true })
166
- .then(async r => {
167
- let list = r.d;
168
- if (options) {
169
- if (options.includeViews)
170
- list.Views = await GetListViews(siteUrl, listIdOrTitle, options.viewOptions);
171
- if (options.includeContentTypes)
172
- list.ContentTypes = await GetListContentTypes(siteUrl, listIdOrTitle);
173
- if (options.includeRootFolder)
174
- list.RootFolder = await GetListRootFolder(siteUrl, listIdOrTitle);
175
- if (options.includeEventReceivers)
176
- list.EventReceivers = await GetListEventReceivers(siteUrl, listIdOrTitle);
177
- }
178
-
179
- if (list.EffectiveBasePermissions
180
- && (isString(list.EffectiveBasePermissions.High)
181
- || isString(list.EffectiveBasePermissions.Low))) {
182
- list.EffectiveBasePermissions = {
183
- High: Number(list.EffectiveBasePermissions.High),
184
- Low: Number(list.EffectiveBasePermissions.Low)
185
- };
186
- }
187
-
188
- return list;
189
- })
190
- .catch<iList>(() => null);
191
- }
192
- /** Return the list */
193
- export function GetListSync(siteUrl: string, listIdOrTitle: string): iList {
194
- siteUrl = GetSiteUrl(siteUrl);
195
-
196
- if (isNullOrEmptyString(listIdOrTitle)) return null;
197
-
198
- let result = GetJsonSync<{ d: iList; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `?$select=${LIST_SELECT}&$expand=${LIST_EXPAND}`, null, shortLocalCache);
199
- if (result && result.success) {
200
- let list = result.result.d;
201
-
202
- if (list.EffectiveBasePermissions
203
- && (isString(list.EffectiveBasePermissions.High)
204
- || isString(list.EffectiveBasePermissions.Low))) {
205
- list.EffectiveBasePermissions = {
206
- High: Number(list.EffectiveBasePermissions.High),
207
- Low: Number(list.EffectiveBasePermissions.Low)
208
- };
209
- }
210
-
211
- return list;
212
- }
213
- else return null;
214
- }
215
-
216
- export function GetListNameSync(webUrl: string, listIdOrTitle: string): string {
217
- let list = GetListSync(webUrl, listIdOrTitle);
218
- return NormalizeListName({ EntityTypeName: list.EntityTypeName, BaseType: list.BaseType });
219
- }
220
-
221
- export async function GetListName(webUrl: string, listIdOrTitle: string) {
222
- let list = await GetList(webUrl, listIdOrTitle);
223
- return NormalizeListName({ EntityTypeName: list.EntityTypeName, BaseType: list.BaseType });
224
- }
225
-
226
- export function GetListRootFolder(siteUrlOrId: string, listIdOrTitle: string): Promise<{ ServerRelativeUrl: string; Name: string; }> {
227
- let siteUrl = GetSiteUrl(siteUrlOrId);
228
-
229
- return GetJson<{
230
- d: { ServerRelativeUrl: string; Name: string; };
231
- }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/RootFolder?$Select=Name,ServerRelativeUrl`,
232
- null, longLocalCache)
233
- .then(r => {
234
- return r.d;
235
- })
236
- .catch<{ ServerRelativeUrl: string; Name: string; }>(() => null);
237
- }
238
-
239
- export function GetListRootFolderSync(siteUrlOrId: string, listIdOrTitle: string): { ServerRelativeUrl: string; Name: string; } {
240
- let siteUrl = GetSiteUrl(siteUrlOrId);
241
-
242
- let result = GetJsonSync<{
243
- d: { ServerRelativeUrl: string; Name: string; };
244
- }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/RootFolder?$Select=Name,ServerRelativeUrl`,
245
- null, longLocalCache);
246
-
247
- return SafeIfElse(() => result.result.d, null);
248
- }
249
-
250
- export function GetListField(siteUrlOrId: string, listIdOrTitle: string, fieldIdOrName: string, refreshCache?: boolean): Promise<IFieldInfo> {
251
- let siteUrl = GetSiteUrl(siteUrlOrId);
252
-
253
- var url = GetListRestUrl(siteUrl, listIdOrTitle) + `/fields`;
254
-
255
- if (isValidGuid(fieldIdOrName)) {
256
- url += `('${normalizeGuid(fieldIdOrName)}')`;
257
- } else {
258
- url += `/getbyinternalnameortitle(@u)?@u='${encodeURIComponent(fieldIdOrName)}'`;
259
- }
260
-
261
- let result = GetJson<{ d: IFieldInfo; }>(url, null, { allowCache: refreshCache !== true })
262
- .then(r => {
263
- return r.d;
264
- })
265
- .catch<IFieldInfo>(() => null);
266
-
267
- return result;
268
- }
269
-
270
- function _getListFieldsRequestUrl(siteUrl: string, listIdOrTitle: string) {
271
- return GetListRestUrl(siteUrl, listIdOrTitle) + `/fields`;
272
- }
273
-
274
- /** Gets ID, Title, ContentType Author, Editor, Created and Modified fields */
275
- export function GetStandardListFields(siteUrlOrId: string, listIdOrTitle: string, refreshCache?: boolean) {
276
- let fieldNames = ["ID", "Title", "ContentType", "Author", "Editor", "Created", "Modified"];
277
- return GetListFields(siteUrlOrId, listIdOrTitle, { refreshCache: refreshCache, fieldNames: fieldNames });
278
- }
279
-
280
- export interface IGetListFieldsOptions {
281
- refreshCache?: boolean;
282
- /** fieldNames that should be returned with the request */
283
- fieldNames?: string[];
284
- }
285
-
286
- function _processGetListFields(fields: IFieldInfo[], fieldNames: string[]) {
287
- if (isNullOrEmptyArray(fields)) {
288
- return fields as IFieldInfoEX[];
289
- }
290
- let extendedFields = fields.map(f => extendFieldInfo(f, fields));
291
-
292
- if (!isNullOrEmptyArray(fieldNames)) {
293
- return extendedFields.filter((extendedField) => {
294
- return fieldNames.includes(extendedField.InternalName);
295
- });
296
- }
297
- return extendedFields;
298
- }
299
-
300
- export function GetListFields(siteUrlOrId: string, listIdOrTitle: string, options: IGetListFieldsOptions = {}): Promise<IFieldInfoEX[]> {
301
- let siteUrl = GetSiteUrl(siteUrlOrId);
302
- let url = _getListFieldsRequestUrl(siteUrl, listIdOrTitle);
303
-
304
- let restOptions: IRestOptions = {
305
- allowCache: options.refreshCache !== true,
306
- jsonMetadata: jsonTypes.nometadata
307
- };
308
-
309
- return GetJson<{ value: IFieldInfo[]; }>(url, null, restOptions)
310
- .then((result) => {
311
- return _processGetListFields(result.value, options.fieldNames);
312
- }).catch<IFieldInfoEX[]>(() => {
313
- return null;
314
- });
315
- }
316
-
317
- export function GetListFieldsSync(siteUrlOrId: string, listIdOrTitle: string, options: IGetListFieldsOptions = {}): IFieldInfoEX[] {
318
- let siteUrl = GetSiteUrl(siteUrlOrId);
319
- let url = _getListFieldsRequestUrl(siteUrl, listIdOrTitle);
320
-
321
- let restOptions: IRestOptions = {
322
- allowCache: options.refreshCache !== true,
323
- jsonMetadata: jsonTypes.nometadata
324
- };
325
-
326
- let result = GetJsonSync<{ value: IFieldInfo[]; }>(url, null, restOptions);
327
- if (result.success && !isNullOrUndefined(result.result)) {
328
- return _processGetListFields(result.result.value, options.fieldNames);
329
- }
330
- return null;
331
- }
332
-
333
- export async function GetListFieldsAsHash(siteUrlOrId: string, listIdOrTitle: string, refreshCache?: boolean): Promise<IFieldInfoExHash> {
334
- let siteUrl = GetSiteUrl(siteUrlOrId);
335
-
336
- let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: refreshCache });
337
- let hash: IFieldInfoExHash = {};
338
- if (isNotEmptyArray(fields)) {
339
- hash = toHash(fields, f => f.InternalName);
340
- }
341
- return hash;
342
- }
343
-
344
- export function GetListFieldsAsHashSync(siteUrlOrId: string, listIdOrTitle: string, refreshCache?: boolean): IFieldInfoExHash {
345
- let siteUrl = GetSiteUrl(siteUrlOrId);
346
-
347
- let fields = GetListFieldsSync(siteUrl, listIdOrTitle, { refreshCache: refreshCache });
348
- let hash: IFieldInfoExHash = {};
349
- if (isNotEmptyArray(fields)) {
350
- fields.forEach(f => { hash[f.InternalName] = f; });
351
- }
352
- return hash;
353
- }
354
-
355
- export function GetListWorkflows(siteUrl: string, listIdOrTitle: string, refreshCache?: boolean): Promise<IListWorkflowAssociation[]> {
356
- siteUrl = GetSiteUrl(siteUrl);
357
-
358
- return GetJson<{
359
- d: { results: IListWorkflowAssociation[]; };
360
- }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/workflowAssociations`,
361
- null, { allowCache: refreshCache !== true })
362
- .then(r => {
363
- if (r && r.d && isNotEmptyArray(r.d.results)) {
364
- r.d.results.forEach(wf => {
365
- wf.BaseId = normalizeGuid(wf.BaseId);
366
- wf.Id = normalizeGuid(wf.Id);
367
- wf.ListId = normalizeGuid(wf.ListId);
368
- wf.WebId = normalizeGuid(wf.WebId);
369
- });
370
- return r.d.results;
371
- }
372
- else return [];
373
- })
374
- .catch<IListWorkflowAssociation[]>(() => []);
375
- }
376
-
377
- export function UserHasManagePermissions(siteUrl: string, listIdOrTitle: string): Promise<boolean> {
378
- siteUrl = GetSiteUrl(siteUrl);
379
-
380
- return GetJson<{ d: { EffectiveBasePermissions: { High: number; Low: number; }; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/EffectiveBasePermissions`, null,
381
- { ...shortLocalCache })
382
- .then(r => {
383
- return new SPBasePermissions(r.d.EffectiveBasePermissions).has(SPBasePermissionKind.ManageLists);
384
- })
385
- .catch<boolean>(() => null);
386
- }
387
-
388
- export function UserHasEditPermissions(siteUrl: string, listIdOrTitle: string): Promise<boolean> {
389
- return UserHasPermissions(siteUrl, listIdOrTitle, SPBasePermissionKind.EditListItems);
390
- }
391
-
392
- export function UserHasPermissions(siteUrlOrId: string, listIdOrTitle: string, permissionKind: SPBasePermissionKind): Promise<boolean> {
393
- let siteUrl = GetSiteUrl(siteUrlOrId);
394
-
395
- return GetJson<{ d: { EffectiveBasePermissions: { High: number; Low: number; }; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/EffectiveBasePermissions`, null,
396
- { ...shortLocalCache })
397
- .then(r => {
398
- return new SPBasePermissions(r.d.EffectiveBasePermissions).has(permissionKind);
399
- })
400
- .catch<boolean>(() => null);
401
- }
402
-
403
- export function UserHasPermissionsSync(siteUrlOrId: string, listIdOrTitle: string, permissionKind: SPBasePermissionKind): boolean {
404
- let siteUrl = GetSiteUrl(siteUrlOrId);
405
-
406
- const res = GetJsonSync<{ d: { EffectiveBasePermissions: { High: number; Low: number; }; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/EffectiveBasePermissions`, null,
407
- { ...shortLocalCache });
408
-
409
- return new SPBasePermissions(res.result.d.EffectiveBasePermissions).has(permissionKind);
410
- }
411
-
412
- /** 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 */
413
- export async function CreateField(siteUrl: string, listIdOrTitle: string, options: {
414
- Title?: string;
415
- Type?: FieldTypes;
416
- Required?: boolean;
417
- Indexed?: boolean;
418
- SchemaXml?: string;
419
- SchemaXmlSpecificInternalName?: boolean;
420
- SkipAddToDefaultView?: boolean;
421
- ClientSideComponentId?: string;
422
- ClientSideComponentProperties?: string;
423
- JSLink?: string;
424
-
425
- }): Promise<IFieldInfoEX> {
426
- siteUrl = GetSiteUrl(siteUrl);
427
-
428
- let finish = async (result: IFieldInfo) => {
429
- if (!result) {
430
- return null;
431
- }
432
-
433
- let internalName = result.InternalName;
434
- //we need to clear and reload the list fields cache, so call it and return our field from that collection.
435
- let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
436
-
437
- try {
438
- if (options.SkipAddToDefaultView !== true) {
439
- //try to add it to default view, don't wait for it
440
- GetListViews(siteUrl, listIdOrTitle).then(views => {
441
- let defaultView = firstOrNull(views, v => v.DefaultView);
442
- if (defaultView)
443
- GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/views('${defaultView.Id}')/ViewFields/addViewField('${internalName}')`, null, { method: "POST", spWebUrl: siteUrl });
444
- });
445
- }
446
- }
447
- catch (e) { }
448
-
449
- return firstOrNull(fields, f => f.InternalName === internalName);
450
- };
451
-
452
- if (!isNullOrEmptyString(options.SchemaXml)) {
453
- try {
454
- let updateObject: IDictionary<any> = {
455
- 'parameters': {
456
- '__metadata': { 'type': 'SP.XmlSchemaFieldCreationInformation' },
457
- 'SchemaXml': options.SchemaXml,
458
- 'Options': options.SchemaXmlSpecificInternalName !== true ?
459
- 4 ://SP.AddFieldOptions.addToAllContentTypes
460
- 4 | 8//SP.AddFieldOptions.addToAllContentTypes | addFieldInternalNameHint
461
- }
462
- };
463
- let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/fields/createFieldAsXml`;
464
- let newFieldResult = await GetJson<{ d: IFieldInfo; }>(url, JSON.stringify(updateObject));
465
-
466
- if (!isNullOrUndefined(newFieldResult)
467
- && !isNullOrUndefined(newFieldResult.d)) {
468
- if ((!isNullOrEmptyString(options.Title) && options.Title !== newFieldResult.d.Title)
469
- || (isBoolean(options.Indexed) && options.Indexed !== newFieldResult.d.Indexed)) {
470
- let updatedField = await UpdateField(siteUrl, listIdOrTitle, newFieldResult.d.InternalName, {
471
- Title: options.Title,
472
- Indexed: options.Indexed === true
473
- });
474
- return finish(updatedField);
475
- }
476
- }
477
-
478
- return finish(newFieldResult && newFieldResult.d);
479
- } catch {
480
- }
481
- return null;
482
- } else if (!isNullOrEmptyString(options.Title) && !isNullOrUndefined(options.Type)) {
483
- let updateObject: IDictionary<any> = {
484
- '__metadata': { 'type': 'SP.Field' },
485
- 'Title': options.Title,
486
- 'FieldTypeKind': options.Type,
487
- 'Required': options.Required === true,
488
- 'Indexed': options.Indexed === true
489
- };
490
- if (!isNullOrEmptyString(options.ClientSideComponentId)) {
491
- updateObject.ClientSideComponentId = options.ClientSideComponentId;
492
- }
493
- if (!isNullOrEmptyString(options.ClientSideComponentProperties)) {
494
- updateObject.ClientSideComponentProperties = options.ClientSideComponentProperties;
495
- }
496
- if (!isNullOrEmptyString(options.JSLink)) {
497
- updateObject.JSLink = options.JSLink;
498
- }
499
-
500
- try {
501
- let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/fields`;
502
- let newFieldResult = await GetJson<{ d: IFieldInfo; }>(url, JSON.stringify(updateObject));
503
- return finish(newFieldResult && newFieldResult.d);
504
- } catch {
505
- }
506
- return null;
507
- }
508
- else {
509
- console.error("You must send either SchemaXml or Title and Type");
510
- return null;
511
- }
512
- }
513
- /** Update field SchemaXml OR Title, only 1 update at a time supported. */
514
- export async function UpdateField(siteUrlOrId: string, listIdOrTitle: string, fieldInternalName: string, options: {
515
- Title?: string;
516
- Indexed?: boolean;
517
- /** Update 'Choices' propertry on 'Choice' and 'MultiChoice' field types. */
518
- Choices?: string[];
519
- SchemaXml?: string;
520
- FieldType?: FieldTypeAsString;
521
- Required?: boolean;
522
- Hidden?: boolean;
523
- JSLink?: string;
524
- ClientSideComponentId?: string;
525
- ClientSideComponentProperties?: string;
526
- }): Promise<IFieldInfoEX> {
527
- let siteUrl = GetSiteUrl(siteUrlOrId);
528
-
529
- let finish = async () => {
530
- //we need to clear and reload the list fields cache, so call it and return our field from that collection.
531
- let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
532
- return firstOrNull(fields, f => f.InternalName === fieldInternalName);
533
- };
534
-
535
- let fields = await GetListFieldsAsHash(siteUrl, listIdOrTitle, true);
536
- let thisField = fields[fieldInternalName];
537
-
538
- //updates can either be SchemaXml, or others. Cannot be both.
539
- let updates: IDictionary<any> = {
540
- '__metadata': { 'type': 'SP.Field' }
541
- };
542
-
543
- if (!isNullOrEmptyString(options.SchemaXml)) {
544
- updates.SchemaXml = options.SchemaXml;
545
- }
546
- else {
547
- //cannot send schema updates with other updates.
548
- if (!isNullOrEmptyString(options.Title)) {
549
- updates.Title = options.Title;
550
- }
551
- if (!isNullOrEmptyString(options.FieldType)) {
552
- updates.TypeAsString = options.FieldType;
553
- }
554
- if (isBoolean(options.Required)) {
555
- updates.Required = options.Required === true;
556
- }
557
- if (isBoolean(options.Indexed)) {
558
- updates.Indexed = options.Indexed === true;
559
- }
560
- if (!isNullOrEmptyArray(options.Choices)) {
561
- let choiceType = options.FieldType || thisField.TypeAsString;
562
- if (choiceType === "Choice" || choiceType === "MultiChoice") {
563
- updates["__metadata"]["type"] = choiceType === "Choice" ? "SP.FieldChoice" : "SP.FieldMultiChoice"
564
- updates.Choices = { "results": options.Choices };
565
- } else {
566
- logger.warn("Can only update 'Choices' property on 'Choice' and 'MultiChoice' field types.");
567
- }
568
- }
569
- if (isBoolean(options.Hidden)) {
570
- //this requries the CanToggleHidden to be in the schema... if not - we will need to add it before we can update this.
571
- let fields = await GetListFieldsAsHash(siteUrl, listIdOrTitle, false);
572
- let thisField = fields[fieldInternalName];
573
- if (thisField.Hidden !== options.Hidden) {
574
- if (thisField) {
575
- if (thisField.SchemaJson.Attributes.CanToggleHidden !== "TRUE") {
576
- await UpdateField(siteUrl, listIdOrTitle, fieldInternalName, {
577
- SchemaXml:
578
- thisField.SchemaXml.replace("<Field ", `<Field CanToggleHidden="TRUE" `)
579
- });
580
- }
581
- }
582
- updates.Hidden = options.Hidden === true;
583
- }
584
- }
585
-
586
- if (!isNullOrEmptyString(options.ClientSideComponentId))
587
- updates.ClientSideComponentId = options.ClientSideComponentId;
588
- if (!isNullOrEmptyString(options.ClientSideComponentProperties))
589
- updates.ClientSideComponentProperties = options.ClientSideComponentProperties;
590
- if (!isNullOrEmptyString(options.JSLink))
591
- updates.JSLink = options.JSLink;
592
- }
593
-
594
- if (Object.keys(updates).length > 1) {
595
- return GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/fields/getbyinternalnameortitle('${fieldInternalName}')`,
596
- JSON.stringify(updates), { xHttpMethod: "MERGE" })
597
- .then(r => {
598
- return finish();
599
- })
600
- .catch<IFieldInfoEX>(() => null);
601
- }
602
- else {
603
- console.error("You must send an option to update");
604
- return null;
605
- }
606
- }
607
-
608
- export async function DeleteField(siteUrl: string, listIdOrTitle: string, fieldInternalName: string, options?: { DeleteHiddenField?: boolean; }): Promise<boolean> {
609
- siteUrl = GetSiteUrl(siteUrl);
610
-
611
- // let finish = async () => {
612
- // //we need to clear and reload the list fields cache, so call it and return our field from that collection.
613
- // let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
614
- // return firstOrNull(fields, f => f.InternalName === fieldInternalName);
615
- // };
616
-
617
- if (options && options.DeleteHiddenField)
618
- await UpdateField(siteUrl, listIdOrTitle, fieldInternalName, { Hidden: false });
619
-
620
-
621
- return GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/fields/getbyinternalnameortitle('${fieldInternalName}')`, null, {
622
- method: "POST",
623
- xHttpMethod: "DELETE"
624
- })
625
- .then(r => true)
626
- .catch<boolean>((e) => false);
627
- }
628
-
629
- export interface IListViewOptions { includeViewFields?: boolean; }
630
- export function GetListViews(siteUrl: string, listIdOrTitle: string, options?: IListViewOptions): Promise<iListView[]> {
631
- siteUrl = GetSiteUrl(siteUrl);
632
-
633
- return GetJson<{
634
- value: iListView[];
635
- }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/views?$select=Title,Id,ServerRelativeUrl,RowLimit,Paged,ViewQuery,ListViewXml,PersonalView,MobileView,MobileDefaultView,Hidden,DefaultView,ReadOnlyView${options && options.includeViewFields ? "&$expand=ViewFields" : ""}`,
636
- null, { allowCache: true, jsonMetadata: jsonTypes.nometadata })
637
- .then(r => {
638
- let views = r.value;
639
- if (isNotEmptyArray(views)) {
640
- views.forEach(v => {
641
- v.Id = normalizeGuid(v.Id);
642
- if (options && options.includeViewFields) {
643
- v.ViewFields = v.ViewFields && v.ViewFields["Items"] && v.ViewFields["Items"] || [];
644
- }
645
- });
646
- }
647
- return views;
648
- })
649
- .catch<iListView[]>(() => null);
650
- }
651
-
652
- export function GetListViewsSync(siteUrl: string, listIdOrTitle: string): iListView[] {
653
- siteUrl = GetSiteUrl(siteUrl);
654
-
655
- let result = GetJsonSync<{
656
- d: {
657
- results: iListView[];
658
- };
659
- }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/views`,
660
- null, { allowCache: true });
661
- if (result.success) {
662
- let views = result && result.result && result.result.d && result.result.d.results;
663
- if (isNotEmptyArray(views)) {
664
- views.forEach(v => { v.Id = normalizeGuid(v.Id); });
665
- }
666
- return views;
667
- }
668
- return null;
669
- }
670
-
671
- export async function AddViewFieldToListView(siteUrl: string, listIdOrTitle: string, viewId: string, viewField: string) {
672
- return _addOrRemoveViewField(siteUrl, listIdOrTitle, viewId, viewField, "addviewfield");
673
- }
674
-
675
- export async function RemoveViewFieldFromListView(siteUrl: string, listIdOrTitle: string, viewId: string, viewField: string) {
676
- return _addOrRemoveViewField(siteUrl, listIdOrTitle, viewId, viewField, "removeviewfield");
677
- }
678
-
679
- async function _addOrRemoveViewField(siteUrl: string, listIdOrTitle: string, viewId: string, viewField: string, action: "addviewfield" | "removeviewfield") {
680
- siteUrl = GetSiteUrl(siteUrl);
681
-
682
- if (isNullOrEmptyString(viewField) || !isValidGuid(viewId)) {
683
- return false;
684
- }
685
-
686
- let views = await GetListViews(siteUrl, listIdOrTitle, { includeViewFields: true });
687
-
688
- if (isNullOrEmptyArray(views)) {
689
- return false;
690
- }
691
-
692
- let view = views.filter((view) => {
693
- return normalizeGuid(view.Id) === normalizeGuid(viewId);
694
- })[0];
695
-
696
- if (isNullOrUndefined(view)) {
697
- return false;
698
- }
699
-
700
- let hasField = view.ViewFields.includes(viewField);
701
-
702
- if (action === "addviewfield" && hasField === true) {
703
- return true;
704
- }
705
-
706
- if (action === "removeviewfield" && hasField === false) {
707
- return true;
708
- }
709
-
710
- try {
711
- let url = GetListRestUrl(siteUrl, listIdOrTitle) + `/views('${normalizeGuid(view.Id)}')/viewfields/${action}('${viewField}')`;
712
-
713
- let result = await GetJson<{ "odata.null": boolean; }>(url, null, { method: "POST" });
714
-
715
- if (result && result["odata.null"] === true) {
716
- return true;
717
- }
718
- } catch { }
719
-
720
- return false;
721
- }
722
-
723
- export function GetListContentTypes(siteUrl: string, listIdOrTitle: string,
724
- options?: Omit<IGetContentTypesOptions, "listIdOrTitle" | "fromRooWeb">, refreshCache = false): Promise<iContentType[]> {
725
- return GetContentTypes(siteUrl, { ...(options || {}), listIdOrTitle: listIdOrTitle }, refreshCache);
726
- }
727
-
728
- export function GetListContentTypesSync(siteUrl: string, listIdOrTitle: string,
729
- options?: Omit<IGetContentTypesOptions, "listIdOrTitle" | "fromRooWeb">, refreshCache = false): iContentType[] {
730
- return GetContentTypesSync(siteUrl, { ...(options || {}), listIdOrTitle: listIdOrTitle }, refreshCache);
731
- }
732
-
733
- /** generic version. for the KWIZ forms version that supports action id call GetListFormUrlAppsWeb instead */
734
- export function GetListFormUrl(siteUrl: string, listId: string, pageType: PageType, params?: { contentTypeId?: string; itemId?: number | string; rootFolder?: string }) {
735
- siteUrl = GetSiteUrl(siteUrl);
736
-
737
- if (!isValidGuid(listId)) console.error('GetListFormUrl requires a list id');
738
- let url = `${normalizeUrl(siteUrl)}/_layouts/15/listform.aspx?PageType=${pageType}&ListId=${encodeURIComponent(listId)}`;
739
- if (params) {
740
- if (!isNullOrEmptyString(params.contentTypeId))
741
- url += `&ContentTypeId=${encodeURIComponent(params.contentTypeId)}`;
742
- if (!isNullOrEmptyString(params.itemId))
743
- url += `&ID=${encodeURIComponent(params.itemId as string)}`;
744
- if (!isNullOrEmptyString(params.rootFolder))
745
- url += `&RootFolder=${encodeURIComponent(params.rootFolder)}`;
746
- }
747
- return url;
748
- }
749
-
750
- export function GetFieldSchemaSync(siteUrl: string, listIdOrTitle: string, fieldInternalName: string, refreshCache?: boolean): IFieldJsonSchema {
751
- siteUrl = GetSiteUrl(siteUrl);
752
-
753
- //ISSUE: 1516 - The get schema request will fail if the field doesn't exist in the list, so we load the fields and ensure the field
754
- //exists before requesting the schema.
755
- let fields = GetListFieldsSync(siteUrl, listIdOrTitle, {
756
- refreshCache: refreshCache,
757
- fieldNames: [fieldInternalName]
758
- });
759
-
760
- if (isNullOrEmptyArray(fields)) {
761
- return null;
762
- }
763
-
764
- let field = fields[0];
765
- return SchemaXmlToJson(field.SchemaXml);
766
- // let url = GetListRestUrl(siteUrl, listIdOrTitle) + `/fields/getByInternalNameOrTitle('${fieldInternalName}')?$select=SchemaXml`;
767
- // let result = GetJsonSync<{ d: { SchemaXml: string; }; }>(
768
- // url,
769
- // null,
770
- // {
771
- // ...shortLocalCache,
772
- // forceCacheUpdate: refreshCache === true
773
- // });
774
-
775
- // if (result && result.success) {
776
- // return SchemaXmlToJson(result.result.d.SchemaXml);
777
- // }
778
- // return null;
779
- //#endregion
780
- }
781
-
782
- export async function GetFieldSchema(siteUrl: string, listIdOrTitle: string, fieldInternalName: string, refreshCache?: boolean) {
783
- siteUrl = GetSiteUrl(siteUrl);
784
-
785
- //ISSUE: 1516 - The get schema request will fail if the field doesn't exist in the list, so we load the fields and ensure the field
786
- //exists before requesting the schema
787
- let fields = await GetListFields(siteUrl, listIdOrTitle, {
788
- refreshCache: refreshCache,
789
- fieldNames: [fieldInternalName]
790
- });
791
-
792
- if (isNullOrEmptyArray(fields)) {
793
- return null;
794
- }
795
-
796
- let field = fields[0];
797
- return SchemaXmlToJson(field.SchemaXml);
798
- }
799
-
800
- export async function GetListItems(siteUrl: string, listIdOrTitle: string, options: {
801
- /** Optional, default: 1000. 0: get all items. */
802
- rowLimit?: number;
803
- /** Id, Title, Modified, FileLeafRef, FileDirRef, FileRef, FileSystemObjectType */
804
- columns: (string | IFieldInfoEX)[];
805
- foldersBehaviour?: GeListItemsFoldersBehaviour;
806
- /** Optional, request to expand some columns. */
807
- expand?: string[];
808
- /** allow to change the jsonMetadata for this request, default: verbose */
809
- jsonMetadata?: jsonTypes;
810
- refreshCache?: boolean;
811
- /** allow to send a filter statement */
812
- $filter?: string;
813
- }): Promise<IRestItem[]> {
814
- let info = _GetListItemsInfo(siteUrl, listIdOrTitle, options);
815
-
816
- let items: IRestItem[] = [];
817
-
818
- do {
819
- let resultItems: IRestItem[] = [];
820
- let next: string = null;
821
- if (info.noMetadata) {
822
- let requestResult = (await GetJson<{
823
- value: IRestItem[];
824
- "odata.nextLink": string;
825
- }>(info.requestUrl, null, {
826
- allowCache: options.refreshCache !== true,
827
- jsonMetadata: options.jsonMetadata
828
- }));
829
- resultItems = requestResult.value;
830
- next = requestResult["odata.nextLink"];
831
- }
832
- else {
833
- let requestResult = (await GetJson<{
834
- d: {
835
- results: IRestItem[];
836
- __next?: string;
837
- };
838
- }>(info.requestUrl, null, {
839
- allowCache: options.refreshCache !== true
840
- }));
841
- resultItems = requestResult.d.results;
842
- next = requestResult.d.__next;
843
- }
844
-
845
- if (isNotEmptyArray(resultItems))
846
- items.push(...resultItems);
847
-
848
- if (info.totalNumberOfItemsToGet > items.length)
849
- info.requestUrl = next;
850
- else
851
- info.requestUrl = null;
852
-
853
- } while (!isNullOrEmptyString(info.requestUrl));
854
-
855
- return __fixGetListItemsResults(siteUrl, listIdOrTitle, items, options.foldersBehaviour, info.expandedLookupFields);
856
- }
857
-
858
- export function GetListItemsSync(siteUrl: string, listIdOrTitle: string, options: {
859
- /** Optional, default: 1000. 0: get all items. */
860
- rowLimit?: number;
861
- /** Id, Title, Modified, FileLeafRef, FileDirRef, FileRef, FileSystemObjectType */
862
- columns: (string | IFieldInfoEX)[];
863
- foldersBehaviour?: GeListItemsFoldersBehaviour;
864
- /** Optional, request to expand some columns. */
865
- expand?: string[];
866
- /** allow to send a filter statement */
867
- $filter?: string;
868
- }): IRestItem[] {
869
- let info = _GetListItemsInfo(siteUrl, listIdOrTitle, options);
870
-
871
- let items: IRestItem[] = [];
872
-
873
- do {
874
- let resultItems: IRestItem[] = [];
875
- let next: string = null;
876
- if (info.noMetadata) {
877
- let requestResult = GetJsonSync<{
878
- value: IRestItem[];
879
- "odata.nextLink": string;
880
- }>(info.requestUrl, null, { allowCache: true });
881
- if (requestResult.success) {
882
- resultItems = requestResult.result.value;
883
- next = requestResult.result["odata.nextLink"];
884
- }
885
- }
886
- else {
887
- let requestResult = GetJsonSync<{
888
- d: { results: IRestItem[]; __next?: string; };
889
- }>(info.requestUrl, null, { allowCache: true });
890
- if (requestResult.success) {
891
- resultItems = requestResult.result.d.results;
892
- next = requestResult.result.d.__next;
893
- }
894
- }
895
-
896
- if (isNotEmptyArray(resultItems))
897
- items.push(...resultItems);
898
-
899
- if (info.totalNumberOfItemsToGet > items.length)
900
- info.requestUrl = next;
901
- else
902
- info.requestUrl = null;
903
-
904
- } while (!isNullOrEmptyString(info.requestUrl));
905
-
906
- return __fixGetListItemsResults(siteUrl, listIdOrTitle, items, options.foldersBehaviour, info.expandedLookupFields);
907
- }
908
-
909
- function _GetListItemsInfo(siteUrl: string, listIdOrTitle: string, options: {
910
- /** Optional, default: 1000. 0: get all items. */
911
- rowLimit?: number;
912
- /** Id, Title, Modified, FileLeafRef, FileDirRef, FileRef, FileSystemObjectType */
913
- columns: (string | IFieldInfoEX)[];
914
- /** Optional, request to expand some columns. */
915
- expand?: string[];
916
- /** allow to change the jsonMetadata for this request, default: verbose */
917
- jsonMetadata?: jsonTypes;
918
- /** allow to send a filter statement */
919
- $filter?: string;
920
- }) {
921
- siteUrl = GetSiteUrl(siteUrl);
922
-
923
- let url = GetListRestUrl(siteUrl, listIdOrTitle) + `/items`;
924
- let queryParams: string[] = [];
925
-
926
- //Issue 8189 expand lookup fields
927
- let columns: string[] = [];
928
- let expand: string[] = [];
929
- let expandedLookupFields: IFieldInfoEX[] = [];
930
- options.columns.forEach(c => {
931
- if (isString(c)) columns.push(c);
932
- else {
933
- let internalName = c.InternalName;
934
- //Issue 828, 336
935
- if (internalName.startsWith("_")) internalName = `OData_${internalName}`;
936
-
937
- let isLookupField = c.TypeAsString === "Lookup" || c.TypeAsString === "LookupMulti";
938
- let isUserField = c.TypeAsString === "User" || c.TypeAsString === "UserMulti";
939
-
940
- if (isLookupField || isUserField) {
941
- //ISSUE: 1519 - Added lookupField property to able to retrieve value of the additional lookup field key
942
- let lookupField = (c as IFieldLookupInfo).LookupField;
943
- if (!isNullOrEmptyString(lookupField) && isLookupField) {
944
- columns.push(`${internalName}/${lookupField}`);
945
- }
946
- //we want to expand it
947
- columns.push(`${internalName}/Title`);
948
- columns.push(`${internalName}/Id`);
949
- expand.push(internalName);
950
- expandedLookupFields.push(c);
951
- }
952
- else columns.push(internalName);
953
- }
954
- });
955
- if (isNotEmptyArray(options.expand)) {
956
- expand.push(...options.expand);
957
- }
958
-
959
- //add the ones we need
960
- PushNoDuplicate(columns, "Id");
961
- PushNoDuplicate(columns, "FileRef");
962
- PushNoDuplicate(columns, "FileSystemObjectType");
963
-
964
- queryParams.push(`$select=${encodeURIComponent(makeUniqueArray(columns).join(','))}`);
965
-
966
- if (isNotEmptyArray(expand))
967
- queryParams.push(`$expand=${encodeURIComponent(makeUniqueArray(expand).join(','))}`);
968
-
969
- let batchSize = 2000;
970
- let limit = options.rowLimit >= 0 && options.rowLimit < batchSize ? options.rowLimit : batchSize;
971
- let totalNumberOfItemsToGet = !isNumber(options.rowLimit) || options.rowLimit < 1 ? 99999 : options.rowLimit > batchSize ? options.rowLimit : limit;
972
-
973
- if (!isNullOrEmptyString(options.$filter))
974
- queryParams.push(`$filter=${options.$filter}`);
975
- queryParams.push(`$top=${limit}`);
976
-
977
- let requestUrl = url + (queryParams.length > 0 ? '?' + queryParams.join('&') : '');
978
- let noMetadata = options.jsonMetadata === jsonTypes.nometadata;
979
-
980
- return { requestUrl, noMetadata, totalNumberOfItemsToGet, expandedLookupFields };
981
- }
982
-
983
- /** Find an item by id, even if it is nested in a sub-folder */
984
- export function FindListItemById(items: IRestItem[], itemId: number): IRestItem {
985
- for (let i = 0; i < items.length; i++) {
986
- let current = items[i];
987
- if (current.Id === itemId) return current;
988
- else if (isNotEmptyArray(current.__Items))//folder? look inside
989
- {
990
- let nestedResult = FindListItemById(current.__Items, itemId);
991
- if (!isNullOrUndefined(nestedResult)) return nestedResult;
992
- }
993
- }
994
- //not found
995
- return null;
996
- }
997
-
998
- function _getListEventReceiversRequestUrl(siteUrl: string, listIdOrTitle: string) {
999
- return GetListRestUrl(siteUrl, listIdOrTitle) + `/EventReceivers`
1000
- }
1001
-
1002
- export async function GetListEventReceivers(siteUrl: string, listIdOrTitle: string, refreshCache?: boolean): Promise<ISPEventReceiver[]> {
1003
- try {
1004
- let url = _getListEventReceiversRequestUrl(siteUrl, listIdOrTitle);
1005
- let response = await GetJson<{
1006
- value: ISPEventReceiver[];
1007
- }>(url,
1008
- null, {
1009
- allowCache: refreshCache !== true,
1010
- jsonMetadata: jsonTypes.nometadata
1011
- });
1012
-
1013
- return !isNullOrUndefined(response) ? response.value : null;
1014
- } catch {
1015
- }
1016
-
1017
- return null;
1018
- }
1019
-
1020
- export async function AddListEventReceiver(siteUrl: string, listIdOrTitle: string, eventReceiverDefinition: Pick<ISPEventReceiver, "EventType" | "ReceiverName" | "ReceiverUrl" | "SequenceNumber">): Promise<ISPEventReceiver> {
1021
- let newEventReceiver: Omit<ISPEventReceiver, "ReceiverId" | "Synchronization"> = {
1022
- ReceiverAssembly: "",
1023
- ReceiverClass: "",
1024
- ...eventReceiverDefinition
1025
- };
1026
-
1027
- try {
1028
- let url = _getListEventReceiversRequestUrl(siteUrl, listIdOrTitle);
1029
- let response = await GetJson<ISPEventReceiver>(url, JSON.stringify(newEventReceiver), {
1030
- method: "POST",
1031
- includeDigestInPost: true,
1032
- jsonMetadata: jsonTypes.nometadata,
1033
- headers: {
1034
- "content-type": contentTypes.json
1035
- }
1036
- });
1037
-
1038
- return !isNullOrUndefined(response) && isValidGuid(response.ReceiverId) ? response : null;
1039
- } catch {
1040
- }
1041
-
1042
- return null;
1043
- }
1044
-
1045
- export async function DeleteListEventReceiver(siteUrl: string, listIdOrTitle: string, eventReceiverId: string): Promise<boolean> {
1046
- try {
1047
- let url = `${_getListEventReceiversRequestUrl(siteUrl, listIdOrTitle)}('${normalizeGuid(eventReceiverId)}')/deleteObject`;
1048
- let response = await GetJson<{ "odata.null": boolean }>(url, null, {
1049
- method: "POST",
1050
- includeDigestInPost: true,
1051
- jsonMetadata: jsonTypes.nometadata
1052
- });
1053
-
1054
- return !isNullOrUndefined(response) && response["odata.null"] === true;
1055
- } catch {
1056
- }
1057
-
1058
- return false;
1059
- }
1060
-
1061
- /** timestamp of changes:
1062
- * - item updates
1063
- * - changes to columns
1064
- * - content types
1065
- * - list versioning settings
1066
- * - list title/description
1067
- * - content approval settings
1068
- * does not track:
1069
- * - Changes to views
1070
- * - changing list/items permissions
1071
- */
1072
- export function GetListLastItemModifiedDate(siteUrl: string, listIdOrTitle: string, options: {
1073
- sync: true;
1074
- refreshCache?: boolean;
1075
- /** ignore system changes */
1076
- userChangesOnly?: boolean;
1077
- }): string;
1078
- /** timestamp of changes:
1079
- * - item updates
1080
- * - changes to columns
1081
- * - content types
1082
- * - list versioning settings
1083
- * - list title/description
1084
- * - content approval settings
1085
- * does not track:
1086
- * - Changes to views
1087
- * - changing list/items permissions
1088
- */
1089
- export function GetListLastItemModifiedDate(siteUrl: string, listIdOrTitle: string, options?: {
1090
- sync?: false;
1091
- refreshCache?: boolean;
1092
- /** ignore system changes */
1093
- userChangesOnly?: boolean;
1094
- }): Promise<string>;
1095
-
1096
- export function GetListLastItemModifiedDate(siteUrl: string, listIdOrTitle: string, options?: {
1097
- sync?: boolean;
1098
- refreshCache?: boolean;
1099
- /** ignore system changes */
1100
- userChangesOnly?: boolean;
1101
- }): string | Promise<string> {
1102
- siteUrl = GetSiteUrl(siteUrl);
1103
-
1104
- let endPoint = options && options.userChangesOnly ? 'LastItemUserModifiedDate' : 'LastItemModifiedDate';
1105
-
1106
- let sync = options && options.sync ? true : false;
1107
- let caller = sync ? GetJsonSync : GetJson;
1108
-
1109
- let result = caller<{ value: string; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/${endPoint}`, null, {
1110
- allowCache: true,//in memory only
1111
- jsonMetadata: jsonTypes.nometadata,
1112
- forceCacheUpdate: options && options.refreshCache === true || false
1113
- });
1114
-
1115
- if (isPromise(result))
1116
- return result.then(r => r.value, () => null);
1117
- else
1118
- return result.success ? result.result.value : null;
1119
- }
1120
-
1121
- export async function ReloadListLastModified(siteUrl: string, listIdOrTitle: string) {
1122
- await GetListLastItemModifiedDate(siteUrl, listIdOrTitle, { refreshCache: true });
1123
- //make sure we do it for both title and id, we don't know how the other callers may use this in their API
1124
-
1125
- if (!isValidGuid(listIdOrTitle)) {
1126
- try {
1127
- var listId = GetListId(siteUrl, listIdOrTitle);
1128
- await GetListLastItemModifiedDate(siteUrl, listId, { refreshCache: true });
1129
- } catch (e) { }
1130
- }
1131
- else {
1132
- try {
1133
- var listTitle = await GetListTitle(siteUrl, listIdOrTitle);
1134
- await GetListLastItemModifiedDate(siteUrl, listTitle, { refreshCache: true });
1135
- } catch (e) { }
1136
- }
1137
- }
1138
-
1139
- export async function ListHasUniquePermissions(siteUrl: string, listIdOrTitle: string): Promise<boolean> {
1140
- let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/?$select=hasuniqueroleassignments`;
1141
- let has = await GetJson<{ HasUniqueRoleAssignments: boolean }>(url, undefined, { allowCache: false, jsonMetadata: jsonTypes.nometadata });
1142
- return has.HasUniqueRoleAssignments === true;
1143
- }
1144
- export async function RestoreListPermissionInheritance(siteUrl: string, listIdOrTitle: string): Promise<void> {
1145
- let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/ResetRoleInheritance`;
1146
- await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1147
- }
1148
- export async function BreakListPermissionInheritance(siteUrl: string, listIdOrTitle: string, clear = true): Promise<void> {
1149
- let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/breakroleinheritance(copyRoleAssignments=${clear ? 'false' : 'true'}, clearSubscopes=true)`;
1150
- await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1151
- }
1152
- export async function AssignListPermission(siteUrl: string, listIdOrTitle: string, principalId: number, roleId: number) {
1153
- let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/roleassignments/addroleassignment(principalid=${principalId},roleDefId=${roleId})`;
1154
- await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1155
- }
1156
- export async function RemoveListPermission(siteUrl: string, listIdOrTitle: string, principalId: number, roleId: number) {
1157
- let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/roleassignments/removeroleassignment(principalid=${principalId},roleDefId=${roleId})`;
1158
- await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1159
- }
1160
-
1161
- interface iCreateListResult {
1162
- AllowContentTypes: boolean,
1163
- BaseTemplate: ListTemplateTypes,
1164
- BaseType: BaseTypes,
1165
- ContentTypesEnabled: boolean,
1166
- Created: string,
1167
- DefaultItemOpenUseListSetting: boolean,
1168
- Description: string,
1169
- DisableCommenting: boolean,
1170
- DisableGridEditing: boolean,
1171
- DocumentTemplateUrl: string,//"/sites/s/cms/CMSLayouts/Forms/template.dotx",
1172
- DraftVersionVisibility: 0,
1173
- EnableAttachments: boolean,
1174
- EnableFolderCreation: boolean,
1175
- EnableMinorVersions: boolean,
1176
- EnableModeration: false,
1177
- EnableRequestSignOff: boolean,
1178
- EnableVersioning: boolean,
1179
- EntityTypeName: string,//"CMSLayouts",
1180
- ExemptFromBlockDownloadOfNonViewableFiles: boolean,
1181
- FileSavePostProcessingEnabled: boolean,
1182
- ForceCheckout: boolean,
1183
- HasExternalDataSource: boolean,
1184
- Hidden: boolean,
1185
- Id: string,//"c21d4eb4-70cc-4c95-925a-aa34bb9e01e0",
1186
- ImagePath: {
1187
- DecodedUrl: string,//"/_layouts/15/images/itdl.png?rev=47"
1188
- },
1189
- ImageUrl: string,//"/_layouts/15/images/itdl.png?rev=47",
1190
- IsApplicationList: boolean,
1191
- IsCatalog: boolean,
1192
- IsPrivate: boolean,
1193
- ItemCount: 0,
1194
- LastItemDeletedDate: string,//"2024-02-05T18:26:05Z",
1195
- LastItemModifiedDate: string,//"2024-02-05T18:26:06Z",
1196
- LastItemUserModifiedDate: string,//"2024-02-05T18:26:05Z",
1197
- ListExperienceOptions: ListExperienceOptions,
1198
- ListItemEntityTypeFullName: string,//"SP.Data.CMSLayoutsItem",
1199
- MajorVersionLimit: number,//500,
1200
- MajorWithMinorVersionsLimit: number,//0,
1201
- MultipleDataList: boolean,
1202
- NoCrawl: boolean,
1203
- ParentWebPath: {
1204
- DecodedUrl: string,//"/sites/s/cms"
1205
- },
1206
- ParentWebUrl: string,//"/sites/s/cms",
1207
- ParserDisabled: boolean,
1208
- ServerTemplateCanCreateFolders: boolean,
1209
- TemplateFeatureId: string,//"00bfea71-e717-4e80-aa17-d0c71b360101",
1210
- Title: string,//"CMSLayouts"
1211
- }
1212
- export async function CreateList(siteUrl: string, info: {
1213
- title: string; description: string;
1214
- type: BaseTypes; template: ListTemplateTypes;
1215
- }): Promise<iCreateListResult> {
1216
- let url = `${GetRestBaseUrl(siteUrl)}/web/lists`;
1217
- const body = {
1218
- __metadata: { type: 'SP.List' },
1219
- AllowContentTypes: false,
1220
- ContentTypesEnabled: false,
1221
- BaseTemplate: info.template,
1222
- BaseType: info.type,
1223
- Description: info.description,
1224
- Title: info.title
1225
- };
1226
-
1227
- let newList = (await GetJson<{ d: iCreateListResult }>(url, jsonStringify(body))).d;
1228
- normalizeGuid(newList.Id);
1229
- return newList;
1230
- }
1231
-
1232
- export async function SearchList(siteUrl: string, listIdOrTitle: string, query: string) {
1233
- let listId = GetListId(siteUrl, listIdOrTitle);
1234
- let url = `${GetRestBaseUrl(siteUrl)}/search/query?querytext='(${query}*)'&querytemplate='{searchTerms} (NormListID:${listId})'`;
1235
-
1236
- try {
1237
- const result = await GetJson<{
1238
- ElapsedTime: number,
1239
- PrimaryQueryResult: {
1240
- CustomResults: [];
1241
- QueryId: string;//"7fdf01b1-f6f0-4d42-b046-d9db22597084",
1242
- QueryRuleId: string;// "00000000-0000-0000-0000-000000000000",
1243
- RefinementResults: null,
1244
- RelevantResults: {
1245
- RowCount: number,
1246
- Table: {
1247
- Rows: {
1248
- Cells: {
1249
- Key:
1250
- /** "1989637621861439888" "Edm.Int64" */
1251
- "WorkId"
1252
- /** "1000.1073372","Edm.Double" */
1253
- | "Rank"
1254
- /** "sample md as text","Edm.String" */
1255
- | "Title"
1256
- /** "Shai Petel", "Edm.String" */
1257
- | "Author"
1258
- /** "91", "Edm.Int64" */
1259
- | "Size"
1260
- /** "https://kwizcom.sharepoint.com/sites/s/cms/CMSPages/sample md as text.txt", "Edm.String" */
1261
- | "Path"
1262
- /** null, "Null" */
1263
- | "Description"
1264
- /** "# hello world! - bullet - bullet | table | col | | ----- | ---- | |table |col | ", "Edm.String" */
1265
- | "HitHighlightedSummary"
1266
- /** "https://kwizcom.sharepoint.com/_api/v2.1/drives/b!8NAeO-mocUWbgyMTqcM0Mfh8XKPhn7xOhhMrO5KfJjBs_gXb9j8ZRaLxuppgj0Uk/items/01OBXW4FLU6G4LIT7AX5BK3MXHDICVTIOT/thumbnails/0/c400x99999/content?prefer=noRedirect", "Edm.String" */
1267
- | "PictureThumbnailURL"
1268
- /** null,"Null" */
1269
- | "ServerRedirectedURL"
1270
- /** null,"Null" */
1271
- | "ServerRedirectedEmbedURL"
1272
- /** null,"Null" */
1273
- | "ServerRedirectedPreviewURL"
1274
- /** "txt","Edm.String" */
1275
- | "FileExtension"
1276
- /** "0x010100CB212272F1372446A2423F0A2BEA12B8", "Edm.String" */
1277
- | "ContentTypeId"
1278
- /** "https://kwizcom.sharepoint.com/sites/s/cms/CMSPages/Forms/AllItems.aspx","Edm.String" */
1279
- | "ParentLink"
1280
- /** "1","Edm.Int64" */
1281
- | "ViewsLifeTime"
1282
- /** "1","Edm.Int64" */
1283
- | "ViewsRecent"
1284
- /** "2024-02-22T18:35:48.0000000Z","Edm.DateTime" */
1285
- | "LastModifiedTime"
1286
- /** "txt","Edm.String" */
1287
- | "FileType"
1288
- /** "1989637621861439888","Edm.Int64" */
1289
- | "DocId"
1290
- /** "https://kwizcom.sharepoint.com/sites/s/cms","Edm.String" */
1291
- | "SPWebUrl"
1292
- /** "{b4b8f174-e04f-42bf-adb2-e71a0559a1d3}","Edm.String" */
1293
- | "UniqueId"
1294
- /** "3b1ed0f0-a8e9-4571-9b83-2313a9c33431","Edm.String" */
1295
- | "SiteId"
1296
- /** "a35c7cf8-9fe1-4ebc-8613-2b3b929f2630","Edm.String" */
1297
- | "WebId"
1298
- /** "db05fe6c-3ff6-4519-a2f1-ba9a608f4524","Edm.String" */
1299
- | "ListId"
1300
- /** "https://kwizcom.sharepoint.com/sites/s/cms/CMSPages/sample md as text.txt","Edm.String" */
1301
- | "OriginalPath"
1302
- ;
1303
- Value: string,
1304
- ValueType: "Edm.Int64" | "Edm.Double" | "Edm.String" | "Edm.DateTime" | "Null"
1305
- }[]
1306
- }[]
1307
- },
1308
- TotalRows: number,
1309
- TotalRowsIncludingDuplicates: number
1310
- }
1311
- },
1312
- }>(url, null, { jsonMetadata: jsonTypes.nometadata });
1313
- logger.json(result.PrimaryQueryResult.RelevantResults, `search ${query}`);
1314
- let rows = result.PrimaryQueryResult.RelevantResults.Table.Rows;
1315
-
1316
- const mapped: (IDictionary<string | Date | number> & {
1317
- WorkId?: number;
1318
- Rank?: number;
1319
- Title?: string;
1320
- Author?: string;
1321
- Size?: number;
1322
- Path?: string;
1323
- Description?: string;
1324
- HitHighlightedSummary?: string;
1325
- PictureThumbnailURL?: string;
1326
- ServerRedirectedURL?: string;
1327
- ServerRedirectedEmbedURL?: string;
1328
- ServerRedirectedPreviewURL?: string;
1329
- FileExtension?: string;
1330
- ContentTypeId?: string;
1331
- ParentLink?: string;
1332
- ViewsLifeTime?: number;
1333
- ViewsRecent?: number;
1334
- LastModifiedTime?: Date;
1335
- FileType?: string;
1336
- DocId?: number;
1337
- SPWebUrl?: string;
1338
- UniqueId?: string;
1339
- SiteId?: string;
1340
- WebId?: string;
1341
- ListId?: string;
1342
- OriginalPath?: string;
1343
- $itemId?: number;
1344
- })[] = [];
1345
- rows.forEach(r => {
1346
- try {
1347
- const rowValues: IDictionary<string | Date | number> = {};
1348
- r.Cells.forEach(cell => {
1349
- rowValues[cell.Key] = cell.ValueType === "Edm.Int64" || cell.ValueType === "Edm.Double"
1350
- ? parseInt(cell.Value, 10)
1351
- : cell.ValueType === "Edm.DateTime"
1352
- ? new Date(cell.Value)
1353
- : cell.ValueType === "Null"
1354
- ? ""
1355
- : cell.Value
1356
- });
1357
- let resultPath = isNullOrEmptyString(rowValues.Path) ? "" : (rowValues.Path as string).toLowerCase();
1358
- let indexOfId = resultPath.toLowerCase().indexOf("id=");
1359
- let itemId = indexOfId >= 0 ? parseInt(resultPath.slice(indexOfId + 3)) : -1;
1360
- if (itemId >= 0)
1361
- rowValues.$itemId = itemId;
1362
- mapped.push(rowValues);
1363
- } catch (e) { return null; }
1364
- });
1365
-
1366
- return mapped;
1367
- } catch (e) {
1368
- logger.error(e);
1369
- }
1370
-
1371
- return [];
1372
- }
1373
-
1374
- export async function UpdateListExperience(siteUrl: string, listId: string, experience: ListExperienceOptions) {
1375
- try {
1376
- let url = GetListRestUrl(siteUrl, listId);
1377
- let data = {
1378
- "ListExperienceOptions": experience
1379
- };
1380
- let result = await GetJson(url, JSON.stringify(data), {
1381
- xHttpMethod: "MERGE",
1382
- jsonMetadata: jsonTypes.nometadata
1383
- });
1384
- return isNullOrEmptyString(result);
1385
- } catch (e) {
1386
- logger.error(e);
1387
- }
1388
- return false;
1
+ import { PushNoDuplicate, firstOrNull, makeUniqueArray, toHash } from "../../helpers/collections.base";
2
+ import { jsonStringify } from "../../helpers/json";
3
+ import { NormalizeListName, SPBasePermissions, SchemaXmlToJson, extendFieldInfos } from "../../helpers/sharepoint";
4
+ import { normalizeGuid } from "../../helpers/strings";
5
+ import { SafeIfElse, isBoolean, isNotEmptyArray, isNullOrEmptyArray, isNullOrEmptyString, isNullOrUndefined, isNumber, isPromise, isString, isValidGuid } from "../../helpers/typecheckers";
6
+ import { makeServerRelativeUrl, normalizeUrl } from "../../helpers/url";
7
+ import { IDictionary } from "../../types/common.types";
8
+ import { IRestOptions, contentTypes, jsonTypes } from "../../types/rest.types";
9
+ import { BaseTypes, FieldTypeAsString, FieldTypes, IFieldInfo, IFieldInfoEX, IFieldInfoExHash, IFieldJsonSchema, IFieldLookupInfo, ISPEventReceiver, ListTemplateTypes, PageType, SPBasePermissionKind } from "../../types/sharepoint.types";
10
+ import { GeListItemsFoldersBehaviour, IListWorkflowAssociation, IRestItem, ListExperienceOptions, iContentType, iList, iListView } from "../../types/sharepoint.utils.types";
11
+ import { ConsoleLogger } from "../consolelogger";
12
+ import { GetJson, GetJsonSync, longLocalCache, shortLocalCache } from "../rest";
13
+ import { GetRestBaseUrl, GetSiteUrl, LIST_EXPAND, LIST_SELECT } from "./common";
14
+ import { __fixGetListItemsResults } from "./listutils/common";
15
+ import { GetContentTypes, GetContentTypesSync, GetListsSync, IGetContentTypesOptions } from "./web";
16
+
17
+ const logger = ConsoleLogger.get("SharePoint.Rest.List");
18
+
19
+ /** returns /_api/web/lists/getById() or /_api/web/lists/getByTitle() */
20
+ export function GetListRestUrl(siteUrl: string, listIdOrTitle: string): string {
21
+ siteUrl = GetSiteUrl(siteUrl);
22
+
23
+ let listId = GetListId(siteUrl, listIdOrTitle);
24
+
25
+ let listPart = isValidGuid(listId) ? `getById('${normalizeGuid(listId)}')` : `getByTitle('${encodeURIComponent(listIdOrTitle)}')`;
26
+ return GetRestBaseUrl(siteUrl) + `/web/lists/${listPart}`;
27
+ }
28
+
29
+ export function GetListId(siteUrl: string, listIdOrTitle: string): string {
30
+ if (isNullOrEmptyString(listIdOrTitle)) return null;
31
+ if (isValidGuid(listIdOrTitle)) return listIdOrTitle;
32
+ //Issue 7508
33
+ //When translation is enabled, and user changes list title but he is not on the same language as the site
34
+ //he translates the list, but not changing its title
35
+ //so REST api /lists/getByTitle will not work
36
+ //instead, we need to get the list id from the web's lists collection.
37
+ let lists = GetListsSync(siteUrl);
38
+ var lower = listIdOrTitle.toLowerCase();
39
+ var list = firstOrNull(lists, l => l.Title.toLowerCase() === lower);
40
+ return list && list.Id || null;
41
+ }
42
+
43
+ /** get the list ID from a list page, such as a list view or an item form */
44
+ export function GetListIdFromPageSync(siteUrl: string, listPageUrl: string): string {
45
+ let url = `${GetRestBaseUrl(siteUrl)}/web/getlist('${makeServerRelativeUrl(listPageUrl.split('?')[0].split('#')[0])}')?$select=id`;
46
+ let response = GetJsonSync<{ Id: string; }>(url, null, {
47
+ ...longLocalCache,
48
+ jsonMetadata: jsonTypes.nometadata
49
+ });
50
+ if (!isNullOrUndefined(response) && response.success) {
51
+ let listId = response.result.Id;
52
+ return normalizeGuid(listId);
53
+ }
54
+ return null;
55
+ }
56
+
57
+ interface IGetSiteAssetLibraryResult { Id: string, Name: string, ServerRelativeUrl: string }
58
+ interface IGetSiteAssetLibraryReturnValue {
59
+ value: {
60
+ Id: string;
61
+ RootFolder: {
62
+ Name: string;
63
+ ServerRelativeUrl: string;
64
+ Exists: boolean;
65
+ };
66
+ }[];
67
+ }
68
+
69
+ /** ensures the site assets library exists and return its info. on errors - it will return null. */
70
+ export function EnsureAssetLibrary(siteUrl: string): Promise<IGetSiteAssetLibraryResult> {
71
+ siteUrl = GetSiteUrl(siteUrl);
72
+
73
+ let url = GetRestBaseUrl(siteUrl) +
74
+ "/web/lists/EnsureSiteAssetsLibrary?$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists&$expand=RootFolder";
75
+ return GetJson<{
76
+ d: { Id: string; RootFolder: { Name: string; ServerRelativeUrl: string; Exists: boolean; }; };
77
+ }>(url, null, { method: "POST", spWebUrl: siteUrl, ...longLocalCache }).then(result => {
78
+ if (result && result.d) {
79
+ return {
80
+ Id: result.d.Id,
81
+ Name: result.d.RootFolder.Name,
82
+ ServerRelativeUrl: result.d.RootFolder.ServerRelativeUrl
83
+ };
84
+ } else return null;
85
+ }).catch<IGetSiteAssetLibraryResult>(() => null);
86
+ }
87
+
88
+ interface IGetSitePagesLibrarResult { Id: string, Name: string, ServerRelativeUrl: string }
89
+
90
+ /** ensures the site pages library exists and return its info. on errors - it will return null. */
91
+ export async function EnsureSitePagesLibrary(siteUrl: string): Promise<IGetSitePagesLibrarResult> {
92
+ let url = `${GetRestBaseUrl(siteUrl)}/web/lists/EnsureSitePagesLibrary`
93
+ + `?$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists&$expand=RootFolder`;
94
+ let response = await GetJson<iList>(url, null, {
95
+ method: "POST",
96
+ jsonMetadata: jsonTypes.nometadata,
97
+ includeDigestInPost: true,
98
+ ...longLocalCache
99
+ });
100
+
101
+ if (!isNullOrUndefined(response) && !isNullOrUndefined(response.RootFolder)) {
102
+ return {
103
+ Id: response.Id,
104
+ Name: response.RootFolder.Name,
105
+ ServerRelativeUrl: response.RootFolder.ServerRelativeUrl
106
+ };
107
+ }
108
+ return null;
109
+ }
110
+
111
+ export function GetSiteAssetLibrary(siteUrl: string, sync?: false): Promise<IGetSiteAssetLibraryResult>;
112
+ export function GetSiteAssetLibrary(siteUrl: string, sync: true): IGetSiteAssetLibraryResult;
113
+ export function GetSiteAssetLibrary(siteUrl: string, sync?: boolean): IGetSiteAssetLibraryResult | Promise<IGetSiteAssetLibraryResult> {
114
+ let reqUrl = `${GetRestBaseUrl(siteUrl)}/web/lists?`
115
+ //Issue 1492: isSiteAssetsLibrary eq true does not work for reader users.
116
+ //+ `$filter=isSiteAssetsLibrary eq true&$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists`
117
+ + `$filter=EntityTypeName%20eq%20%27SiteAssets%27&$select=ID,RootFolder/Name,RootFolder/ServerRelativeUrl,RootFolder/Exists`
118
+ + `&$expand=RootFolder`;
119
+
120
+ let caller = sync ? GetJsonSync : GetJson;
121
+
122
+ let result = caller<IGetSiteAssetLibraryReturnValue>(reqUrl, null, { ...longLocalCache, jsonMetadata: jsonTypes.nometadata });
123
+
124
+ let transform: (v: IGetSiteAssetLibraryReturnValue) => IGetSiteAssetLibraryResult = (v) => {
125
+ if (isNotEmptyArray(v && v.value)) {
126
+ let assetLibrary = v.value[0];
127
+ return {
128
+ Id: assetLibrary.Id,
129
+ Name: assetLibrary.RootFolder.Name,
130
+ ServerRelativeUrl: assetLibrary.RootFolder.ServerRelativeUrl
131
+ };
132
+ }
133
+ return null;
134
+ };
135
+
136
+ if (isPromise(result))
137
+ return result.then(r => transform(r), () => null);
138
+ else
139
+ return result.success ? transform(result.result) : null;
140
+ }
141
+
142
+ /** Return the list Title */
143
+ export function GetListTitle(siteUrl: string, listIdOrTitle: string): Promise<string> {
144
+ siteUrl = GetSiteUrl(siteUrl);
145
+
146
+ return GetJson<{ d: { Title: string; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/Title`, null, { allowCache: true })
147
+ .then(r => {
148
+ return r.d.Title;
149
+ })
150
+ .catch<string>(() => null);
151
+ }
152
+
153
+ /** Return the list */
154
+ export function GetList(siteUrlOrId: string, listIdOrTitle: string, options?: {
155
+ includeViews?: boolean;
156
+ viewOptions?: IListViewOptions;
157
+ includeContentTypes?: boolean;
158
+ includeRootFolder?: boolean;
159
+ includeEventReceivers?: boolean;
160
+ }): Promise<iList> {
161
+ let siteUrl = GetSiteUrl(siteUrlOrId);
162
+
163
+ if (isNullOrEmptyString(listIdOrTitle)) return null;
164
+
165
+ return GetJson<{ d: iList; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `?$select=${LIST_SELECT}&$expand=${LIST_EXPAND}`, null, { allowCache: true })
166
+ .then(async r => {
167
+ let list = r.d;
168
+ if (options) {
169
+ if (options.includeViews)
170
+ list.Views = await GetListViews(siteUrl, listIdOrTitle, options.viewOptions);
171
+ if (options.includeContentTypes)
172
+ list.ContentTypes = await GetListContentTypes(siteUrl, listIdOrTitle);
173
+ if (options.includeRootFolder)
174
+ list.RootFolder = await GetListRootFolder(siteUrl, listIdOrTitle);
175
+ if (options.includeEventReceivers)
176
+ list.EventReceivers = await GetListEventReceivers(siteUrl, listIdOrTitle);
177
+ }
178
+
179
+ if (list.EffectiveBasePermissions
180
+ && (isString(list.EffectiveBasePermissions.High)
181
+ || isString(list.EffectiveBasePermissions.Low))) {
182
+ list.EffectiveBasePermissions = {
183
+ High: Number(list.EffectiveBasePermissions.High),
184
+ Low: Number(list.EffectiveBasePermissions.Low)
185
+ };
186
+ }
187
+
188
+ return list;
189
+ })
190
+ .catch<iList>(() => null);
191
+ }
192
+ /** Return the list */
193
+ export function GetListSync(siteUrl: string, listIdOrTitle: string): iList {
194
+ siteUrl = GetSiteUrl(siteUrl);
195
+
196
+ if (isNullOrEmptyString(listIdOrTitle)) return null;
197
+
198
+ let result = GetJsonSync<{ d: iList; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `?$select=${LIST_SELECT}&$expand=${LIST_EXPAND}`, null, shortLocalCache);
199
+ if (result && result.success) {
200
+ let list = result.result.d;
201
+
202
+ if (list.EffectiveBasePermissions
203
+ && (isString(list.EffectiveBasePermissions.High)
204
+ || isString(list.EffectiveBasePermissions.Low))) {
205
+ list.EffectiveBasePermissions = {
206
+ High: Number(list.EffectiveBasePermissions.High),
207
+ Low: Number(list.EffectiveBasePermissions.Low)
208
+ };
209
+ }
210
+
211
+ return list;
212
+ }
213
+ else return null;
214
+ }
215
+
216
+ export function GetListNameSync(webUrl: string, listIdOrTitle: string): string {
217
+ let list = GetListSync(webUrl, listIdOrTitle);
218
+ return NormalizeListName({ EntityTypeName: list.EntityTypeName, BaseType: list.BaseType });
219
+ }
220
+
221
+ export async function GetListName(webUrl: string, listIdOrTitle: string) {
222
+ let list = await GetList(webUrl, listIdOrTitle);
223
+ return NormalizeListName({ EntityTypeName: list.EntityTypeName, BaseType: list.BaseType });
224
+ }
225
+
226
+ export function GetListRootFolder(siteUrlOrId: string, listIdOrTitle: string): Promise<{ ServerRelativeUrl: string; Name: string; }> {
227
+ let siteUrl = GetSiteUrl(siteUrlOrId);
228
+
229
+ return GetJson<{
230
+ d: { ServerRelativeUrl: string; Name: string; };
231
+ }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/RootFolder?$Select=Name,ServerRelativeUrl`,
232
+ null, longLocalCache)
233
+ .then(r => {
234
+ return r.d;
235
+ })
236
+ .catch<{ ServerRelativeUrl: string; Name: string; }>(() => null);
237
+ }
238
+
239
+ export function GetListRootFolderSync(siteUrlOrId: string, listIdOrTitle: string): { ServerRelativeUrl: string; Name: string; } {
240
+ let siteUrl = GetSiteUrl(siteUrlOrId);
241
+
242
+ let result = GetJsonSync<{
243
+ d: { ServerRelativeUrl: string; Name: string; };
244
+ }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/RootFolder?$Select=Name,ServerRelativeUrl`,
245
+ null, longLocalCache);
246
+
247
+ return SafeIfElse(() => result.result.d, null);
248
+ }
249
+
250
+ export function GetListField(siteUrlOrId: string, listIdOrTitle: string, fieldIdOrName: string, refreshCache?: boolean): Promise<IFieldInfo> {
251
+ let siteUrl = GetSiteUrl(siteUrlOrId);
252
+
253
+ var url = GetListRestUrl(siteUrl, listIdOrTitle) + `/fields`;
254
+
255
+ if (isValidGuid(fieldIdOrName)) {
256
+ url += `('${normalizeGuid(fieldIdOrName)}')`;
257
+ } else {
258
+ url += `/getbyinternalnameortitle(@u)?@u='${encodeURIComponent(fieldIdOrName)}'`;
259
+ }
260
+
261
+ let result = GetJson<{ d: IFieldInfo; }>(url, null, { allowCache: refreshCache !== true })
262
+ .then(r => {
263
+ return r.d;
264
+ })
265
+ .catch<IFieldInfo>(() => null);
266
+
267
+ return result;
268
+ }
269
+
270
+ function _getListFieldsRequestUrl(siteUrl: string, listIdOrTitle: string) {
271
+ return GetListRestUrl(siteUrl, listIdOrTitle) + `/fields`;
272
+ }
273
+
274
+ /** Gets ID, Title, ContentType Author, Editor, Created and Modified fields */
275
+ export function GetStandardListFields(siteUrlOrId: string, listIdOrTitle: string, refreshCache?: boolean) {
276
+ let fieldNames = ["ID", "Title", "ContentType", "Author", "Editor", "Created", "Modified"];
277
+ return GetListFields(siteUrlOrId, listIdOrTitle, { refreshCache: refreshCache, fieldNames: fieldNames });
278
+ }
279
+
280
+ export interface IGetListFieldsOptions {
281
+ refreshCache?: boolean;
282
+ /** fieldNames that should be returned with the request */
283
+ fieldNames?: string[];
284
+ }
285
+
286
+ function _processGetListFields(fields: IFieldInfo[], fieldNames: string[]) {
287
+ if (isNullOrEmptyArray(fields)) {
288
+ return fields as IFieldInfoEX[];
289
+ }
290
+ let extendedFields = extendFieldInfos(fields);
291
+
292
+ if (!isNullOrEmptyArray(fieldNames)) {
293
+ return extendedFields.filter((extendedField) => {
294
+ return fieldNames.includes(extendedField.InternalName);
295
+ });
296
+ }
297
+ return extendedFields;
298
+ }
299
+
300
+ export function GetListFields(siteUrlOrId: string, listIdOrTitle: string, options: IGetListFieldsOptions = {}): Promise<IFieldInfoEX[]> {
301
+ let siteUrl = GetSiteUrl(siteUrlOrId);
302
+ let url = _getListFieldsRequestUrl(siteUrl, listIdOrTitle);
303
+
304
+ let restOptions: IRestOptions = {
305
+ allowCache: options.refreshCache !== true,
306
+ jsonMetadata: jsonTypes.nometadata
307
+ };
308
+
309
+ return GetJson<{ value: IFieldInfo[]; }>(url, null, restOptions)
310
+ .then((result) => {
311
+ return _processGetListFields(result.value, options.fieldNames);
312
+ }).catch<IFieldInfoEX[]>(() => {
313
+ return null;
314
+ });
315
+ }
316
+
317
+ export function GetListFieldsSync(siteUrlOrId: string, listIdOrTitle: string, options: IGetListFieldsOptions = {}): IFieldInfoEX[] {
318
+ let siteUrl = GetSiteUrl(siteUrlOrId);
319
+ let url = _getListFieldsRequestUrl(siteUrl, listIdOrTitle);
320
+
321
+ let restOptions: IRestOptions = {
322
+ allowCache: options.refreshCache !== true,
323
+ jsonMetadata: jsonTypes.nometadata
324
+ };
325
+
326
+ let result = GetJsonSync<{ value: IFieldInfo[]; }>(url, null, restOptions);
327
+ if (result.success && !isNullOrUndefined(result.result)) {
328
+ return _processGetListFields(result.result.value, options.fieldNames);
329
+ }
330
+ return null;
331
+ }
332
+
333
+ export async function GetListFieldsAsHash(siteUrlOrId: string, listIdOrTitle: string, refreshCache?: boolean): Promise<IFieldInfoExHash> {
334
+ let siteUrl = GetSiteUrl(siteUrlOrId);
335
+
336
+ let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: refreshCache });
337
+ let hash: IFieldInfoExHash = {};
338
+ if (isNotEmptyArray(fields)) {
339
+ hash = toHash(fields, f => f.InternalName);
340
+ }
341
+ return hash;
342
+ }
343
+
344
+ export function GetListFieldsAsHashSync(siteUrlOrId: string, listIdOrTitle: string, refreshCache?: boolean): IFieldInfoExHash {
345
+ let siteUrl = GetSiteUrl(siteUrlOrId);
346
+
347
+ let fields = GetListFieldsSync(siteUrl, listIdOrTitle, { refreshCache: refreshCache });
348
+ let hash: IFieldInfoExHash = {};
349
+ if (isNotEmptyArray(fields)) {
350
+ fields.forEach(f => { hash[f.InternalName] = f; });
351
+ }
352
+ return hash;
353
+ }
354
+
355
+ export function GetListWorkflows(siteUrl: string, listIdOrTitle: string, refreshCache?: boolean): Promise<IListWorkflowAssociation[]> {
356
+ siteUrl = GetSiteUrl(siteUrl);
357
+
358
+ return GetJson<{
359
+ d: { results: IListWorkflowAssociation[]; };
360
+ }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/workflowAssociations`,
361
+ null, { allowCache: refreshCache !== true })
362
+ .then(r => {
363
+ if (r && r.d && isNotEmptyArray(r.d.results)) {
364
+ r.d.results.forEach(wf => {
365
+ wf.BaseId = normalizeGuid(wf.BaseId);
366
+ wf.Id = normalizeGuid(wf.Id);
367
+ wf.ListId = normalizeGuid(wf.ListId);
368
+ wf.WebId = normalizeGuid(wf.WebId);
369
+ });
370
+ return r.d.results;
371
+ }
372
+ else return [];
373
+ })
374
+ .catch<IListWorkflowAssociation[]>(() => []);
375
+ }
376
+
377
+ export function UserHasManagePermissions(siteUrl: string, listIdOrTitle: string): Promise<boolean> {
378
+ siteUrl = GetSiteUrl(siteUrl);
379
+
380
+ return GetJson<{ d: { EffectiveBasePermissions: { High: number; Low: number; }; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/EffectiveBasePermissions`, null,
381
+ { ...shortLocalCache })
382
+ .then(r => {
383
+ return new SPBasePermissions(r.d.EffectiveBasePermissions).has(SPBasePermissionKind.ManageLists);
384
+ })
385
+ .catch<boolean>(() => null);
386
+ }
387
+
388
+ export function UserHasEditPermissions(siteUrl: string, listIdOrTitle: string): Promise<boolean> {
389
+ return UserHasPermissions(siteUrl, listIdOrTitle, SPBasePermissionKind.EditListItems);
390
+ }
391
+
392
+ export function UserHasPermissions(siteUrlOrId: string, listIdOrTitle: string, permissionKind: SPBasePermissionKind): Promise<boolean> {
393
+ let siteUrl = GetSiteUrl(siteUrlOrId);
394
+
395
+ return GetJson<{ d: { EffectiveBasePermissions: { High: number; Low: number; }; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/EffectiveBasePermissions`, null,
396
+ { ...shortLocalCache })
397
+ .then(r => {
398
+ return new SPBasePermissions(r.d.EffectiveBasePermissions).has(permissionKind);
399
+ })
400
+ .catch<boolean>(() => null);
401
+ }
402
+
403
+ export function UserHasPermissionsSync(siteUrlOrId: string, listIdOrTitle: string, permissionKind: SPBasePermissionKind): boolean {
404
+ let siteUrl = GetSiteUrl(siteUrlOrId);
405
+
406
+ const res = GetJsonSync<{ d: { EffectiveBasePermissions: { High: number; Low: number; }; }; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/EffectiveBasePermissions`, null,
407
+ { ...shortLocalCache });
408
+
409
+ return new SPBasePermissions(res.result.d.EffectiveBasePermissions).has(permissionKind);
410
+ }
411
+
412
+ /** 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 */
413
+ export async function CreateField(siteUrl: string, listIdOrTitle: string, options: {
414
+ Title?: string;
415
+ Type?: FieldTypes;
416
+ Required?: boolean;
417
+ Indexed?: boolean;
418
+ SchemaXml?: string;
419
+ SchemaXmlSpecificInternalName?: boolean;
420
+ SkipAddToDefaultView?: boolean;
421
+ ClientSideComponentId?: string;
422
+ ClientSideComponentProperties?: string;
423
+ JSLink?: string;
424
+
425
+ }): Promise<IFieldInfoEX> {
426
+ siteUrl = GetSiteUrl(siteUrl);
427
+
428
+ let finish = async (result: IFieldInfo) => {
429
+ if (!result) {
430
+ return null;
431
+ }
432
+
433
+ let internalName = result.InternalName;
434
+ //we need to clear and reload the list fields cache, so call it and return our field from that collection.
435
+ let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
436
+
437
+ try {
438
+ if (options.SkipAddToDefaultView !== true) {
439
+ //try to add it to default view, don't wait for it
440
+ GetListViews(siteUrl, listIdOrTitle).then(views => {
441
+ let defaultView = firstOrNull(views, v => v.DefaultView);
442
+ if (defaultView)
443
+ GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/views('${defaultView.Id}')/ViewFields/addViewField('${internalName}')`, null, { method: "POST", spWebUrl: siteUrl });
444
+ });
445
+ }
446
+ }
447
+ catch (e) { }
448
+
449
+ return firstOrNull(fields, f => f.InternalName === internalName);
450
+ };
451
+
452
+ if (!isNullOrEmptyString(options.SchemaXml)) {
453
+ try {
454
+ let updateObject: IDictionary<any> = {
455
+ 'parameters': {
456
+ '__metadata': { 'type': 'SP.XmlSchemaFieldCreationInformation' },
457
+ 'SchemaXml': options.SchemaXml,
458
+ 'Options': options.SchemaXmlSpecificInternalName !== true ?
459
+ 4 ://SP.AddFieldOptions.addToAllContentTypes
460
+ 4 | 8//SP.AddFieldOptions.addToAllContentTypes | addFieldInternalNameHint
461
+ }
462
+ };
463
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/fields/createFieldAsXml`;
464
+ let newFieldResult = await GetJson<{ d: IFieldInfo; }>(url, JSON.stringify(updateObject));
465
+
466
+ if (!isNullOrUndefined(newFieldResult)
467
+ && !isNullOrUndefined(newFieldResult.d)) {
468
+ if ((!isNullOrEmptyString(options.Title) && options.Title !== newFieldResult.d.Title)
469
+ || (isBoolean(options.Indexed) && options.Indexed !== newFieldResult.d.Indexed)) {
470
+ let updatedField = await UpdateField(siteUrl, listIdOrTitle, newFieldResult.d.InternalName, {
471
+ Title: options.Title,
472
+ Indexed: options.Indexed === true
473
+ });
474
+ return finish(updatedField);
475
+ }
476
+ }
477
+
478
+ return finish(newFieldResult && newFieldResult.d);
479
+ } catch {
480
+ }
481
+ return null;
482
+ } else if (!isNullOrEmptyString(options.Title) && !isNullOrUndefined(options.Type)) {
483
+ let updateObject: IDictionary<any> = {
484
+ '__metadata': { 'type': 'SP.Field' },
485
+ 'Title': options.Title,
486
+ 'FieldTypeKind': options.Type,
487
+ 'Required': options.Required === true,
488
+ 'Indexed': options.Indexed === true
489
+ };
490
+ if (!isNullOrEmptyString(options.ClientSideComponentId)) {
491
+ updateObject.ClientSideComponentId = options.ClientSideComponentId;
492
+ }
493
+ if (!isNullOrEmptyString(options.ClientSideComponentProperties)) {
494
+ updateObject.ClientSideComponentProperties = options.ClientSideComponentProperties;
495
+ }
496
+ if (!isNullOrEmptyString(options.JSLink)) {
497
+ updateObject.JSLink = options.JSLink;
498
+ }
499
+
500
+ try {
501
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/fields`;
502
+ let newFieldResult = await GetJson<{ d: IFieldInfo; }>(url, JSON.stringify(updateObject));
503
+ return finish(newFieldResult && newFieldResult.d);
504
+ } catch {
505
+ }
506
+ return null;
507
+ }
508
+ else {
509
+ console.error("You must send either SchemaXml or Title and Type");
510
+ return null;
511
+ }
512
+ }
513
+ /** Update field SchemaXml OR Title, only 1 update at a time supported. */
514
+ export async function UpdateField(siteUrlOrId: string, listIdOrTitle: string, fieldInternalName: string, options: {
515
+ Title?: string;
516
+ Indexed?: boolean;
517
+ /** Update 'Choices' propertry on 'Choice' and 'MultiChoice' field types. */
518
+ Choices?: string[];
519
+ SchemaXml?: string;
520
+ FieldType?: FieldTypeAsString;
521
+ Required?: boolean;
522
+ Hidden?: boolean;
523
+ JSLink?: string;
524
+ ClientSideComponentId?: string;
525
+ ClientSideComponentProperties?: string;
526
+ }): Promise<IFieldInfoEX> {
527
+ let siteUrl = GetSiteUrl(siteUrlOrId);
528
+
529
+ let finish = async () => {
530
+ //we need to clear and reload the list fields cache, so call it and return our field from that collection.
531
+ let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
532
+ return firstOrNull(fields, f => f.InternalName === fieldInternalName);
533
+ };
534
+
535
+ let fields = await GetListFieldsAsHash(siteUrl, listIdOrTitle, true);
536
+ let thisField = fields[fieldInternalName];
537
+
538
+ //updates can either be SchemaXml, or others. Cannot be both.
539
+ let updates: IDictionary<any> = {
540
+ '__metadata': { 'type': 'SP.Field' }
541
+ };
542
+
543
+ if (!isNullOrEmptyString(options.SchemaXml)) {
544
+ updates.SchemaXml = options.SchemaXml;
545
+ }
546
+ else {
547
+ //cannot send schema updates with other updates.
548
+ if (!isNullOrEmptyString(options.Title)) {
549
+ updates.Title = options.Title;
550
+ }
551
+ if (!isNullOrEmptyString(options.FieldType)) {
552
+ updates.TypeAsString = options.FieldType;
553
+ }
554
+ if (isBoolean(options.Required)) {
555
+ updates.Required = options.Required === true;
556
+ }
557
+ if (isBoolean(options.Indexed)) {
558
+ updates.Indexed = options.Indexed === true;
559
+ }
560
+ if (!isNullOrEmptyArray(options.Choices)) {
561
+ let choiceType = options.FieldType || thisField.TypeAsString;
562
+ if (choiceType === "Choice" || choiceType === "MultiChoice") {
563
+ updates["__metadata"]["type"] = choiceType === "Choice" ? "SP.FieldChoice" : "SP.FieldMultiChoice"
564
+ updates.Choices = { "results": options.Choices };
565
+ } else {
566
+ logger.warn("Can only update 'Choices' property on 'Choice' and 'MultiChoice' field types.");
567
+ }
568
+ }
569
+ if (isBoolean(options.Hidden)) {
570
+ //this requries the CanToggleHidden to be in the schema... if not - we will need to add it before we can update this.
571
+ let fields = await GetListFieldsAsHash(siteUrl, listIdOrTitle, false);
572
+ let thisField = fields[fieldInternalName];
573
+ if (thisField.Hidden !== options.Hidden) {
574
+ if (thisField) {
575
+ if (thisField.SchemaJson.Attributes.CanToggleHidden !== "TRUE") {
576
+ await UpdateField(siteUrl, listIdOrTitle, fieldInternalName, {
577
+ SchemaXml:
578
+ thisField.SchemaXml.replace("<Field ", `<Field CanToggleHidden="TRUE" `)
579
+ });
580
+ }
581
+ }
582
+ updates.Hidden = options.Hidden === true;
583
+ }
584
+ }
585
+
586
+ if (!isNullOrEmptyString(options.ClientSideComponentId))
587
+ updates.ClientSideComponentId = options.ClientSideComponentId;
588
+ if (!isNullOrEmptyString(options.ClientSideComponentProperties))
589
+ updates.ClientSideComponentProperties = options.ClientSideComponentProperties;
590
+ if (!isNullOrEmptyString(options.JSLink))
591
+ updates.JSLink = options.JSLink;
592
+ }
593
+
594
+ if (Object.keys(updates).length > 1) {
595
+ return GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/fields/getbyinternalnameortitle('${fieldInternalName}')`,
596
+ JSON.stringify(updates), { xHttpMethod: "MERGE" })
597
+ .then(r => {
598
+ return finish();
599
+ })
600
+ .catch<IFieldInfoEX>(() => null);
601
+ }
602
+ else {
603
+ console.error("You must send an option to update");
604
+ return null;
605
+ }
606
+ }
607
+
608
+ export async function DeleteField(siteUrl: string, listIdOrTitle: string, fieldInternalName: string, options?: { DeleteHiddenField?: boolean; }): Promise<boolean> {
609
+ siteUrl = GetSiteUrl(siteUrl);
610
+
611
+ // let finish = async () => {
612
+ // //we need to clear and reload the list fields cache, so call it and return our field from that collection.
613
+ // let fields = await GetListFields(siteUrl, listIdOrTitle, { refreshCache: true });
614
+ // return firstOrNull(fields, f => f.InternalName === fieldInternalName);
615
+ // };
616
+
617
+ if (options && options.DeleteHiddenField)
618
+ await UpdateField(siteUrl, listIdOrTitle, fieldInternalName, { Hidden: false });
619
+
620
+
621
+ return GetJson(GetListRestUrl(siteUrl, listIdOrTitle) + `/fields/getbyinternalnameortitle('${fieldInternalName}')`, null, {
622
+ method: "POST",
623
+ xHttpMethod: "DELETE"
624
+ })
625
+ .then(r => true)
626
+ .catch<boolean>((e) => false);
627
+ }
628
+
629
+ export interface IListViewOptions { includeViewFields?: boolean; }
630
+ export function GetListViews(siteUrl: string, listIdOrTitle: string, options?: IListViewOptions): Promise<iListView[]> {
631
+ siteUrl = GetSiteUrl(siteUrl);
632
+
633
+ return GetJson<{
634
+ value: iListView[];
635
+ }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/views?$select=Title,Id,ServerRelativeUrl,RowLimit,Paged,ViewQuery,ListViewXml,PersonalView,MobileView,MobileDefaultView,Hidden,DefaultView,ReadOnlyView${options && options.includeViewFields ? "&$expand=ViewFields" : ""}`,
636
+ null, { allowCache: true, jsonMetadata: jsonTypes.nometadata })
637
+ .then(r => {
638
+ let views = r.value;
639
+ if (isNotEmptyArray(views)) {
640
+ views.forEach(v => {
641
+ v.Id = normalizeGuid(v.Id);
642
+ if (options && options.includeViewFields) {
643
+ v.ViewFields = v.ViewFields && v.ViewFields["Items"] && v.ViewFields["Items"] || [];
644
+ }
645
+ });
646
+ }
647
+ return views;
648
+ })
649
+ .catch<iListView[]>(() => null);
650
+ }
651
+
652
+ export function GetListViewsSync(siteUrl: string, listIdOrTitle: string): iListView[] {
653
+ siteUrl = GetSiteUrl(siteUrl);
654
+
655
+ let result = GetJsonSync<{
656
+ d: {
657
+ results: iListView[];
658
+ };
659
+ }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/views`,
660
+ null, { allowCache: true });
661
+ if (result.success) {
662
+ let views = result && result.result && result.result.d && result.result.d.results;
663
+ if (isNotEmptyArray(views)) {
664
+ views.forEach(v => { v.Id = normalizeGuid(v.Id); });
665
+ }
666
+ return views;
667
+ }
668
+ return null;
669
+ }
670
+
671
+ export async function AddViewFieldToListView(siteUrl: string, listIdOrTitle: string, viewId: string, viewField: string) {
672
+ return _addOrRemoveViewField(siteUrl, listIdOrTitle, viewId, viewField, "addviewfield");
673
+ }
674
+
675
+ export async function RemoveViewFieldFromListView(siteUrl: string, listIdOrTitle: string, viewId: string, viewField: string) {
676
+ return _addOrRemoveViewField(siteUrl, listIdOrTitle, viewId, viewField, "removeviewfield");
677
+ }
678
+
679
+ async function _addOrRemoveViewField(siteUrl: string, listIdOrTitle: string, viewId: string, viewField: string, action: "addviewfield" | "removeviewfield") {
680
+ siteUrl = GetSiteUrl(siteUrl);
681
+
682
+ if (isNullOrEmptyString(viewField) || !isValidGuid(viewId)) {
683
+ return false;
684
+ }
685
+
686
+ let views = await GetListViews(siteUrl, listIdOrTitle, { includeViewFields: true });
687
+
688
+ if (isNullOrEmptyArray(views)) {
689
+ return false;
690
+ }
691
+
692
+ let view = views.filter((view) => {
693
+ return normalizeGuid(view.Id) === normalizeGuid(viewId);
694
+ })[0];
695
+
696
+ if (isNullOrUndefined(view)) {
697
+ return false;
698
+ }
699
+
700
+ let hasField = view.ViewFields.includes(viewField);
701
+
702
+ if (action === "addviewfield" && hasField === true) {
703
+ return true;
704
+ }
705
+
706
+ if (action === "removeviewfield" && hasField === false) {
707
+ return true;
708
+ }
709
+
710
+ try {
711
+ let url = GetListRestUrl(siteUrl, listIdOrTitle) + `/views('${normalizeGuid(view.Id)}')/viewfields/${action}('${viewField}')`;
712
+
713
+ let result = await GetJson<{ "odata.null": boolean; }>(url, null, { method: "POST" });
714
+
715
+ if (result && result["odata.null"] === true) {
716
+ return true;
717
+ }
718
+ } catch { }
719
+
720
+ return false;
721
+ }
722
+
723
+ export function GetListContentTypes(siteUrl: string, listIdOrTitle: string,
724
+ options?: Omit<IGetContentTypesOptions, "listIdOrTitle" | "fromRooWeb">, refreshCache = false): Promise<iContentType[]> {
725
+ return GetContentTypes(siteUrl, { ...(options || {}), listIdOrTitle: listIdOrTitle }, refreshCache);
726
+ }
727
+
728
+ export function GetListContentTypesSync(siteUrl: string, listIdOrTitle: string,
729
+ options?: Omit<IGetContentTypesOptions, "listIdOrTitle" | "fromRooWeb">, refreshCache = false): iContentType[] {
730
+ return GetContentTypesSync(siteUrl, { ...(options || {}), listIdOrTitle: listIdOrTitle }, refreshCache);
731
+ }
732
+
733
+ /** generic version. for the KWIZ forms version that supports action id call GetListFormUrlAppsWeb instead */
734
+ export function GetListFormUrl(siteUrl: string, listId: string, pageType: PageType, params?: { contentTypeId?: string; itemId?: number | string; rootFolder?: string }) {
735
+ siteUrl = GetSiteUrl(siteUrl);
736
+
737
+ if (!isValidGuid(listId)) console.error('GetListFormUrl requires a list id');
738
+ let url = `${normalizeUrl(siteUrl)}/_layouts/15/listform.aspx?PageType=${pageType}&ListId=${encodeURIComponent(listId)}`;
739
+ if (params) {
740
+ if (!isNullOrEmptyString(params.contentTypeId))
741
+ url += `&ContentTypeId=${encodeURIComponent(params.contentTypeId)}`;
742
+ if (!isNullOrEmptyString(params.itemId))
743
+ url += `&ID=${encodeURIComponent(params.itemId as string)}`;
744
+ if (!isNullOrEmptyString(params.rootFolder))
745
+ url += `&RootFolder=${encodeURIComponent(params.rootFolder)}`;
746
+ }
747
+ return url;
748
+ }
749
+
750
+ export function GetFieldSchemaSync(siteUrl: string, listIdOrTitle: string, fieldInternalName: string, refreshCache?: boolean): IFieldJsonSchema {
751
+ siteUrl = GetSiteUrl(siteUrl);
752
+
753
+ //ISSUE: 1516 - The get schema request will fail if the field doesn't exist in the list, so we load the fields and ensure the field
754
+ //exists before requesting the schema.
755
+ let fields = GetListFieldsSync(siteUrl, listIdOrTitle, {
756
+ refreshCache: refreshCache,
757
+ fieldNames: [fieldInternalName]
758
+ });
759
+
760
+ if (isNullOrEmptyArray(fields)) {
761
+ return null;
762
+ }
763
+
764
+ let field = fields[0];
765
+ return SchemaXmlToJson(field.SchemaXml);
766
+ // let url = GetListRestUrl(siteUrl, listIdOrTitle) + `/fields/getByInternalNameOrTitle('${fieldInternalName}')?$select=SchemaXml`;
767
+ // let result = GetJsonSync<{ d: { SchemaXml: string; }; }>(
768
+ // url,
769
+ // null,
770
+ // {
771
+ // ...shortLocalCache,
772
+ // forceCacheUpdate: refreshCache === true
773
+ // });
774
+
775
+ // if (result && result.success) {
776
+ // return SchemaXmlToJson(result.result.d.SchemaXml);
777
+ // }
778
+ // return null;
779
+ //#endregion
780
+ }
781
+
782
+ export async function GetFieldSchema(siteUrl: string, listIdOrTitle: string, fieldInternalName: string, refreshCache?: boolean) {
783
+ siteUrl = GetSiteUrl(siteUrl);
784
+
785
+ //ISSUE: 1516 - The get schema request will fail if the field doesn't exist in the list, so we load the fields and ensure the field
786
+ //exists before requesting the schema
787
+ let fields = await GetListFields(siteUrl, listIdOrTitle, {
788
+ refreshCache: refreshCache,
789
+ fieldNames: [fieldInternalName]
790
+ });
791
+
792
+ if (isNullOrEmptyArray(fields)) {
793
+ return null;
794
+ }
795
+
796
+ let field = fields[0];
797
+ return SchemaXmlToJson(field.SchemaXml);
798
+ }
799
+
800
+ export async function GetListItems(siteUrl: string, listIdOrTitle: string, options: {
801
+ /** Optional, default: 1000. 0: get all items. */
802
+ rowLimit?: number;
803
+ /** Id, Title, Modified, FileLeafRef, FileDirRef, FileRef, FileSystemObjectType */
804
+ columns: (string | IFieldInfoEX)[];
805
+ foldersBehaviour?: GeListItemsFoldersBehaviour;
806
+ /** Optional, request to expand some columns. */
807
+ expand?: string[];
808
+ /** allow to change the jsonMetadata for this request, default: verbose */
809
+ jsonMetadata?: jsonTypes;
810
+ refreshCache?: boolean;
811
+ /** allow to send a filter statement */
812
+ $filter?: string;
813
+ }): Promise<IRestItem[]> {
814
+ let info = _GetListItemsInfo(siteUrl, listIdOrTitle, options);
815
+
816
+ let items: IRestItem[] = [];
817
+
818
+ do {
819
+ let resultItems: IRestItem[] = [];
820
+ let next: string = null;
821
+ if (info.noMetadata) {
822
+ let requestResult = (await GetJson<{
823
+ value: IRestItem[];
824
+ "odata.nextLink": string;
825
+ }>(info.requestUrl, null, {
826
+ allowCache: options.refreshCache !== true,
827
+ jsonMetadata: options.jsonMetadata
828
+ }));
829
+ resultItems = requestResult.value;
830
+ next = requestResult["odata.nextLink"];
831
+ }
832
+ else {
833
+ let requestResult = (await GetJson<{
834
+ d: {
835
+ results: IRestItem[];
836
+ __next?: string;
837
+ };
838
+ }>(info.requestUrl, null, {
839
+ allowCache: options.refreshCache !== true
840
+ }));
841
+ resultItems = requestResult.d.results;
842
+ next = requestResult.d.__next;
843
+ }
844
+
845
+ if (isNotEmptyArray(resultItems))
846
+ items.push(...resultItems);
847
+
848
+ if (info.totalNumberOfItemsToGet > items.length)
849
+ info.requestUrl = next;
850
+ else
851
+ info.requestUrl = null;
852
+
853
+ } while (!isNullOrEmptyString(info.requestUrl));
854
+
855
+ return __fixGetListItemsResults(siteUrl, listIdOrTitle, items, options.foldersBehaviour, info.expandedLookupFields);
856
+ }
857
+
858
+ export function GetListItemsSync(siteUrl: string, listIdOrTitle: string, options: {
859
+ /** Optional, default: 1000. 0: get all items. */
860
+ rowLimit?: number;
861
+ /** Id, Title, Modified, FileLeafRef, FileDirRef, FileRef, FileSystemObjectType */
862
+ columns: (string | IFieldInfoEX)[];
863
+ foldersBehaviour?: GeListItemsFoldersBehaviour;
864
+ /** Optional, request to expand some columns. */
865
+ expand?: string[];
866
+ /** allow to send a filter statement */
867
+ $filter?: string;
868
+ }): IRestItem[] {
869
+ let info = _GetListItemsInfo(siteUrl, listIdOrTitle, options);
870
+
871
+ let items: IRestItem[] = [];
872
+
873
+ do {
874
+ let resultItems: IRestItem[] = [];
875
+ let next: string = null;
876
+ if (info.noMetadata) {
877
+ let requestResult = GetJsonSync<{
878
+ value: IRestItem[];
879
+ "odata.nextLink": string;
880
+ }>(info.requestUrl, null, { allowCache: true });
881
+ if (requestResult.success) {
882
+ resultItems = requestResult.result.value;
883
+ next = requestResult.result["odata.nextLink"];
884
+ }
885
+ }
886
+ else {
887
+ let requestResult = GetJsonSync<{
888
+ d: { results: IRestItem[]; __next?: string; };
889
+ }>(info.requestUrl, null, { allowCache: true });
890
+ if (requestResult.success) {
891
+ resultItems = requestResult.result.d.results;
892
+ next = requestResult.result.d.__next;
893
+ }
894
+ }
895
+
896
+ if (isNotEmptyArray(resultItems))
897
+ items.push(...resultItems);
898
+
899
+ if (info.totalNumberOfItemsToGet > items.length)
900
+ info.requestUrl = next;
901
+ else
902
+ info.requestUrl = null;
903
+
904
+ } while (!isNullOrEmptyString(info.requestUrl));
905
+
906
+ return __fixGetListItemsResults(siteUrl, listIdOrTitle, items, options.foldersBehaviour, info.expandedLookupFields);
907
+ }
908
+
909
+ function _GetListItemsInfo(siteUrl: string, listIdOrTitle: string, options: {
910
+ /** Optional, default: 1000. 0: get all items. */
911
+ rowLimit?: number;
912
+ /** Id, Title, Modified, FileLeafRef, FileDirRef, FileRef, FileSystemObjectType */
913
+ columns: (string | IFieldInfoEX)[];
914
+ /** Optional, request to expand some columns. */
915
+ expand?: string[];
916
+ /** allow to change the jsonMetadata for this request, default: verbose */
917
+ jsonMetadata?: jsonTypes;
918
+ /** allow to send a filter statement */
919
+ $filter?: string;
920
+ }) {
921
+ siteUrl = GetSiteUrl(siteUrl);
922
+
923
+ let url = GetListRestUrl(siteUrl, listIdOrTitle) + `/items`;
924
+ let queryParams: string[] = [];
925
+
926
+ //Issue 8189 expand lookup fields
927
+ let columns: string[] = [];
928
+ let expand: string[] = [];
929
+ let expandedLookupFields: IFieldInfoEX[] = [];
930
+ options.columns.forEach(c => {
931
+ if (isString(c)) columns.push(c);
932
+ else {
933
+ let internalName = c.InternalName;
934
+ //Issue 828, 336
935
+ if (internalName.startsWith("_")) internalName = `OData_${internalName}`;
936
+
937
+ let isLookupField = c.TypeAsString === "Lookup" || c.TypeAsString === "LookupMulti";
938
+ let isUserField = c.TypeAsString === "User" || c.TypeAsString === "UserMulti";
939
+
940
+ if (isLookupField || isUserField) {
941
+ //ISSUE: 1519 - Added lookupField property to able to retrieve value of the additional lookup field key
942
+ let lookupField = (c as IFieldLookupInfo).LookupField;
943
+ if (!isNullOrEmptyString(lookupField) && isLookupField) {
944
+ columns.push(`${internalName}/${lookupField}`);
945
+ }
946
+ //we want to expand it
947
+ columns.push(`${internalName}/Title`);
948
+ columns.push(`${internalName}/Id`);
949
+ expand.push(internalName);
950
+ expandedLookupFields.push(c);
951
+ }
952
+ else columns.push(internalName);
953
+ }
954
+ });
955
+ if (isNotEmptyArray(options.expand)) {
956
+ expand.push(...options.expand);
957
+ }
958
+
959
+ //add the ones we need
960
+ PushNoDuplicate(columns, "Id");
961
+ PushNoDuplicate(columns, "FileRef");
962
+ PushNoDuplicate(columns, "FileSystemObjectType");
963
+
964
+ queryParams.push(`$select=${encodeURIComponent(makeUniqueArray(columns).join(','))}`);
965
+
966
+ if (isNotEmptyArray(expand))
967
+ queryParams.push(`$expand=${encodeURIComponent(makeUniqueArray(expand).join(','))}`);
968
+
969
+ let batchSize = 2000;
970
+ let limit = options.rowLimit >= 0 && options.rowLimit < batchSize ? options.rowLimit : batchSize;
971
+ let totalNumberOfItemsToGet = !isNumber(options.rowLimit) || options.rowLimit < 1 ? 99999 : options.rowLimit > batchSize ? options.rowLimit : limit;
972
+
973
+ if (!isNullOrEmptyString(options.$filter))
974
+ queryParams.push(`$filter=${options.$filter}`);
975
+ queryParams.push(`$top=${limit}`);
976
+
977
+ let requestUrl = url + (queryParams.length > 0 ? '?' + queryParams.join('&') : '');
978
+ let noMetadata = options.jsonMetadata === jsonTypes.nometadata;
979
+
980
+ return { requestUrl, noMetadata, totalNumberOfItemsToGet, expandedLookupFields };
981
+ }
982
+
983
+ /** Find an item by id, even if it is nested in a sub-folder */
984
+ export function FindListItemById(items: IRestItem[], itemId: number): IRestItem {
985
+ for (let i = 0; i < items.length; i++) {
986
+ let current = items[i];
987
+ if (current.Id === itemId) return current;
988
+ else if (isNotEmptyArray(current.__Items))//folder? look inside
989
+ {
990
+ let nestedResult = FindListItemById(current.__Items, itemId);
991
+ if (!isNullOrUndefined(nestedResult)) return nestedResult;
992
+ }
993
+ }
994
+ //not found
995
+ return null;
996
+ }
997
+
998
+ function _getListEventReceiversRequestUrl(siteUrl: string, listIdOrTitle: string) {
999
+ return GetListRestUrl(siteUrl, listIdOrTitle) + `/EventReceivers`
1000
+ }
1001
+
1002
+ export async function GetListEventReceivers(siteUrl: string, listIdOrTitle: string, refreshCache?: boolean): Promise<ISPEventReceiver[]> {
1003
+ try {
1004
+ let url = _getListEventReceiversRequestUrl(siteUrl, listIdOrTitle);
1005
+ let response = await GetJson<{
1006
+ value: ISPEventReceiver[];
1007
+ }>(url,
1008
+ null, {
1009
+ allowCache: refreshCache !== true,
1010
+ jsonMetadata: jsonTypes.nometadata
1011
+ });
1012
+
1013
+ return !isNullOrUndefined(response) ? response.value : null;
1014
+ } catch {
1015
+ }
1016
+
1017
+ return null;
1018
+ }
1019
+
1020
+ export async function AddListEventReceiver(siteUrl: string, listIdOrTitle: string, eventReceiverDefinition: Pick<ISPEventReceiver, "EventType" | "ReceiverName" | "ReceiverUrl" | "SequenceNumber">): Promise<ISPEventReceiver> {
1021
+ let newEventReceiver: Omit<ISPEventReceiver, "ReceiverId" | "Synchronization"> = {
1022
+ ReceiverAssembly: "",
1023
+ ReceiverClass: "",
1024
+ ...eventReceiverDefinition
1025
+ };
1026
+
1027
+ try {
1028
+ let url = _getListEventReceiversRequestUrl(siteUrl, listIdOrTitle);
1029
+ let response = await GetJson<ISPEventReceiver>(url, JSON.stringify(newEventReceiver), {
1030
+ method: "POST",
1031
+ includeDigestInPost: true,
1032
+ jsonMetadata: jsonTypes.nometadata,
1033
+ headers: {
1034
+ "content-type": contentTypes.json
1035
+ }
1036
+ });
1037
+
1038
+ return !isNullOrUndefined(response) && isValidGuid(response.ReceiverId) ? response : null;
1039
+ } catch {
1040
+ }
1041
+
1042
+ return null;
1043
+ }
1044
+
1045
+ export async function DeleteListEventReceiver(siteUrl: string, listIdOrTitle: string, eventReceiverId: string): Promise<boolean> {
1046
+ try {
1047
+ let url = `${_getListEventReceiversRequestUrl(siteUrl, listIdOrTitle)}('${normalizeGuid(eventReceiverId)}')/deleteObject`;
1048
+ let response = await GetJson<{ "odata.null": boolean }>(url, null, {
1049
+ method: "POST",
1050
+ includeDigestInPost: true,
1051
+ jsonMetadata: jsonTypes.nometadata
1052
+ });
1053
+
1054
+ return !isNullOrUndefined(response) && response["odata.null"] === true;
1055
+ } catch {
1056
+ }
1057
+
1058
+ return false;
1059
+ }
1060
+
1061
+ /** timestamp of changes:
1062
+ * - item updates
1063
+ * - changes to columns
1064
+ * - content types
1065
+ * - list versioning settings
1066
+ * - list title/description
1067
+ * - content approval settings
1068
+ * does not track:
1069
+ * - Changes to views
1070
+ * - changing list/items permissions
1071
+ */
1072
+ export function GetListLastItemModifiedDate(siteUrl: string, listIdOrTitle: string, options: {
1073
+ sync: true;
1074
+ refreshCache?: boolean;
1075
+ /** ignore system changes */
1076
+ userChangesOnly?: boolean;
1077
+ }): string;
1078
+ /** timestamp of changes:
1079
+ * - item updates
1080
+ * - changes to columns
1081
+ * - content types
1082
+ * - list versioning settings
1083
+ * - list title/description
1084
+ * - content approval settings
1085
+ * does not track:
1086
+ * - Changes to views
1087
+ * - changing list/items permissions
1088
+ */
1089
+ export function GetListLastItemModifiedDate(siteUrl: string, listIdOrTitle: string, options?: {
1090
+ sync?: false;
1091
+ refreshCache?: boolean;
1092
+ /** ignore system changes */
1093
+ userChangesOnly?: boolean;
1094
+ }): Promise<string>;
1095
+
1096
+ export function GetListLastItemModifiedDate(siteUrl: string, listIdOrTitle: string, options?: {
1097
+ sync?: boolean;
1098
+ refreshCache?: boolean;
1099
+ /** ignore system changes */
1100
+ userChangesOnly?: boolean;
1101
+ }): string | Promise<string> {
1102
+ siteUrl = GetSiteUrl(siteUrl);
1103
+
1104
+ let endPoint = options && options.userChangesOnly ? 'LastItemUserModifiedDate' : 'LastItemModifiedDate';
1105
+
1106
+ let sync = options && options.sync ? true : false;
1107
+ let caller = sync ? GetJsonSync : GetJson;
1108
+
1109
+ let result = caller<{ value: string; }>(GetListRestUrl(siteUrl, listIdOrTitle) + `/${endPoint}`, null, {
1110
+ allowCache: true,//in memory only
1111
+ jsonMetadata: jsonTypes.nometadata,
1112
+ forceCacheUpdate: options && options.refreshCache === true || false
1113
+ });
1114
+
1115
+ if (isPromise(result))
1116
+ return result.then(r => r.value, () => null);
1117
+ else
1118
+ return result.success ? result.result.value : null;
1119
+ }
1120
+
1121
+ export async function ReloadListLastModified(siteUrl: string, listIdOrTitle: string) {
1122
+ await GetListLastItemModifiedDate(siteUrl, listIdOrTitle, { refreshCache: true });
1123
+ //make sure we do it for both title and id, we don't know how the other callers may use this in their API
1124
+
1125
+ if (!isValidGuid(listIdOrTitle)) {
1126
+ try {
1127
+ var listId = GetListId(siteUrl, listIdOrTitle);
1128
+ await GetListLastItemModifiedDate(siteUrl, listId, { refreshCache: true });
1129
+ } catch (e) { }
1130
+ }
1131
+ else {
1132
+ try {
1133
+ var listTitle = await GetListTitle(siteUrl, listIdOrTitle);
1134
+ await GetListLastItemModifiedDate(siteUrl, listTitle, { refreshCache: true });
1135
+ } catch (e) { }
1136
+ }
1137
+ }
1138
+
1139
+ export async function ListHasUniquePermissions(siteUrl: string, listIdOrTitle: string): Promise<boolean> {
1140
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/?$select=hasuniqueroleassignments`;
1141
+ let has = await GetJson<{ HasUniqueRoleAssignments: boolean }>(url, undefined, { allowCache: false, jsonMetadata: jsonTypes.nometadata });
1142
+ return has.HasUniqueRoleAssignments === true;
1143
+ }
1144
+ export async function RestoreListPermissionInheritance(siteUrl: string, listIdOrTitle: string): Promise<void> {
1145
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/ResetRoleInheritance`;
1146
+ await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1147
+ }
1148
+ export async function BreakListPermissionInheritance(siteUrl: string, listIdOrTitle: string, clear = true): Promise<void> {
1149
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/breakroleinheritance(copyRoleAssignments=${clear ? 'false' : 'true'}, clearSubscopes=true)`;
1150
+ await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1151
+ }
1152
+ export async function AssignListPermission(siteUrl: string, listIdOrTitle: string, principalId: number, roleId: number) {
1153
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/roleassignments/addroleassignment(principalid=${principalId},roleDefId=${roleId})`;
1154
+ await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1155
+ }
1156
+ export async function RemoveListPermission(siteUrl: string, listIdOrTitle: string, principalId: number, roleId: number) {
1157
+ let url = `${GetListRestUrl(siteUrl, listIdOrTitle)}/roleassignments/removeroleassignment(principalid=${principalId},roleDefId=${roleId})`;
1158
+ await GetJson(url, undefined, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata, spWebUrl: siteUrl });
1159
+ }
1160
+
1161
+ interface iCreateListResult {
1162
+ AllowContentTypes: boolean,
1163
+ BaseTemplate: ListTemplateTypes,
1164
+ BaseType: BaseTypes,
1165
+ ContentTypesEnabled: boolean,
1166
+ Created: string,
1167
+ DefaultItemOpenUseListSetting: boolean,
1168
+ Description: string,
1169
+ DisableCommenting: boolean,
1170
+ DisableGridEditing: boolean,
1171
+ DocumentTemplateUrl: string,//"/sites/s/cms/CMSLayouts/Forms/template.dotx",
1172
+ DraftVersionVisibility: 0,
1173
+ EnableAttachments: boolean,
1174
+ EnableFolderCreation: boolean,
1175
+ EnableMinorVersions: boolean,
1176
+ EnableModeration: false,
1177
+ EnableRequestSignOff: boolean,
1178
+ EnableVersioning: boolean,
1179
+ EntityTypeName: string,//"CMSLayouts",
1180
+ ExemptFromBlockDownloadOfNonViewableFiles: boolean,
1181
+ FileSavePostProcessingEnabled: boolean,
1182
+ ForceCheckout: boolean,
1183
+ HasExternalDataSource: boolean,
1184
+ Hidden: boolean,
1185
+ Id: string,//"c21d4eb4-70cc-4c95-925a-aa34bb9e01e0",
1186
+ ImagePath: {
1187
+ DecodedUrl: string,//"/_layouts/15/images/itdl.png?rev=47"
1188
+ },
1189
+ ImageUrl: string,//"/_layouts/15/images/itdl.png?rev=47",
1190
+ IsApplicationList: boolean,
1191
+ IsCatalog: boolean,
1192
+ IsPrivate: boolean,
1193
+ ItemCount: 0,
1194
+ LastItemDeletedDate: string,//"2024-02-05T18:26:05Z",
1195
+ LastItemModifiedDate: string,//"2024-02-05T18:26:06Z",
1196
+ LastItemUserModifiedDate: string,//"2024-02-05T18:26:05Z",
1197
+ ListExperienceOptions: ListExperienceOptions,
1198
+ ListItemEntityTypeFullName: string,//"SP.Data.CMSLayoutsItem",
1199
+ MajorVersionLimit: number,//500,
1200
+ MajorWithMinorVersionsLimit: number,//0,
1201
+ MultipleDataList: boolean,
1202
+ NoCrawl: boolean,
1203
+ ParentWebPath: {
1204
+ DecodedUrl: string,//"/sites/s/cms"
1205
+ },
1206
+ ParentWebUrl: string,//"/sites/s/cms",
1207
+ ParserDisabled: boolean,
1208
+ ServerTemplateCanCreateFolders: boolean,
1209
+ TemplateFeatureId: string,//"00bfea71-e717-4e80-aa17-d0c71b360101",
1210
+ Title: string,//"CMSLayouts"
1211
+ }
1212
+ export async function CreateList(siteUrl: string, info: {
1213
+ title: string; description: string;
1214
+ type: BaseTypes; template: ListTemplateTypes;
1215
+ }): Promise<iCreateListResult> {
1216
+ let url = `${GetRestBaseUrl(siteUrl)}/web/lists`;
1217
+ const body = {
1218
+ __metadata: { type: 'SP.List' },
1219
+ AllowContentTypes: false,
1220
+ ContentTypesEnabled: false,
1221
+ BaseTemplate: info.template,
1222
+ BaseType: info.type,
1223
+ Description: info.description,
1224
+ Title: info.title
1225
+ };
1226
+
1227
+ let newList = (await GetJson<{ d: iCreateListResult }>(url, jsonStringify(body))).d;
1228
+ normalizeGuid(newList.Id);
1229
+ return newList;
1230
+ }
1231
+
1232
+ export async function SearchList(siteUrl: string, listIdOrTitle: string, query: string) {
1233
+ let listId = GetListId(siteUrl, listIdOrTitle);
1234
+ let url = `${GetRestBaseUrl(siteUrl)}/search/query?querytext='(${query}*)'&querytemplate='{searchTerms} (NormListID:${listId})'`;
1235
+
1236
+ try {
1237
+ const result = await GetJson<{
1238
+ ElapsedTime: number,
1239
+ PrimaryQueryResult: {
1240
+ CustomResults: [];
1241
+ QueryId: string;//"7fdf01b1-f6f0-4d42-b046-d9db22597084",
1242
+ QueryRuleId: string;// "00000000-0000-0000-0000-000000000000",
1243
+ RefinementResults: null,
1244
+ RelevantResults: {
1245
+ RowCount: number,
1246
+ Table: {
1247
+ Rows: {
1248
+ Cells: {
1249
+ Key:
1250
+ /** "1989637621861439888" "Edm.Int64" */
1251
+ "WorkId"
1252
+ /** "1000.1073372","Edm.Double" */
1253
+ | "Rank"
1254
+ /** "sample md as text","Edm.String" */
1255
+ | "Title"
1256
+ /** "Shai Petel", "Edm.String" */
1257
+ | "Author"
1258
+ /** "91", "Edm.Int64" */
1259
+ | "Size"
1260
+ /** "https://kwizcom.sharepoint.com/sites/s/cms/CMSPages/sample md as text.txt", "Edm.String" */
1261
+ | "Path"
1262
+ /** null, "Null" */
1263
+ | "Description"
1264
+ /** "# hello world! - bullet - bullet | table | col | | ----- | ---- | |table |col | ", "Edm.String" */
1265
+ | "HitHighlightedSummary"
1266
+ /** "https://kwizcom.sharepoint.com/_api/v2.1/drives/b!8NAeO-mocUWbgyMTqcM0Mfh8XKPhn7xOhhMrO5KfJjBs_gXb9j8ZRaLxuppgj0Uk/items/01OBXW4FLU6G4LIT7AX5BK3MXHDICVTIOT/thumbnails/0/c400x99999/content?prefer=noRedirect", "Edm.String" */
1267
+ | "PictureThumbnailURL"
1268
+ /** null,"Null" */
1269
+ | "ServerRedirectedURL"
1270
+ /** null,"Null" */
1271
+ | "ServerRedirectedEmbedURL"
1272
+ /** null,"Null" */
1273
+ | "ServerRedirectedPreviewURL"
1274
+ /** "txt","Edm.String" */
1275
+ | "FileExtension"
1276
+ /** "0x010100CB212272F1372446A2423F0A2BEA12B8", "Edm.String" */
1277
+ | "ContentTypeId"
1278
+ /** "https://kwizcom.sharepoint.com/sites/s/cms/CMSPages/Forms/AllItems.aspx","Edm.String" */
1279
+ | "ParentLink"
1280
+ /** "1","Edm.Int64" */
1281
+ | "ViewsLifeTime"
1282
+ /** "1","Edm.Int64" */
1283
+ | "ViewsRecent"
1284
+ /** "2024-02-22T18:35:48.0000000Z","Edm.DateTime" */
1285
+ | "LastModifiedTime"
1286
+ /** "txt","Edm.String" */
1287
+ | "FileType"
1288
+ /** "1989637621861439888","Edm.Int64" */
1289
+ | "DocId"
1290
+ /** "https://kwizcom.sharepoint.com/sites/s/cms","Edm.String" */
1291
+ | "SPWebUrl"
1292
+ /** "{b4b8f174-e04f-42bf-adb2-e71a0559a1d3}","Edm.String" */
1293
+ | "UniqueId"
1294
+ /** "3b1ed0f0-a8e9-4571-9b83-2313a9c33431","Edm.String" */
1295
+ | "SiteId"
1296
+ /** "a35c7cf8-9fe1-4ebc-8613-2b3b929f2630","Edm.String" */
1297
+ | "WebId"
1298
+ /** "db05fe6c-3ff6-4519-a2f1-ba9a608f4524","Edm.String" */
1299
+ | "ListId"
1300
+ /** "https://kwizcom.sharepoint.com/sites/s/cms/CMSPages/sample md as text.txt","Edm.String" */
1301
+ | "OriginalPath"
1302
+ ;
1303
+ Value: string,
1304
+ ValueType: "Edm.Int64" | "Edm.Double" | "Edm.String" | "Edm.DateTime" | "Null"
1305
+ }[]
1306
+ }[]
1307
+ },
1308
+ TotalRows: number,
1309
+ TotalRowsIncludingDuplicates: number
1310
+ }
1311
+ },
1312
+ }>(url, null, { jsonMetadata: jsonTypes.nometadata });
1313
+ logger.json(result.PrimaryQueryResult.RelevantResults, `search ${query}`);
1314
+ let rows = result.PrimaryQueryResult.RelevantResults.Table.Rows;
1315
+
1316
+ const mapped: (IDictionary<string | Date | number> & {
1317
+ WorkId?: number;
1318
+ Rank?: number;
1319
+ Title?: string;
1320
+ Author?: string;
1321
+ Size?: number;
1322
+ Path?: string;
1323
+ Description?: string;
1324
+ HitHighlightedSummary?: string;
1325
+ PictureThumbnailURL?: string;
1326
+ ServerRedirectedURL?: string;
1327
+ ServerRedirectedEmbedURL?: string;
1328
+ ServerRedirectedPreviewURL?: string;
1329
+ FileExtension?: string;
1330
+ ContentTypeId?: string;
1331
+ ParentLink?: string;
1332
+ ViewsLifeTime?: number;
1333
+ ViewsRecent?: number;
1334
+ LastModifiedTime?: Date;
1335
+ FileType?: string;
1336
+ DocId?: number;
1337
+ SPWebUrl?: string;
1338
+ UniqueId?: string;
1339
+ SiteId?: string;
1340
+ WebId?: string;
1341
+ ListId?: string;
1342
+ OriginalPath?: string;
1343
+ $itemId?: number;
1344
+ })[] = [];
1345
+ rows.forEach(r => {
1346
+ try {
1347
+ const rowValues: IDictionary<string | Date | number> = {};
1348
+ r.Cells.forEach(cell => {
1349
+ rowValues[cell.Key] = cell.ValueType === "Edm.Int64" || cell.ValueType === "Edm.Double"
1350
+ ? parseInt(cell.Value, 10)
1351
+ : cell.ValueType === "Edm.DateTime"
1352
+ ? new Date(cell.Value)
1353
+ : cell.ValueType === "Null"
1354
+ ? ""
1355
+ : cell.Value
1356
+ });
1357
+ let resultPath = isNullOrEmptyString(rowValues.Path) ? "" : (rowValues.Path as string).toLowerCase();
1358
+ let indexOfId = resultPath.toLowerCase().indexOf("id=");
1359
+ let itemId = indexOfId >= 0 ? parseInt(resultPath.slice(indexOfId + 3)) : -1;
1360
+ if (itemId >= 0)
1361
+ rowValues.$itemId = itemId;
1362
+ mapped.push(rowValues);
1363
+ } catch (e) { return null; }
1364
+ });
1365
+
1366
+ return mapped;
1367
+ } catch (e) {
1368
+ logger.error(e);
1369
+ }
1370
+
1371
+ return [];
1372
+ }
1373
+
1374
+ export async function UpdateListExperience(siteUrl: string, listId: string, experience: ListExperienceOptions) {
1375
+ try {
1376
+ let url = GetListRestUrl(siteUrl, listId);
1377
+ let data = {
1378
+ "ListExperienceOptions": experience
1379
+ };
1380
+ let result = await GetJson(url, JSON.stringify(data), {
1381
+ xHttpMethod: "MERGE",
1382
+ jsonMetadata: jsonTypes.nometadata
1383
+ });
1384
+ return isNullOrEmptyString(result);
1385
+ } catch (e) {
1386
+ logger.error(e);
1387
+ }
1388
+ return false;
1389
1389
  }