@kwiz/common 1.0.4 → 1.0.6

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 (71) hide show
  1. package/dist/helpers/base64.js +10 -21
  2. package/dist/helpers/base64.js.map +1 -1
  3. package/dist/helpers/browser.d.ts +13 -5
  4. package/dist/helpers/browser.js +61 -39
  5. package/dist/helpers/browser.js.map +1 -1
  6. package/dist/helpers/collections.base.js +46 -65
  7. package/dist/helpers/collections.base.js.map +1 -1
  8. package/dist/helpers/collections.js +4 -1
  9. package/dist/helpers/collections.js.map +1 -1
  10. package/dist/helpers/date.d.ts +2 -1
  11. package/dist/helpers/date.js +22 -3
  12. package/dist/helpers/date.js.map +1 -1
  13. package/dist/helpers/debug.js +9 -20
  14. package/dist/helpers/debug.js.map +1 -1
  15. package/dist/helpers/objects.js +2 -11
  16. package/dist/helpers/objects.js.map +1 -1
  17. package/dist/helpers/promises.js +28 -41
  18. package/dist/helpers/promises.js.map +1 -1
  19. package/dist/helpers/strings.js +2 -1
  20. package/dist/helpers/strings.js.map +1 -1
  21. package/dist/utils/base64.js +18 -29
  22. package/dist/utils/base64.js.map +1 -1
  23. package/dist/utils/consolelogger.js +16 -27
  24. package/dist/utils/consolelogger.js.map +1 -1
  25. package/dist/utils/emails.js +5 -2
  26. package/dist/utils/emails.js.map +1 -1
  27. package/dist/utils/knownscript.js +4 -15
  28. package/dist/utils/knownscript.js.map +1 -1
  29. package/dist/utils/localstoragecache.js +3 -3
  30. package/dist/utils/localstoragecache.js.map +1 -1
  31. package/dist/utils/rest.d.ts +2 -0
  32. package/dist/utils/rest.js +18 -5
  33. package/dist/utils/rest.js.map +1 -1
  34. package/dist/utils/script.js +25 -40
  35. package/dist/utils/script.js.map +1 -1
  36. package/dist/utils/sharepoint.rest/file.folder.d.ts +2 -0
  37. package/dist/utils/sharepoint.rest/file.folder.js +123 -123
  38. package/dist/utils/sharepoint.rest/file.folder.js.map +1 -1
  39. package/dist/utils/sharepoint.rest/item.d.ts +10 -0
  40. package/dist/utils/sharepoint.rest/item.js +233 -237
  41. package/dist/utils/sharepoint.rest/item.js.map +1 -1
  42. package/dist/utils/sharepoint.rest/list.d.ts +30 -1
  43. package/dist/utils/sharepoint.rest/list.js +296 -289
  44. package/dist/utils/sharepoint.rest/list.js.map +1 -1
  45. package/dist/utils/sharepoint.rest/listutils/GetListItemsByCaml.js +352 -360
  46. package/dist/utils/sharepoint.rest/listutils/GetListItemsByCaml.js.map +1 -1
  47. package/dist/utils/sharepoint.rest/listutils/GetListItemsById.js +47 -58
  48. package/dist/utils/sharepoint.rest/listutils/GetListItemsById.js.map +1 -1
  49. package/dist/utils/sharepoint.rest/user-search.d.ts +5 -10
  50. package/dist/utils/sharepoint.rest/user-search.js +129 -148
  51. package/dist/utils/sharepoint.rest/user-search.js.map +1 -1
  52. package/dist/utils/sharepoint.rest/user.d.ts +1 -0
  53. package/dist/utils/sharepoint.rest/user.js +200 -169
  54. package/dist/utils/sharepoint.rest/user.js.map +1 -1
  55. package/dist/utils/sharepoint.rest/web.d.ts +10 -1
  56. package/dist/utils/sharepoint.rest/web.js +381 -401
  57. package/dist/utils/sharepoint.rest/web.js.map +1 -1
  58. package/dist/utils/sod.js +8 -19
  59. package/dist/utils/sod.js.map +1 -1
  60. package/package.json +5 -4
  61. package/src/helpers/browser.ts +56 -13
  62. package/src/helpers/date.ts +23 -1
  63. package/src/helpers/strings.test.js +14 -2
  64. package/src/helpers/strings.ts +2 -1
  65. package/src/utils/rest.ts +7 -0
  66. package/src/utils/sharepoint.rest/file.folder.ts +18 -1
  67. package/src/utils/sharepoint.rest/item.ts +54 -1
  68. package/src/utils/sharepoint.rest/list.ts +144 -0
  69. package/src/utils/sharepoint.rest/user-search.ts +10 -14
  70. package/src/utils/sharepoint.rest/user.ts +49 -3
  71. package/src/utils/sharepoint.rest/web.ts +23 -1
