@kwiz/common 1.0.96 → 1.0.97

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