@kwiz/common 1.0.101 → 1.0.102

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