@@ -1,15 +1,6 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.GetSPFieldValueAsText = exports.GetSPRestItemValueAsTextArray = exports.GetSPRestItemValueAsText = exports.UpdateItem = exports.AddItem = exports.UpdateMultiTaxonomyValue = exports.DeleteAttachment = exports.AddAttachment = exports.GetListItemsAttachments = exports.GetListItemAttachments = exports.RecycleListItem = exports.DeleteListItem = exports.GetListItemFieldValuesHistory = exports.GetListItemFieldValues = exports.GetListItemFieldValue = exports.GetListItemFieldDisplayValues = exports.GetListItemFieldDisplayValue = exports.GetListItemFieldDisplayValuesSync = exports.GetListItemFieldDisplayValueSync = exports.FileSizeColumnInternalNames = void 0;
3
+ exports.SetItemCreatedModifiedInfo = exports.GetSPFieldValueAsText = exports.GetSPRestItemValueAsTextArray = exports.GetSPRestItemValueAsText = exports.UpdateItem = exports.AddItem = exports.UpdateMultiTaxonomyValue = exports.DeleteAttachment = exports.AddAttachment = exports.GetListItemsAttachments = exports.GetListItemAttachments = exports.RecycleListItem = exports.DeleteListItem = exports.GetListItemFieldValuesHistory = exports.GetListItemFieldValues = exports.GetListItemFieldValue = exports.GetListItemFieldDisplayValues = exports.GetListItemFieldDisplayValue = exports.GetListItemFieldDisplayValuesSync = exports.GetListItemFieldDisplayValueSync = exports.FileSizeColumnInternalNames = void 0;
13
4
  const knownscript_1 = require("../../utils/knownscript");
14
5
  const _dependencies_1 = require("../_dependencies");
15
6
  const consolelogger_1 = require("../consolelogger");
@@ -69,28 +60,26 @@ function _parseValueFromRawValue(rawValue, asDisplayValue = false) {
69
60
  }
70
61
  }
71
62
  }
72
- function _getListItemRawFieldValues(siteUrl, listIdOrTitle, itemId, fields, options) {
73
- return __awaiter(this, void 0, void 0, function* () {
74
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
75
- options = options || {};
76
- let listFields = yield (0, list_1.GetListFields)(siteUrl, listIdOrTitle);
77
- var { selectFields, expandFields } = _getListItemSelectExpandFields(fields, listFields);
78
- var $select = `$select=` + encodeURIComponent(selectFields.length ? `${selectFields.join(',')}` : fields.join(','));
79
- var $expand = expandFields.length ? `$expand=${encodeURIComponent(expandFields.join(','))}` : "";
80
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})?${$select}&${$expand}`;
81
- let result = yield (0, rest_1.GetJson)(url, null, { allowCache: options.refreshCache !== true });
82
- var values = {};
83
- if (result && typeof (result.d) !== "undefined") {
84
- var rawValues = result.d;
85
- fields.forEach((fieldName) => {
86
- let rawValue = (exports.FileSizeColumnInternalNames.includes(fieldName)) ? rawValues[exports.FileSizeColumnInternalNames[0]] : rawValues[fieldName];
87
- if (!(0, _dependencies_1.isNullOrUndefined)(rawValue)) {
88
- values[fieldName] = rawValue;
89
- }
90
- });
91
- }
92
- return values;
93
- });
63
+ async function _getListItemRawFieldValues(siteUrl, listIdOrTitle, itemId, fields, options) {
64
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
65
+ options = options || {};
66
+ let listFields = await (0, list_1.GetListFields)(siteUrl, listIdOrTitle);
67
+ var { selectFields, expandFields } = _getListItemSelectExpandFields(fields, listFields);
68
+ var $select = `$select=` + encodeURIComponent(selectFields.length ? `${selectFields.join(',')}` : fields.join(','));
69
+ var $expand = expandFields.length ? `$expand=${encodeURIComponent(expandFields.join(','))}` : "";
70
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})?${$select}&${$expand}`;
71
+ let result = await (0, rest_1.GetJson)(url, null, { allowCache: options.refreshCache !== true });
72
+ var values = {};
73
+ if (result && typeof (result.d) !== "undefined") {
74
+ var rawValues = result.d;
75
+ fields.forEach((fieldName) => {
76
+ let rawValue = (exports.FileSizeColumnInternalNames.includes(fieldName)) ? rawValues[exports.FileSizeColumnInternalNames[0]] : rawValues[fieldName];
77
+ if (!(0, _dependencies_1.isNullOrUndefined)(rawValue)) {
78
+ values[fieldName] = rawValue;
79
+ }
80
+ });
81
+ }
82
+ return values;
94
83
  }
95
84
  function GetListItemFieldDisplayValueSync(siteUrl, listIdOrTitle, itemId, field) {
96
85
  return GetListItemFieldDisplayValuesSync(siteUrl, listIdOrTitle, itemId, [field])[field];
@@ -102,247 +91,219 @@ function GetListItemFieldDisplayValuesSync(siteUrl, listIdOrTitle, itemId, field
102
91
  return result.success ? result.result : {};
103
92
  }
104
93
  exports.GetListItemFieldDisplayValuesSync = GetListItemFieldDisplayValuesSync;
105
- function GetListItemFieldDisplayValue(siteUrl, listIdOrTitle, itemId, field, options) {
106
- return __awaiter(this, void 0, void 0, function* () {
107
- var values = yield GetListItemFieldDisplayValues(siteUrl, listIdOrTitle, itemId, [field], options);
108
- return values && values[field];
109
- });
94
+ async function GetListItemFieldDisplayValue(siteUrl, listIdOrTitle, itemId, field, options) {
95
+ var values = await GetListItemFieldDisplayValues(siteUrl, listIdOrTitle, itemId, [field], options);
96
+ return values && values[field];
110
97
  }
111
98
  exports.GetListItemFieldDisplayValue = GetListItemFieldDisplayValue;
112
- function GetListItemFieldDisplayValues(siteUrl, listIdOrTitle, itemId, fields, options) {
113
- return __awaiter(this, void 0, void 0, function* () {
114
- var rawValues = yield _getListItemRawFieldValues(siteUrl, listIdOrTitle, itemId, fields, options);
115
- var values = {};
116
- Object.keys(rawValues).forEach(key => {
117
- var fieldValue = _parseValueFromRawValue(rawValues[key], true);
118
- if (!(0, _dependencies_1.isNullOrUndefined)(fieldValue)) {
119
- values[key] = fieldValue;
120
- }
121
- });
122
- return values;
99
+ async function GetListItemFieldDisplayValues(siteUrl, listIdOrTitle, itemId, fields, options) {
100
+ var rawValues = await _getListItemRawFieldValues(siteUrl, listIdOrTitle, itemId, fields, options);
101
+ var values = {};
102
+ Object.keys(rawValues).forEach(key => {
103
+ var fieldValue = _parseValueFromRawValue(rawValues[key], true);
104
+ if (!(0, _dependencies_1.isNullOrUndefined)(fieldValue)) {
105
+ values[key] = fieldValue;
106
+ }
123
107
  });
108
+ return values;
124
109
  }
125
110
  exports.GetListItemFieldDisplayValues = GetListItemFieldDisplayValues;
126
- function GetListItemFieldValue(siteUrl, listIdOrTitle, itemId, field, options) {
127
- return __awaiter(this, void 0, void 0, function* () {
128
- var values = yield GetListItemFieldValues(siteUrl, listIdOrTitle, itemId, [field], options);
129
- return values && values[field];
130
- });
111
+ async function GetListItemFieldValue(siteUrl, listIdOrTitle, itemId, field, options) {
112
+ var values = await GetListItemFieldValues(siteUrl, listIdOrTitle, itemId, [field], options);
113
+ return values && values[field];
131
114
  }
132
115
  exports.GetListItemFieldValue = GetListItemFieldValue;
133
- function GetListItemFieldValues(siteUrl, listIdOrTitle, itemId, fields, options) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- var rawValues = yield _getListItemRawFieldValues(siteUrl, listIdOrTitle, itemId, fields, options);
136
- var values = {};
137
- Object.keys(rawValues).forEach(key => {
138
- var fieldValue = _parseValueFromRawValue(rawValues[key]);
139
- if (!(0, _dependencies_1.isNullOrUndefined)(fieldValue)) {
140
- values[key] = fieldValue;
141
- }
142
- });
143
- return values;
116
+ async function GetListItemFieldValues(siteUrl, listIdOrTitle, itemId, fields, options) {
117
+ var rawValues = await _getListItemRawFieldValues(siteUrl, listIdOrTitle, itemId, fields, options);
118
+ var values = {};
119
+ Object.keys(rawValues).forEach(key => {
120
+ var fieldValue = _parseValueFromRawValue(rawValues[key]);
121
+ if (!(0, _dependencies_1.isNullOrUndefined)(fieldValue)) {
122
+ values[key] = fieldValue;
123
+ }
144
124
  });
125
+ return values;
145
126
  }
146
127
  exports.GetListItemFieldValues = GetListItemFieldValues;
147
128
  /** Returns version array, newest version first */
148
- function GetListItemFieldValuesHistory(siteUrl, listIdOrTitle, itemId, fields, options) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
151
- options = options || {};
152
- var $select = (0, _dependencies_1.isNotEmptyArray)(fields) ? `$select=` + encodeURIComponent(`${fields.join(',')}`) : "";
153
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/versions?${$select}`;
154
- let result = yield (0, rest_1.GetJson)(url, null, {
155
- allowCache: options.refreshCache !== true,
156
- jsonMetadata: _dependencies_1.jsonTypes.nometadata
157
- });
158
- return result && result.value || [];
129
+ async function GetListItemFieldValuesHistory(siteUrl, listIdOrTitle, itemId, fields, options) {
130
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
131
+ options = options || {};
132
+ var $select = (0, _dependencies_1.isNotEmptyArray)(fields) ? `$select=` + encodeURIComponent(`${fields.join(',')}`) : "";
133
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/versions?${$select}`;
134
+ let result = await (0, rest_1.GetJson)(url, null, {
135
+ allowCache: options.refreshCache !== true,
136
+ jsonMetadata: _dependencies_1.jsonTypes.nometadata
159
137
  });
138
+ return result && result.value || [];
160
139
  }
161
140
  exports.GetListItemFieldValuesHistory = GetListItemFieldValuesHistory;
162
- function DeleteListItem(siteUrl, listIdOrTitle, itemId) {
163
- return __awaiter(this, void 0, void 0, function* () {
164
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
165
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})`;
166
- let result = { deleted: true };
167
- try {
168
- yield (0, rest_1.GetJson)(url, null, { method: "POST", spWebUrl: siteUrl, xHttpMethod: "DELETE" });
169
- //empty string means deleted
170
- }
171
- catch (e) {
172
- result.deleted = false;
173
- result.errorMessage = (0, common_1.__getSPRestErrorData)(e).message;
174
- }
175
- return result;
176
- });
141
+ async function DeleteListItem(siteUrl, listIdOrTitle, itemId) {
142
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
143
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})`;
144
+ let result = { deleted: true };
145
+ try {
146
+ await (0, rest_1.GetJson)(url, null, { method: "POST", spWebUrl: siteUrl, xHttpMethod: "DELETE" });
147
+ //empty string means deleted
148
+ }
149
+ catch (e) {
150
+ result.deleted = false;
151
+ result.errorMessage = (0, common_1.__getSPRestErrorData)(e).message;
152
+ }
153
+ return result;
177
154
  }
178
155
  exports.DeleteListItem = DeleteListItem;
179
- function RecycleListItem(siteUrl, listIdOrTitle, itemId) {
180
- return __awaiter(this, void 0, void 0, function* () {
181
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
182
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/recycle()`;
183
- let result = { recycled: true };
184
- try {
185
- yield (0, rest_1.GetJson)(url, null, { method: "POST", spWebUrl: siteUrl });
186
- //value.d.Recycle will hold guide reference id
187
- }
188
- catch (e) {
189
- result.recycled = false;
190
- result.errorMessage = (0, common_1.__getSPRestErrorData)(e).message;
191
- }
192
- return result;
193
- });
156
+ async function RecycleListItem(siteUrl, listIdOrTitle, itemId) {
157
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
158
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/recycle()`;
159
+ let result = { recycled: true };
160
+ try {
161
+ await (0, rest_1.GetJson)(url, null, { method: "POST", spWebUrl: siteUrl });
162
+ //value.d.Recycle will hold guide reference id
163
+ }
164
+ catch (e) {
165
+ result.recycled = false;
166
+ result.errorMessage = (0, common_1.__getSPRestErrorData)(e).message;
167
+ }
168
+ return result;
194
169
  }
195
170
  exports.RecycleListItem = RecycleListItem;
196
- function GetListItemAttachments(siteUrl, listIdOrTitle, itemId) {
197
- return __awaiter(this, void 0, void 0, function* () {
198
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
199
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/AttachmentFiles`;
200
- try {
201
- let result = yield (0, rest_1.GetJson)(url, null, { includeDigestInGet: true });
202
- let attachmentFiles = result.d && result.d.results ? result.d.results : [];
203
- return attachmentFiles;
204
- }
205
- catch (e) {
206
- }
207
- return [];
208
- });
171
+ async function GetListItemAttachments(siteUrl, listIdOrTitle, itemId) {
172
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
173
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/AttachmentFiles`;
174
+ try {
175
+ let result = await (0, rest_1.GetJson)(url, null, { includeDigestInGet: true });
176
+ let attachmentFiles = result.d && result.d.results ? result.d.results : [];
177
+ return attachmentFiles;
178
+ }
179
+ catch (e) {
180
+ }
181
+ return [];
209
182
  }
210
183
  exports.GetListItemAttachments = GetListItemAttachments;
211
- function GetListItemsAttachments(siteUrl, listIdOrTitle, itemIds) {
212
- return __awaiter(this, void 0, void 0, function* () {
213
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
214
- let chunks = (0, _dependencies_1.chunkArray)(itemIds, 30);
215
- let select = `$select=ID,AttachmentFiles`;
216
- let expand = `$expand=AttachmentFiles`;
217
- let baseUrl = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items`;
218
- let promises = chunks.map((chunk) => {
219
- return () => {
220
- let filter = `$filter=${chunk.map(i => `ID eq ${i}`).join(" or ")}`;
221
- let url = `${baseUrl}?${select}&${filter}&${expand}`;
222
- return (0, rest_1.GetJson)(url, null, { includeDigestInGet: true, jsonMetadata: _dependencies_1.jsonTypes.nometadata });
223
- };
224
- });
225
- try {
226
- let result = yield (0, _dependencies_1.promiseNParallel)(promises, 5);
227
- return result && result.length > 0 ? result.map(v => v.value) : [];
228
- }
229
- catch (_a) {
230
- }
231
- return [];
184
+ async function GetListItemsAttachments(siteUrl, listIdOrTitle, itemIds) {
185
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
186
+ let chunks = (0, _dependencies_1.chunkArray)(itemIds, 30);
187
+ let select = `$select=ID,AttachmentFiles`;
188
+ let expand = `$expand=AttachmentFiles`;
189
+ let baseUrl = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items`;
190
+ let promises = chunks.map((chunk) => {
191
+ return () => {
192
+ let filter = `$filter=${chunk.map(i => `ID eq ${i}`).join(" or ")}`;
193
+ let url = `${baseUrl}?${select}&${filter}&${expand}`;
194
+ return (0, rest_1.GetJson)(url, null, { includeDigestInGet: true, jsonMetadata: _dependencies_1.jsonTypes.nometadata });
195
+ };
232
196
  });
197
+ try {
198
+ let result = await (0, _dependencies_1.promiseNParallel)(promises, 5);
199
+ return result && result.length > 0 ? result.map(v => v.value) : [];
200
+ }
201
+ catch {
202
+ }
203
+ return [];
233
204
  }
234
205
  exports.GetListItemsAttachments = GetListItemsAttachments;
235
- function AddAttachment(siteUrl, listIdOrTitle, itemId, filename, buffer) {
236
- return __awaiter(this, void 0, void 0, function* () {
237
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
238
- //Issue 999
239
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/AttachmentFiles/add(FileName='${(0, _dependencies_1.encodeURIComponentEX)(filename, { singleQuoteMultiplier: 2 })}')`;
240
- try {
241
- let result = yield (0, rest_1.GetJson)(url, buffer, { includeDigestInPost: true, method: "POST" });
242
- let attachmentFile = result && result.d;
243
- return attachmentFile;
244
- }
245
- catch (e) {
246
- }
247
- return null;
248
- });
206
+ async function AddAttachment(siteUrl, listIdOrTitle, itemId, filename, buffer) {
207
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
208
+ //Issue 999
209
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/AttachmentFiles/add(FileName='${(0, _dependencies_1.encodeURIComponentEX)(filename, { singleQuoteMultiplier: 2 })}')`;
210
+ try {
211
+ let result = await (0, rest_1.GetJson)(url, buffer, { includeDigestInPost: true, method: "POST" });
212
+ let attachmentFile = result && result.d;
213
+ return attachmentFile;
214
+ }
215
+ catch (e) {
216
+ }
217
+ return null;
249
218
  }
250
219
  exports.AddAttachment = AddAttachment;
251
- function DeleteAttachment(siteUrl, listIdOrTitle, itemId, filename) {
252
- return __awaiter(this, void 0, void 0, function* () {
253
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
254
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/AttachmentFiles('${(0, _dependencies_1.encodeURIComponentEX)(filename, { singleQuoteMultiplier: 2 })}')`;
255
- try {
256
- let result = yield (0, rest_1.GetJson)(url, null, { includeDigestInGet: true, includeDigestInPost: true, xHttpMethod: "DELETE" });
257
- let attachmentFile = result && result.d;
258
- return attachmentFile;
259
- }
260
- catch (e) {
261
- }
262
- return null;
263
- });
220
+ async function DeleteAttachment(siteUrl, listIdOrTitle, itemId, filename) {
221
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
222
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/AttachmentFiles('${(0, _dependencies_1.encodeURIComponentEX)(filename, { singleQuoteMultiplier: 2 })}')`;
223
+ try {
224
+ let result = await (0, rest_1.GetJson)(url, null, { includeDigestInGet: true, includeDigestInPost: true, xHttpMethod: "DELETE" });
225
+ let attachmentFile = result && result.d;
226
+ return attachmentFile;
227
+ }
228
+ catch (e) {
229
+ }
230
+ return null;
264
231
  }
265
232
  exports.DeleteAttachment = DeleteAttachment;
266
233
  //** Update value of taxonomy multi-value field. See issue 7585 for more info */
267
- function UpdateMultiTaxonomyValue(siteUrl, listIdOrTitle, itemId, updates) {
268
- return __awaiter(this, void 0, void 0, function* () {
269
- let fields = updates && Object.keys(updates) || [];
270
- if ((0, _dependencies_1.isNullOrEmptyArray)(fields))
271
- return [];
272
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
273
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/ValidateUpdateListItem()`;
274
- try {
275
- let result = yield (0, rest_1.GetJson)(url, JSON.stringify({
276
- bNewDocumentUpdate: false,
277
- checkInComment: null,
278
- formValues: fields.map(field => ({
279
- ErrorMessage: null,
280
- FieldName: field,
281
- FieldValue: updates[field].map(v => `${v.Label}|${v.TermGuid};`).join(''),
282
- HasException: false
283
- }))
284
- }), { includeDigestInPost: true, method: "POST" });
285
- return result && result.d && result.d.ValidateUpdateListItem.results.map(v => ({ field: v.FieldName, error: v.ErrorMEssage })) || [];
286
- }
287
- catch (e) {
288
- logger.error(`Error updating UpdateMultiTaxonomyValue ${e}`);
289
- }
290
- return fields.map(f => ({ field: f, error: 'Unspecified update error' }));
291
- });
234
+ async function UpdateMultiTaxonomyValue(siteUrl, listIdOrTitle, itemId, updates) {
235
+ let fields = updates && Object.keys(updates) || [];
236
+ if ((0, _dependencies_1.isNullOrEmptyArray)(fields))
237
+ return [];
238
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
239
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/ValidateUpdateListItem()`;
240
+ try {
241
+ let result = await (0, rest_1.GetJson)(url, JSON.stringify({
242
+ bNewDocumentUpdate: false,
243
+ checkInComment: null,
244
+ formValues: fields.map(field => ({
245
+ ErrorMessage: null,
246
+ FieldName: field,
247
+ FieldValue: updates[field].map(v => `${v.Label}|${v.TermGuid};`).join(''),
248
+ HasException: false
249
+ }))
250
+ }), { includeDigestInPost: true, method: "POST" });
251
+ return result && result.d && result.d.ValidateUpdateListItem.results.map(v => ({ field: v.FieldName, error: v.ErrorMEssage })) || [];
252
+ }
253
+ catch (e) {
254
+ logger.error(`Error updating UpdateMultiTaxonomyValue ${e}`);
255
+ }
256
+ return fields.map(f => ({ field: f, error: 'Unspecified update error' }));
292
257
  }
293
258
  exports.UpdateMultiTaxonomyValue = UpdateMultiTaxonomyValue;
294
- function AddItem(siteUrl, listIdOrTitle, fieldValues) {
295
- return __awaiter(this, void 0, void 0, function* () {
296
- //we must force creating even if no values, otherwise the item won't be created at all.
297
- return UpdateItem(siteUrl, listIdOrTitle, null, fieldValues, { updateIfNoFields: true });
298
- });
259
+ async function AddItem(siteUrl, listIdOrTitle, fieldValues) {
260
+ //we must force creating even if no values, otherwise the item won't be created at all.
261
+ return UpdateItem(siteUrl, listIdOrTitle, null, fieldValues, { updateIfNoFields: true });
299
262
  }
300
263
  exports.AddItem = AddItem;
301
- function UpdateItem(siteUrl, listIdOrTitle, itemId, fieldValues, options) {
302
- return __awaiter(this, void 0, void 0, function* () {
303
- var success = false;
304
- var error = null;
305
- try {
306
- siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
307
- let isNewItem = itemId > 0 ? false : true;
308
- let listInfo = yield (0, list_1.GetList)(siteUrl, listIdOrTitle);
309
- let fields = yield (0, list_1.GetListFieldsAsHash)(siteUrl, listIdOrTitle);
310
- let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items${isNewItem ? '' : `(${itemId})`}`;
311
- var itemUpdateInfo = {
312
- '__metadata': { 'type': `SP.Data.${listInfo.EntityTypeName}Item` }
313
- };
314
- let hasUpdates = false;
315
- Object.keys(fieldValues).forEach(updateField => {
316
- let listField = fields[updateField];
317
- if (listField) //make sure this field exists on the list
318
- {
319
- //todo: we might want to get the value first, make sure it is formatted correctly for the field type.
320
- itemUpdateInfo[(0, common_1.getFieldNameForUpdate)(listField)] = fieldValues[updateField];
321
- hasUpdates = true;
322
- }
323
- });
324
- if (!hasUpdates) {
325
- let forceUpdate = options && options.updateIfNoFields;
326
- if (!forceUpdate)
327
- return { success: true, itemId: itemId };
328
- }
329
- var xHttpMethod = isNewItem ? null : "MERGE";
330
- try {
331
- let result = yield (0, rest_1.GetJson)(url, JSON.stringify(itemUpdateInfo), { method: "POST", xHttpMethod: xHttpMethod });
332
- if (result)
333
- if (isNewItem)
334
- itemId = result.d.Id; // update item will not return data. only new item will.
335
- success = true;
336
- }
337
- catch (e) {
338
- error = (0, common_1.__getSPRestErrorData)(e).message;
264
+ async function UpdateItem(siteUrl, listIdOrTitle, itemId, fieldValues, options) {
265
+ var success = false;
266
+ var error = null;
267
+ try {
268
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
269
+ let isNewItem = itemId > 0 ? false : true;
270
+ let listInfo = await (0, list_1.GetList)(siteUrl, listIdOrTitle);
271
+ let fields = await (0, list_1.GetListFieldsAsHash)(siteUrl, listIdOrTitle);
272
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items${isNewItem ? '' : `(${itemId})`}`;
273
+ var itemUpdateInfo = {
274
+ '__metadata': { 'type': `SP.Data.${listInfo.EntityTypeName}Item` }
275
+ };
276
+ let hasUpdates = false;
277
+ Object.keys(fieldValues).forEach(updateField => {
278
+ let listField = fields[updateField];
279
+ if (listField) //make sure this field exists on the list
280
+ {
281
+ //todo: we might want to get the value first, make sure it is formatted correctly for the field type.
282
+ itemUpdateInfo[(0, common_1.getFieldNameForUpdate)(listField)] = fieldValues[updateField];
283
+ hasUpdates = true;
339
284
  }
285
+ });
286
+ if (!hasUpdates) {
287
+ let forceUpdate = options && options.updateIfNoFields;
288
+ if (!forceUpdate)
289
+ return { success: true, itemId: itemId };
290
+ }
291
+ var xHttpMethod = isNewItem ? null : "MERGE";
292
+ try {
293
+ let result = await (0, rest_1.GetJson)(url, JSON.stringify(itemUpdateInfo), { method: "POST", xHttpMethod: xHttpMethod });
294
+ if (result)
295
+ if (isNewItem)
296
+ itemId = result.d.Id; // update item will not return data. only new item will.
297
+ success = true;
340
298
  }
341
299
  catch (e) {
342
- logger.group(() => logger.error(e), 'Update item failed');
300
+ error = (0, common_1.__getSPRestErrorData)(e).message;
343
301
  }
344
- return { success: success, errorMessage: error, itemId: itemId };
345
- });
302
+ }
303
+ catch (e) {
304
+ logger.group(() => logger.error(e), 'Update item failed');
305
+ }
306
+ return { success: success, errorMessage: error, itemId: itemId };
346
307
  }
347
308
  exports.UpdateItem = UpdateItem;
348
309
  /** Get a sharepoint field value as text, from a rest item */
@@ -441,4 +402,39 @@ function GetSPFieldValueAsText(value, field) {
441
402
  return textResults;
442
403
  }
443
404
  exports.GetSPFieldValueAsText = GetSPFieldValueAsText;
405
+ /** set an existing item system info: author, editor, created and modified dates */
406
+ async function SetItemCreatedModifiedInfo(siteUrl, listIdOrTitle, itemId, updates) {
407
+ let updateValues = {};
408
+ let fields = updates && Object.keys(updates) || [];
409
+ if (!(0, _dependencies_1.isNullOrEmptyString)(updates.Created)) //date must be yyyy-MM-dd hh:mm:ss
410
+ updateValues.Created = ((0, _dependencies_1.isString)(updates.Created) ? new Date(updates.Created) : updates.Created).toISOString().replace('T', ' ').split('.')[0];
411
+ if (!(0, _dependencies_1.isNullOrEmptyString)(updates.Modified))
412
+ updateValues.Modified = ((0, _dependencies_1.isString)(updates.Modified) ? new Date(updates.Modified) : updates.Modified).toISOString().replace('T', ' ').split('.')[0];
413
+ if (updates.AuthorId > 0) {
414
+ let asUser = await (0, user_1.GetUser)(siteUrl, updates.AuthorId);
415
+ updateValues.AuthorId = `[{'Key':'${asUser.UserPrincipalName}'}]`; //[{'Key':'i:0#.f|membership|user@Tenant.onmicrosoft.com'}]
416
+ }
417
+ if (updates.EditorId > 0) {
418
+ let asUser = await (0, user_1.GetUser)(siteUrl, updates.EditorId);
419
+ updateValues.AuthorId = `[{'Key':'${asUser.UserPrincipalName}'}]`; //[{'Key':'i:0#.f|membership|user@Tenant.onmicrosoft.com'}]
420
+ }
421
+ if ((0, _dependencies_1.isNullOrEmptyArray)(fields))
422
+ return [];
423
+ siteUrl = (0, common_1.GetSiteUrl)(siteUrl);
424
+ let url = (0, list_1.GetListRestUrl)(siteUrl, listIdOrTitle) + `/items(${itemId})/ValidateUpdateListItem()`;
425
+ try {
426
+ let result = await (0, rest_1.GetJson)(url, JSON.stringify({
427
+ formValues: fields.map(field => ({
428
+ FieldName: field,
429
+ FieldValue: updateValues[field]
430
+ }))
431
+ }), { method: "POST" });
432
+ return result && result.d && result.d.ValidateUpdateListItem.results.map(v => ({ field: v.FieldName, error: v.ErrorMEssage })) || [];
433
+ }
434
+ catch (e) {
435
+ logger.error(`Error updating values ${e}`);
436
+ }
437
+ return fields.map(f => ({ field: f, error: 'Unspecified update error' }));
438
+ }
439
+ exports.SetItemCreatedModifiedInfo = SetItemCreatedModifiedInfo;
444
440
  //# sourceMappingURL=item.js.map