@kwiz/common 1.0.78 → 1.0.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/.github/workflows/npm-publish.yml +24 -0
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/sharepoint.js +5 -1
  6. package/lib/cjs/helpers/sharepoint.js.map +1 -1
  7. package/lib/cjs/helpers/typecheckers.js +5 -1
  8. package/lib/cjs/helpers/typecheckers.js.map +1 -1
  9. package/lib/cjs/types/libs/msal.types.js +26 -26
  10. package/lib/cjs/utils/sharepoint.rest/list.js +1 -1
  11. package/lib/cjs/utils/sharepoint.rest/list.js.map +1 -1
  12. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  13. package/lib/esm/helpers/sharepoint.js +3 -0
  14. package/lib/esm/helpers/sharepoint.js.map +1 -1
  15. package/lib/esm/helpers/typecheckers.js +3 -0
  16. package/lib/esm/helpers/typecheckers.js.map +1 -1
  17. package/lib/esm/types/libs/msal.types.js +26 -26
  18. package/lib/esm/utils/sharepoint.rest/list.js +2 -2
  19. package/lib/esm/utils/sharepoint.rest/list.js.map +1 -1
  20. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  21. package/lib/types/helpers/sharepoint.d.ts +1 -0
  22. package/lib/types/helpers/typecheckers.d.ts +1 -0
  23. package/package.json +77 -77
  24. package/readme.md +17 -17
  25. package/src/_dependencies.ts +12 -12
  26. package/src/config.ts +17 -17
  27. package/src/helpers/Guid.ts +181 -181
  28. package/src/helpers/base64.ts +173 -173
  29. package/src/helpers/browser.test.js +13 -13
  30. package/src/helpers/browser.ts +1348 -1348
  31. package/src/helpers/browserinfo.ts +292 -292
  32. package/src/helpers/collections.base.test.js +25 -25
  33. package/src/helpers/collections.base.ts +437 -437
  34. package/src/helpers/collections.ts +107 -107
  35. package/src/helpers/color.ts +54 -54
  36. package/src/helpers/cookies.ts +59 -59
  37. package/src/helpers/date.test.js +119 -119
  38. package/src/helpers/date.ts +188 -188
  39. package/src/helpers/debug.ts +186 -186
  40. package/src/helpers/emails.ts +6 -6
  41. package/src/helpers/eval.ts +5 -5
  42. package/src/helpers/file.test.js +50 -50
  43. package/src/helpers/file.ts +58 -58
  44. package/src/helpers/flatted.ts +149 -149
  45. package/src/helpers/functions.ts +16 -16
  46. package/src/helpers/graph/calendar.types.ts +10 -10
  47. package/src/helpers/http.ts +69 -69
  48. package/src/helpers/images.ts +22 -22
  49. package/src/helpers/json.ts +38 -38
  50. package/src/helpers/md5.ts +189 -189
  51. package/src/helpers/objects.test.js +33 -33
  52. package/src/helpers/objects.ts +270 -270
  53. package/src/helpers/promises.test.js +37 -37
  54. package/src/helpers/promises.ts +165 -165
  55. package/src/helpers/random.ts +27 -27
  56. package/src/helpers/scheduler/scheduler.test.js +103 -103
  57. package/src/helpers/scheduler/scheduler.ts +131 -131
  58. package/src/helpers/sharepoint.ts +776 -772
  59. package/src/helpers/strings.test.js +101 -101
  60. package/src/helpers/strings.ts +317 -317
  61. package/src/helpers/typecheckers.test.js +34 -34
  62. package/src/helpers/typecheckers.ts +266 -262
  63. package/src/helpers/url.test.js +43 -43
  64. package/src/helpers/url.ts +207 -207
  65. package/src/helpers/urlhelper.ts +111 -111
  66. package/src/index.ts +6 -6
  67. package/src/types/auth.ts +54 -54
  68. package/src/types/common.types.ts +15 -15
  69. package/src/types/flatted.types.ts +59 -59
  70. package/src/types/globals.types.ts +6 -6
  71. package/src/types/graph/calendar.types.ts +80 -80
  72. package/src/types/knownscript.types.ts +18 -18
  73. package/src/types/libs/datajs.types.ts +28 -28
  74. package/src/types/libs/ics.types.ts +30 -30
  75. package/src/types/libs/msal.types.ts +49 -49
  76. package/src/types/locales.ts +124 -124
  77. package/src/types/localstoragecache.types.ts +8 -8
  78. package/src/types/location.types.ts +27 -27
  79. package/src/types/moment.ts +11 -11
  80. package/src/types/regex.types.ts +16 -16
  81. package/src/types/rest.types.ts +95 -95
  82. package/src/types/sharepoint.types.ts +1465 -1465
  83. package/src/types/sharepoint.utils.types.ts +287 -287
  84. package/src/utils/auth/common.ts +74 -74
  85. package/src/utils/auth/discovery.test.js +12 -12
  86. package/src/utils/auth/discovery.ts +132 -132
  87. package/src/utils/base64.ts +27 -27
  88. package/src/utils/consolelogger.ts +320 -320
  89. package/src/utils/date.ts +35 -35
  90. package/src/utils/emails.ts +24 -24
  91. package/src/utils/knownscript.ts +286 -286
  92. package/src/utils/localstoragecache.ts +441 -441
  93. package/src/utils/rest.ts +501 -501
  94. package/src/utils/script.ts +170 -170
  95. package/src/utils/sharepoint.rest/common.ts +154 -154
  96. package/src/utils/sharepoint.rest/date.ts +62 -62
  97. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  98. package/src/utils/sharepoint.rest/item.ts +547 -547
  99. package/src/utils/sharepoint.rest/list.ts +1388 -1388
  100. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  101. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  102. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  103. package/src/utils/sharepoint.rest/location.ts +141 -141
  104. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  105. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  106. package/src/utils/sharepoint.rest/user.ts +491 -491
  107. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  108. package/src/utils/sod.ts +194 -194
  109. package/lib/cjs/helpers/_dependencies.js +0 -21
  110. package/lib/cjs/helpers/_dependencies.js.map +0 -1
  111. package/lib/cjs/utils/_dependencies.js +0 -24
  112. package/lib/cjs/utils/_dependencies.js.map +0 -1
  113. package/lib/esm/helpers/_dependencies.js +0 -3
  114. package/lib/esm/helpers/_dependencies.js.map +0 -1
  115. package/lib/esm/utils/_dependencies.js +0 -4
  116. package/lib/esm/utils/_dependencies.js.map +0 -1
  117. package/lib/types/helpers/_dependencies.d.ts +0 -2
  118. package/lib/types/utils/_dependencies.d.ts +0 -3
@@ -1,492 +1,492 @@
1
- import { jsonStringify } from "../../helpers/json";
2
- import { ISPPeoplePickerControlFormEntity, IsSPPeoplePickerControlFormEntity, getPrincipalTypeFromPickerEntity } from "../../helpers/sharepoint";
3
- import { isNullOrEmptyArray, isNullOrEmptyString, isNullOrNaN, isNullOrUndefined, isNumber } from "../../helpers/typecheckers";
4
- import { encodeURIComponentEX } from "../../helpers/url";
5
- import { contentTypes, jsonTypes } from "../../types/rest.types";
6
- import { ISiteGroupInfo, PrincipalType } from "../../types/sharepoint.types";
7
- import { IGroupInfo, IUserInfo } from "../../types/sharepoint.utils.types";
8
- import { ConsoleLogger } from "../consolelogger";
9
- import { GetJson, GetJsonSync, longLocalCache, shortLocalCache } from "../rest";
10
- import { GetRestBaseUrl, GetSiteUrl } from "./common";
11
- import { GetSiteId } from "./web";
12
-
13
- const logger = ConsoleLogger.get("utils/sharepoint/user");
14
- var __currentUserId: number = null;
15
- const groupSelect = "Id,Title,Description,CanCurrentUserViewMembership,OnlyAllowMembersViewMembership,IsHiddenInUI,OwnerTitle";
16
- const userSelect = "PrincipalType,Id,LoginName,UserPrincipalName,Title,IsSiteAdmin,Email";
17
-
18
-
19
- /** Get user login name */
20
- export function GetUserLoginName(siteUrl?: string): Promise<string> {
21
- siteUrl = GetSiteUrl(siteUrl);
22
-
23
- if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
24
- //issue 6309 _spPageContextInfo.userLoginName is wrong for external users
25
- return Promise.resolve(_spPageContextInfo.userPrincipalName);
26
-
27
- return GetJson<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache })
28
- .then(r => r.d.LoginName)
29
- .catch<string>(() => null);
30
- }
31
-
32
- /** Get user login name syncronously */
33
- export function GetUserLoginNameSync(siteUrl?: string): string {
34
- siteUrl = GetSiteUrl(siteUrl);
35
-
36
- if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
37
- //issue 6309 _spPageContextInfo.userLoginName is wrong for external users
38
- return _spPageContextInfo.userPrincipalName;
39
-
40
- let res = GetJsonSync<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache });
41
- if (res.success)
42
- return res.result.d.LoginName;
43
- else return null;
44
- }
45
-
46
- function _getCurrentUserRequestUrl(siteUrl: string, expandGroups: boolean) {
47
- siteUrl = GetSiteUrl(siteUrl);
48
-
49
- var url = `${GetRestBaseUrl(siteUrl)}/web/currentUser${expandGroups ? '?$expand=Groups' : ''}`;
50
- return url;
51
- }
52
-
53
- export async function GetCurrentUser(siteUrl?: string, options?: { expandGroups: boolean; refreshCache?: boolean; }): Promise<IUserInfo> {
54
- siteUrl = GetSiteUrl(siteUrl);
55
-
56
- return GetJson<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
57
- {
58
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
59
- allowCache: !options || options.refreshCache !== true
60
- })
61
- .then(user => {
62
- if (user)
63
- __currentUserId = user.Id;
64
- return user;
65
- })
66
- .catch<IUserInfo>(() => null);
67
- }
68
-
69
- export function GetCurrentUserSync(siteUrl?: string, options?: {
70
- /** expand groups only includes SP groups the user is a direct member of. It does not include groups associated through a security group membership, teams or M365 group */
71
- expandGroups: boolean;
72
- }): IUserInfo {
73
- siteUrl = GetSiteUrl(siteUrl);
74
-
75
- let res = GetJsonSync<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
76
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
77
- if (res.success) {
78
- let user = res.result;
79
- if (user)
80
- __currentUserId = user.Id;
81
-
82
- return user;
83
- }
84
- else return null;
85
- }
86
-
87
- function _getUserRequestUrl(siteUrl: string, userId: number, expandGroups: boolean) {
88
- siteUrl = GetSiteUrl(siteUrl);
89
-
90
- var url = `${GetRestBaseUrl(siteUrl)}/web/GetUserById(${userId})${expandGroups ? '?expand=Groups' : ''}`;
91
- return url;
92
- }
93
-
94
- export async function GetUser(siteUrl?: string, userId?: number, options?: {
95
- /** expand groups only includes SP groups the user is a direct member of. It does not include groups associated through a security group membership, teams or M365 group */
96
- expandGroups: boolean;
97
- }): Promise<IUserInfo> {
98
- siteUrl = GetSiteUrl(siteUrl);
99
-
100
- if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUser(siteUrl, options);
101
- return GetJson<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null, {
102
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata
103
- }).then(user => {
104
- return user;
105
- }).catch<IUserInfo>(() => null);
106
- }
107
-
108
- export function GetUserSync(siteUrl?: string, userId?: number, options?: { expandGroups: boolean; }): IUserInfo {
109
- siteUrl = GetSiteUrl(siteUrl);
110
-
111
- if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUserSync(siteUrl, options);
112
-
113
- let res = GetJsonSync<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null,
114
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
115
- if (res.success) {
116
- let user = res.result;
117
- return user;
118
- }
119
- else return null;
120
- }
121
-
122
- function _getUserByLoginNameRequestUrl(siteUrl: string, loginName: string, expandGroups: boolean) {
123
- siteUrl = GetSiteUrl(siteUrl);
124
-
125
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteUsers/getByLoginName(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
126
- return url;
127
- }
128
-
129
- export async function GetUserByLogin(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
130
- siteUrl = GetSiteUrl(siteUrl);
131
-
132
- if (isNullOrEmptyString(loginName)) {
133
- return GetCurrentUser(siteUrl, options);
134
- }
135
-
136
- return GetJson<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
137
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata })
138
- .then(user => user)
139
- .catch<IUserInfo>(() => null);
140
- }
141
-
142
- export function GetUserByLoginSync(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): IUserInfo {
143
- siteUrl = GetSiteUrl(siteUrl);
144
-
145
- if (isNullOrEmptyString(loginName)) {
146
- return GetCurrentUserSync(siteUrl, options);
147
- }
148
-
149
- let res = GetJsonSync<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
150
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
151
- if (res.success) {
152
- let user = res.result;
153
- return user;
154
- }
155
-
156
- return null;
157
- }
158
-
159
- function _getEnsureUserRequestUrl(siteUrl: string, loginName: string, expandGroups?: boolean) {
160
- siteUrl = GetSiteUrl(siteUrl);
161
-
162
- var url = `${GetRestBaseUrl(siteUrl)}/web/ensureUser(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
163
- return url;
164
- }
165
-
166
- export async function EnsureUser(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
167
- siteUrl = GetSiteUrl(siteUrl);
168
-
169
- if (isNullOrEmptyString(userLogin)) return null;
170
-
171
- return GetJson<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
172
- { method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
173
- .then(user => {
174
- return user;
175
- })
176
- .catch<IUserInfo>(() => null);
177
- }
178
-
179
- export function EnsureUserSync(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): IUserInfo {
180
- siteUrl = GetSiteUrl(siteUrl);
181
-
182
- if (isNullOrEmptyString(userLogin)) return null;
183
-
184
- let res = GetJsonSync<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
185
- { method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
186
- if (res.success) {
187
- let user = res.result;
188
- return user;
189
- }
190
- return null;
191
- }
192
-
193
- export function GetOrEnsureUserByLoginSync(siteUrl: string, key: string, options?: { expandGroups: boolean; }) {
194
- let userValue = GetUserByLoginSync(siteUrl, key, options);
195
- if (!userValue) {
196
- userValue = EnsureUserSync(siteUrl, key, options);
197
- }
198
- return userValue;
199
- }
200
-
201
- export async function GetSecurityGroupByTitle(siteUrl: string, title: string): Promise<IUserInfo> {
202
- siteUrl = GetSiteUrl(siteUrl);
203
- //on premise the title/name of security group could be as domain login
204
- //for example, 'KWIZCOM\ad_qa_group'
205
- //split[0] = will contain the domain name (KWIZCOM)
206
- //split[1] = will contain the title (ad_qa_group)
207
- //if split[1] is null, then we didn't get a domain login and the split[0] will just contain the title/name (ad_qa_group) of the group
208
- var split = title.split("\\");
209
- var groupTitle = (split[1] || split[0]).toLowerCase();
210
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
211
-
212
- return GetJson<{ value: IUserInfo[]; }>(url, null,
213
- { method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
214
- .then(securityGroupsResult => {
215
- var securityGroup: IUserInfo = null;
216
- if (securityGroupsResult && securityGroupsResult.value && securityGroupsResult.value.length) {
217
- //first match the full title and fall back to the split title/name
218
- securityGroup = securityGroupsResult.value.filter((secGroup) => {
219
- //this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
220
- //but will not match when exporting from on premise to online
221
- return secGroup.Title.toLowerCase() === title.toLowerCase();
222
- })[0] || securityGroupsResult.value.filter((secGroup) => {
223
- //this will match settings exported from on premise to online where the title/name of the group changes from 'KWIZCOM\ad_qa_group' to 'AD_QA_GROUP'
224
- return secGroup.Title.toLowerCase() === groupTitle;
225
- })[0];
226
- }
227
- return securityGroup;
228
- })
229
- .catch<IUserInfo>(() => null);
230
- }
231
-
232
- export function GetSecurityGroupByTitleSync(siteUrl: string, title: string): IUserInfo {
233
- siteUrl = GetSiteUrl(siteUrl);
234
- //on premise the title/name of security group could be as domain login
235
- //for example, 'KWIZCOM\ad_qa_group'
236
- //split[0] = will contain the domain name (KWIZCOM)
237
- //split[1] = will contain the title (ad_qa_group)
238
- //if split[1] is null, then we didn't get a domain login and the split[0] will just contain the title/name (ad_qa_group) of the group
239
- var split = title.split("\\");
240
- var groupTitle = (split[1] || split[0]).toLowerCase();
241
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
242
-
243
- let securityGroupsResult = GetJsonSync<{ value: IUserInfo[]; }>(url, null,
244
- { method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
245
-
246
- if (securityGroupsResult && securityGroupsResult.success) {
247
- var securityGroup: IUserInfo = null;
248
- if (securityGroupsResult && securityGroupsResult.result && securityGroupsResult.result.value && securityGroupsResult.result.value.length) {
249
- //first match the full title and fall back to the split title/name
250
- securityGroup = securityGroupsResult.result.value.filter((secGroup) => {
251
- //this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
252
- //but will not match when exporting from on premise to online
253
- return secGroup.Title.toLowerCase() === title.toLowerCase();
254
- })[0] || securityGroupsResult.result.value.filter((secGroup) => {
255
- //this will match settings exported from on premise to online where the title/name of the group changes from 'KWIZCOM\ad_qa_group' to 'AD_QA_GROUP'
256
- return secGroup.Title.toLowerCase() === groupTitle;
257
- })[0];
258
- }
259
- return securityGroup;
260
- }
261
- return null;
262
- }
263
-
264
- function _getGroupRequestUrl(siteUrl: string, groupId: number) {
265
- siteUrl = GetSiteUrl(siteUrl);
266
-
267
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})?$select=${groupSelect}`;
268
- return url;
269
- }
270
-
271
- function _getGroupUsersRequestUrl(siteUrl: string, groupId: number) {
272
- siteUrl = GetSiteUrl(siteUrl);
273
-
274
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})/Users?$select=${userSelect}`;
275
- return url;
276
- }
277
-
278
- export async function GetGroup(siteUrl?: string, groupId?: number, options?: {
279
- expandUsers: boolean;
280
- refreshCache?: boolean;
281
- }): Promise<IGroupInfo> {
282
- siteUrl = GetSiteUrl(siteUrl);
283
-
284
- return GetJson<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
285
- {
286
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
287
- allowCache: !options || options.refreshCache !== true
288
- })
289
- .then(async group => {
290
- if (group) {
291
- group.PrincipalType = PrincipalType.SharePointGroup;
292
- group.LoginName = group.Title;
293
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
294
- let users = await GetJson<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, {
295
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
296
- allowCache: !options || options.refreshCache !== true
297
- });
298
- group.Users = users && users.value;
299
- }
300
- }
301
- return group;
302
- })
303
- .catch<IGroupInfo>(() => null);
304
- }
305
-
306
- export function GetGroupSync(siteUrl?: string, groupId?: number, options?: { expandUsers: boolean; }): IGroupInfo {
307
- siteUrl = GetSiteUrl(siteUrl);
308
-
309
- let res = GetJsonSync<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
310
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
311
- if (res.success) {
312
- let group = res.result;
313
- if (group) {
314
- group.PrincipalType = PrincipalType.SharePointGroup;
315
- group.LoginName = group.Title;
316
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
317
- let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
318
- group.Users = users.success && users.result && users.result.value;
319
- }
320
- }
321
- return group;
322
- }
323
- else return null;
324
- }
325
-
326
- function _getGroupsRequestUrl(siteUrl: string) {
327
- siteUrl = GetSiteUrl(siteUrl);
328
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups?$select=${groupSelect}`;
329
- return url;
330
- }
331
-
332
- function _getGroupByNameRequestUrl(siteUrl: string, groupName: string) {
333
- var url = `${_getGroupsRequestUrl(siteUrl)}&$filter=LoginName eq '${encodeURIComponentEX(groupName, { singleQuoteMultiplier: 2 })}'`;
334
- return url;
335
- }
336
-
337
- export async function GetGroupByName(siteUrl: string, groupName: string, options?: {
338
- expandUsers: boolean;
339
- refreshCache?: boolean;
340
- }): Promise<IGroupInfo> {
341
- siteUrl = GetSiteUrl(siteUrl);
342
-
343
- let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
344
- { ...shortLocalCache, allowCache: !options || options.refreshCache !== true });
345
-
346
- if (res) {
347
- let group = res && res.d && res.d.results && res.d.results[0];
348
- if (group) {
349
- group.PrincipalType = PrincipalType.SharePointGroup;
350
- group.LoginName = group.Title;
351
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
352
- let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, {
353
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
354
- allowCache: !options || options.refreshCache !== true
355
- });
356
- group.Users = users.success && users.result && users.result.value;
357
- }
358
- }
359
- return group;
360
- }
361
- else return null;
362
- }
363
-
364
- export function GetGroupByNameSync(siteUrl: string, groupName: string, options?: { expandUsers: boolean; }): IGroupInfo {
365
- siteUrl = GetSiteUrl(siteUrl);
366
-
367
- let res = GetJsonSync<{ value: IGroupInfo[]; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
368
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
369
- if (res.success) {
370
- let group = res.result && res.result.value && res.result.value[0];
371
- if (group) {
372
- group.PrincipalType = PrincipalType.SharePointGroup;
373
- group.LoginName = group.Title;
374
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
375
- let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
376
- group.Users = users.success && users.result && users.result.value;
377
- }
378
- }
379
- return group;
380
- }
381
- else return null;
382
- }
383
-
384
- export async function GetSiteGroups(siteUrl: string, refreshCache?: boolean) {
385
- siteUrl = GetSiteUrl(siteUrl);
386
- let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupsRequestUrl(siteUrl), null,
387
- { ...shortLocalCache, allowCache: refreshCache !== true });
388
-
389
- if (res) {
390
- let groups = res && res.d && res.d.results || [];
391
- groups.forEach(g => {
392
- g.PrincipalType = PrincipalType.SharePointGroup;
393
- g.LoginName = g.Title;
394
- });
395
- return groups;
396
- }
397
- else return [];
398
- }
399
-
400
- export function GetInfoFromSPPeoplePickerControlFormEntity(entity: ISPPeoplePickerControlFormEntity): IUserInfo | IGroupInfo {
401
- if (IsSPPeoplePickerControlFormEntity(entity)) {
402
- var principalType = getPrincipalTypeFromPickerEntity(entity);
403
- if (isNullOrUndefined(principalType)) {
404
- let userValue = GetOrEnsureUserByLoginSync(null, entity.Key);
405
- if (userValue) {
406
- return userValue;
407
- }
408
- let groupValue = GetGroupByNameSync(null, entity.Key);
409
- if (groupValue) {
410
- return groupValue;
411
- }
412
- } else if (principalType === PrincipalType.SharePointGroup) {
413
- return GetGroupByNameSync(null, entity.Key);
414
- } else {
415
- return GetOrEnsureUserByLoginSync(null, entity.Key);
416
- }
417
- }
418
- return null;
419
- }
420
-
421
- export async function CreateSiteGroup(siteUrl: string, info: { name: string, description: string }): Promise<ISiteGroupInfo> {
422
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups`;
423
- let createGroup = await GetJson<{ d: ISiteGroupInfo }>(url, jsonStringify({
424
- __metadata: {
425
- type: "SP.Group"
426
- },
427
- Title: info.name,
428
- Description: info.description
429
- }), { allowCache: false });
430
- return createGroup.d;
431
- }
432
- export async function AddUserToGroup(siteUrl: string, groupId: number, userIdOrLogin: number | string): Promise<void> {
433
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups(${groupId})/users`;
434
- if (isNumber(userIdOrLogin)) {
435
- let ensured = await GetUser(siteUrl, userIdOrLogin);
436
- userIdOrLogin = ensured.LoginName;
437
- }
438
-
439
- await GetJson(url, jsonStringify({
440
- LoginName: userIdOrLogin
441
- }), { allowCache: false, jsonMetadata: jsonTypes.nometadata });
442
-
443
- }
444
- export async function RemoveUserFromGroup(siteUrl: string, groupId: number, userId: number): Promise<void> {
445
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups(${groupId})/users/removeById(${userId})`;
446
- await GetJson(url, null, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata });
447
- }
448
-
449
- export async function SetGroupOwner(siteUrl: string, groupId: number, ownerId: number, ownerIsAGroup?: boolean) {
450
- //https://github.com/SharePoint/sp-dev-docs/issues/5031#issuecomment-594710013
451
- //if owner is a group - rest API doens't work.
452
- if (ownerIsAGroup !== true) {
453
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById('${groupId}')/SetUserAsOwner(${ownerId})`;
454
- try {
455
- await GetJson<{ 'odata.null': true }>(url, null, { jsonMetadata: jsonTypes.nometadata, method: "POST" });
456
- return true;
457
- } catch (e) {
458
- logger.error(`SetGroupOwner ${groupId} ${ownerId} error:`);
459
- logger.error(e);
460
- return false;
461
- }
462
- }
463
- else {
464
- try {
465
- let soapUrl = `${GetSiteUrl(siteUrl)}_vti_bin/client.svc/ProcessQuery`;
466
- let siteId = await GetSiteId(siteUrl);
467
- let serviceJSONResponse = await GetJson<{ ErrorInfo?: string }[]>(soapUrl, `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
468
- <Actions>
469
- <SetProperty Id="1" ObjectPathId="2" Name="Owner">
470
- <Parameter ObjectPathId="3" />
471
- </SetProperty>
472
- <Method Name="Update" Id="4" ObjectPathId="2" />
473
- </Actions>
474
- <ObjectPaths>
475
- <Identity Id="2" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${groupId}" />
476
- <Identity Id="3" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${ownerId}" />
477
- </ObjectPaths>
478
- </Request>`, {
479
- headers: {
480
- Accept: jsonTypes.standard,
481
- "content-type": contentTypes.xml
482
- }
483
- });
484
- //logger.json(serviceJSONResponse, "soap result");
485
- return isNullOrEmptyArray(serviceJSONResponse) || isNullOrEmptyString(serviceJSONResponse[0].ErrorInfo);
486
- } catch (e) {
487
- logger.error(`SetGroupOwner via SOAP ${ownerId} ${ownerId} error:`);
488
- logger.error(e);
489
- return false;
490
- }
491
- }
1
+ import { jsonStringify } from "../../helpers/json";
2
+ import { ISPPeoplePickerControlFormEntity, IsSPPeoplePickerControlFormEntity, getPrincipalTypeFromPickerEntity } from "../../helpers/sharepoint";
3
+ import { isNullOrEmptyArray, isNullOrEmptyString, isNullOrNaN, isNullOrUndefined, isNumber } from "../../helpers/typecheckers";
4
+ import { encodeURIComponentEX } from "../../helpers/url";
5
+ import { contentTypes, jsonTypes } from "../../types/rest.types";
6
+ import { ISiteGroupInfo, PrincipalType } from "../../types/sharepoint.types";
7
+ import { IGroupInfo, IUserInfo } from "../../types/sharepoint.utils.types";
8
+ import { ConsoleLogger } from "../consolelogger";
9
+ import { GetJson, GetJsonSync, longLocalCache, shortLocalCache } from "../rest";
10
+ import { GetRestBaseUrl, GetSiteUrl } from "./common";
11
+ import { GetSiteId } from "./web";
12
+
13
+ const logger = ConsoleLogger.get("utils/sharepoint/user");
14
+ var __currentUserId: number = null;
15
+ const groupSelect = "Id,Title,Description,CanCurrentUserViewMembership,OnlyAllowMembersViewMembership,IsHiddenInUI,OwnerTitle";
16
+ const userSelect = "PrincipalType,Id,LoginName,UserPrincipalName,Title,IsSiteAdmin,Email";
17
+
18
+
19
+ /** Get user login name */
20
+ export function GetUserLoginName(siteUrl?: string): Promise<string> {
21
+ siteUrl = GetSiteUrl(siteUrl);
22
+
23
+ if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
24
+ //issue 6309 _spPageContextInfo.userLoginName is wrong for external users
25
+ return Promise.resolve(_spPageContextInfo.userPrincipalName);
26
+
27
+ return GetJson<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache })
28
+ .then(r => r.d.LoginName)
29
+ .catch<string>(() => null);
30
+ }
31
+
32
+ /** Get user login name syncronously */
33
+ export function GetUserLoginNameSync(siteUrl?: string): string {
34
+ siteUrl = GetSiteUrl(siteUrl);
35
+
36
+ if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
37
+ //issue 6309 _spPageContextInfo.userLoginName is wrong for external users
38
+ return _spPageContextInfo.userPrincipalName;
39
+
40
+ let res = GetJsonSync<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache });
41
+ if (res.success)
42
+ return res.result.d.LoginName;
43
+ else return null;
44
+ }
45
+
46
+ function _getCurrentUserRequestUrl(siteUrl: string, expandGroups: boolean) {
47
+ siteUrl = GetSiteUrl(siteUrl);
48
+
49
+ var url = `${GetRestBaseUrl(siteUrl)}/web/currentUser${expandGroups ? '?$expand=Groups' : ''}`;
50
+ return url;
51
+ }
52
+
53
+ export async function GetCurrentUser(siteUrl?: string, options?: { expandGroups: boolean; refreshCache?: boolean; }): Promise<IUserInfo> {
54
+ siteUrl = GetSiteUrl(siteUrl);
55
+
56
+ return GetJson<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
57
+ {
58
+ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
59
+ allowCache: !options || options.refreshCache !== true
60
+ })
61
+ .then(user => {
62
+ if (user)
63
+ __currentUserId = user.Id;
64
+ return user;
65
+ })
66
+ .catch<IUserInfo>(() => null);
67
+ }
68
+
69
+ export function GetCurrentUserSync(siteUrl?: string, options?: {
70
+ /** expand groups only includes SP groups the user is a direct member of. It does not include groups associated through a security group membership, teams or M365 group */
71
+ expandGroups: boolean;
72
+ }): IUserInfo {
73
+ siteUrl = GetSiteUrl(siteUrl);
74
+
75
+ let res = GetJsonSync<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
76
+ { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
77
+ if (res.success) {
78
+ let user = res.result;
79
+ if (user)
80
+ __currentUserId = user.Id;
81
+
82
+ return user;
83
+ }
84
+ else return null;
85
+ }
86
+
87
+ function _getUserRequestUrl(siteUrl: string, userId: number, expandGroups: boolean) {
88
+ siteUrl = GetSiteUrl(siteUrl);
89
+
90
+ var url = `${GetRestBaseUrl(siteUrl)}/web/GetUserById(${userId})${expandGroups ? '?expand=Groups' : ''}`;
91
+ return url;
92
+ }
93
+
94
+ export async function GetUser(siteUrl?: string, userId?: number, options?: {
95
+ /** expand groups only includes SP groups the user is a direct member of. It does not include groups associated through a security group membership, teams or M365 group */
96
+ expandGroups: boolean;
97
+ }): Promise<IUserInfo> {
98
+ siteUrl = GetSiteUrl(siteUrl);
99
+
100
+ if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUser(siteUrl, options);
101
+ return GetJson<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null, {
102
+ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata
103
+ }).then(user => {
104
+ return user;
105
+ }).catch<IUserInfo>(() => null);
106
+ }
107
+
108
+ export function GetUserSync(siteUrl?: string, userId?: number, options?: { expandGroups: boolean; }): IUserInfo {
109
+ siteUrl = GetSiteUrl(siteUrl);
110
+
111
+ if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUserSync(siteUrl, options);
112
+
113
+ let res = GetJsonSync<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null,
114
+ { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
115
+ if (res.success) {
116
+ let user = res.result;
117
+ return user;
118
+ }
119
+ else return null;
120
+ }
121
+
122
+ function _getUserByLoginNameRequestUrl(siteUrl: string, loginName: string, expandGroups: boolean) {
123
+ siteUrl = GetSiteUrl(siteUrl);
124
+
125
+ var url = `${GetRestBaseUrl(siteUrl)}/web/siteUsers/getByLoginName(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
126
+ return url;
127
+ }
128
+
129
+ export async function GetUserByLogin(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
130
+ siteUrl = GetSiteUrl(siteUrl);
131
+
132
+ if (isNullOrEmptyString(loginName)) {
133
+ return GetCurrentUser(siteUrl, options);
134
+ }
135
+
136
+ return GetJson<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
137
+ { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata })
138
+ .then(user => user)
139
+ .catch<IUserInfo>(() => null);
140
+ }
141
+
142
+ export function GetUserByLoginSync(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): IUserInfo {
143
+ siteUrl = GetSiteUrl(siteUrl);
144
+
145
+ if (isNullOrEmptyString(loginName)) {
146
+ return GetCurrentUserSync(siteUrl, options);
147
+ }
148
+
149
+ let res = GetJsonSync<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
150
+ { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
151
+ if (res.success) {
152
+ let user = res.result;
153
+ return user;
154
+ }
155
+
156
+ return null;
157
+ }
158
+
159
+ function _getEnsureUserRequestUrl(siteUrl: string, loginName: string, expandGroups?: boolean) {
160
+ siteUrl = GetSiteUrl(siteUrl);
161
+
162
+ var url = `${GetRestBaseUrl(siteUrl)}/web/ensureUser(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
163
+ return url;
164
+ }
165
+
166
+ export async function EnsureUser(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
167
+ siteUrl = GetSiteUrl(siteUrl);
168
+
169
+ if (isNullOrEmptyString(userLogin)) return null;
170
+
171
+ return GetJson<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
172
+ { method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
173
+ .then(user => {
174
+ return user;
175
+ })
176
+ .catch<IUserInfo>(() => null);
177
+ }
178
+
179
+ export function EnsureUserSync(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): IUserInfo {
180
+ siteUrl = GetSiteUrl(siteUrl);
181
+
182
+ if (isNullOrEmptyString(userLogin)) return null;
183
+
184
+ let res = GetJsonSync<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
185
+ { method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
186
+ if (res.success) {
187
+ let user = res.result;
188
+ return user;
189
+ }
190
+ return null;
191
+ }
192
+
193
+ export function GetOrEnsureUserByLoginSync(siteUrl: string, key: string, options?: { expandGroups: boolean; }) {
194
+ let userValue = GetUserByLoginSync(siteUrl, key, options);
195
+ if (!userValue) {
196
+ userValue = EnsureUserSync(siteUrl, key, options);
197
+ }
198
+ return userValue;
199
+ }
200
+
201
+ export async function GetSecurityGroupByTitle(siteUrl: string, title: string): Promise<IUserInfo> {
202
+ siteUrl = GetSiteUrl(siteUrl);
203
+ //on premise the title/name of security group could be as domain login
204
+ //for example, 'KWIZCOM\ad_qa_group'
205
+ //split[0] = will contain the domain name (KWIZCOM)
206
+ //split[1] = will contain the title (ad_qa_group)
207
+ //if split[1] is null, then we didn't get a domain login and the split[0] will just contain the title/name (ad_qa_group) of the group
208
+ var split = title.split("\\");
209
+ var groupTitle = (split[1] || split[0]).toLowerCase();
210
+ var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
211
+
212
+ return GetJson<{ value: IUserInfo[]; }>(url, null,
213
+ { method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
214
+ .then(securityGroupsResult => {
215
+ var securityGroup: IUserInfo = null;
216
+ if (securityGroupsResult && securityGroupsResult.value && securityGroupsResult.value.length) {
217
+ //first match the full title and fall back to the split title/name
218
+ securityGroup = securityGroupsResult.value.filter((secGroup) => {
219
+ //this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
220
+ //but will not match when exporting from on premise to online
221
+ return secGroup.Title.toLowerCase() === title.toLowerCase();
222
+ })[0] || securityGroupsResult.value.filter((secGroup) => {
223
+ //this will match settings exported from on premise to online where the title/name of the group changes from 'KWIZCOM\ad_qa_group' to 'AD_QA_GROUP'
224
+ return secGroup.Title.toLowerCase() === groupTitle;
225
+ })[0];
226
+ }
227
+ return securityGroup;
228
+ })
229
+ .catch<IUserInfo>(() => null);
230
+ }
231
+
232
+ export function GetSecurityGroupByTitleSync(siteUrl: string, title: string): IUserInfo {
233
+ siteUrl = GetSiteUrl(siteUrl);
234
+ //on premise the title/name of security group could be as domain login
235
+ //for example, 'KWIZCOM\ad_qa_group'
236
+ //split[0] = will contain the domain name (KWIZCOM)
237
+ //split[1] = will contain the title (ad_qa_group)
238
+ //if split[1] is null, then we didn't get a domain login and the split[0] will just contain the title/name (ad_qa_group) of the group
239
+ var split = title.split("\\");
240
+ var groupTitle = (split[1] || split[0]).toLowerCase();
241
+ var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
242
+
243
+ let securityGroupsResult = GetJsonSync<{ value: IUserInfo[]; }>(url, null,
244
+ { method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
245
+
246
+ if (securityGroupsResult && securityGroupsResult.success) {
247
+ var securityGroup: IUserInfo = null;
248
+ if (securityGroupsResult && securityGroupsResult.result && securityGroupsResult.result.value && securityGroupsResult.result.value.length) {
249
+ //first match the full title and fall back to the split title/name
250
+ securityGroup = securityGroupsResult.result.value.filter((secGroup) => {
251
+ //this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
252
+ //but will not match when exporting from on premise to online
253
+ return secGroup.Title.toLowerCase() === title.toLowerCase();
254
+ })[0] || securityGroupsResult.result.value.filter((secGroup) => {
255
+ //this will match settings exported from on premise to online where the title/name of the group changes from 'KWIZCOM\ad_qa_group' to 'AD_QA_GROUP'
256
+ return secGroup.Title.toLowerCase() === groupTitle;
257
+ })[0];
258
+ }
259
+ return securityGroup;
260
+ }
261
+ return null;
262
+ }
263
+
264
+ function _getGroupRequestUrl(siteUrl: string, groupId: number) {
265
+ siteUrl = GetSiteUrl(siteUrl);
266
+
267
+ var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})?$select=${groupSelect}`;
268
+ return url;
269
+ }
270
+
271
+ function _getGroupUsersRequestUrl(siteUrl: string, groupId: number) {
272
+ siteUrl = GetSiteUrl(siteUrl);
273
+
274
+ var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})/Users?$select=${userSelect}`;
275
+ return url;
276
+ }
277
+
278
+ export async function GetGroup(siteUrl?: string, groupId?: number, options?: {
279
+ expandUsers: boolean;
280
+ refreshCache?: boolean;
281
+ }): Promise<IGroupInfo> {
282
+ siteUrl = GetSiteUrl(siteUrl);
283
+
284
+ return GetJson<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
285
+ {
286
+ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
287
+ allowCache: !options || options.refreshCache !== true
288
+ })
289
+ .then(async group => {
290
+ if (group) {
291
+ group.PrincipalType = PrincipalType.SharePointGroup;
292
+ group.LoginName = group.Title;
293
+ if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
294
+ let users = await GetJson<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, {
295
+ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
296
+ allowCache: !options || options.refreshCache !== true
297
+ });
298
+ group.Users = users && users.value;
299
+ }
300
+ }
301
+ return group;
302
+ })
303
+ .catch<IGroupInfo>(() => null);
304
+ }
305
+
306
+ export function GetGroupSync(siteUrl?: string, groupId?: number, options?: { expandUsers: boolean; }): IGroupInfo {
307
+ siteUrl = GetSiteUrl(siteUrl);
308
+
309
+ let res = GetJsonSync<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
310
+ { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
311
+ if (res.success) {
312
+ let group = res.result;
313
+ if (group) {
314
+ group.PrincipalType = PrincipalType.SharePointGroup;
315
+ group.LoginName = group.Title;
316
+ if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
317
+ let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
318
+ group.Users = users.success && users.result && users.result.value;
319
+ }
320
+ }
321
+ return group;
322
+ }
323
+ else return null;
324
+ }
325
+
326
+ function _getGroupsRequestUrl(siteUrl: string) {
327
+ siteUrl = GetSiteUrl(siteUrl);
328
+ var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups?$select=${groupSelect}`;
329
+ return url;
330
+ }
331
+
332
+ function _getGroupByNameRequestUrl(siteUrl: string, groupName: string) {
333
+ var url = `${_getGroupsRequestUrl(siteUrl)}&$filter=LoginName eq '${encodeURIComponentEX(groupName, { singleQuoteMultiplier: 2 })}'`;
334
+ return url;
335
+ }
336
+
337
+ export async function GetGroupByName(siteUrl: string, groupName: string, options?: {
338
+ expandUsers: boolean;
339
+ refreshCache?: boolean;
340
+ }): Promise<IGroupInfo> {
341
+ siteUrl = GetSiteUrl(siteUrl);
342
+
343
+ let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
344
+ { ...shortLocalCache, allowCache: !options || options.refreshCache !== true });
345
+
346
+ if (res) {
347
+ let group = res && res.d && res.d.results && res.d.results[0];
348
+ if (group) {
349
+ group.PrincipalType = PrincipalType.SharePointGroup;
350
+ group.LoginName = group.Title;
351
+ if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
352
+ let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, {
353
+ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
354
+ allowCache: !options || options.refreshCache !== true
355
+ });
356
+ group.Users = users.success && users.result && users.result.value;
357
+ }
358
+ }
359
+ return group;
360
+ }
361
+ else return null;
362
+ }
363
+
364
+ export function GetGroupByNameSync(siteUrl: string, groupName: string, options?: { expandUsers: boolean; }): IGroupInfo {
365
+ siteUrl = GetSiteUrl(siteUrl);
366
+
367
+ let res = GetJsonSync<{ value: IGroupInfo[]; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
368
+ { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
369
+ if (res.success) {
370
+ let group = res.result && res.result.value && res.result.value[0];
371
+ if (group) {
372
+ group.PrincipalType = PrincipalType.SharePointGroup;
373
+ group.LoginName = group.Title;
374
+ if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
375
+ let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
376
+ group.Users = users.success && users.result && users.result.value;
377
+ }
378
+ }
379
+ return group;
380
+ }
381
+ else return null;
382
+ }
383
+
384
+ export async function GetSiteGroups(siteUrl: string, refreshCache?: boolean) {
385
+ siteUrl = GetSiteUrl(siteUrl);
386
+ let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupsRequestUrl(siteUrl), null,
387
+ { ...shortLocalCache, allowCache: refreshCache !== true });
388
+
389
+ if (res) {
390
+ let groups = res && res.d && res.d.results || [];
391
+ groups.forEach(g => {
392
+ g.PrincipalType = PrincipalType.SharePointGroup;
393
+ g.LoginName = g.Title;
394
+ });
395
+ return groups;
396
+ }
397
+ else return [];
398
+ }
399
+
400
+ export function GetInfoFromSPPeoplePickerControlFormEntity(entity: ISPPeoplePickerControlFormEntity): IUserInfo | IGroupInfo {
401
+ if (IsSPPeoplePickerControlFormEntity(entity)) {
402
+ var principalType = getPrincipalTypeFromPickerEntity(entity);
403
+ if (isNullOrUndefined(principalType)) {
404
+ let userValue = GetOrEnsureUserByLoginSync(null, entity.Key);
405
+ if (userValue) {
406
+ return userValue;
407
+ }
408
+ let groupValue = GetGroupByNameSync(null, entity.Key);
409
+ if (groupValue) {
410
+ return groupValue;
411
+ }
412
+ } else if (principalType === PrincipalType.SharePointGroup) {
413
+ return GetGroupByNameSync(null, entity.Key);
414
+ } else {
415
+ return GetOrEnsureUserByLoginSync(null, entity.Key);
416
+ }
417
+ }
418
+ return null;
419
+ }
420
+
421
+ export async function CreateSiteGroup(siteUrl: string, info: { name: string, description: string }): Promise<ISiteGroupInfo> {
422
+ let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups`;
423
+ let createGroup = await GetJson<{ d: ISiteGroupInfo }>(url, jsonStringify({
424
+ __metadata: {
425
+ type: "SP.Group"
426
+ },
427
+ Title: info.name,
428
+ Description: info.description
429
+ }), { allowCache: false });
430
+ return createGroup.d;
431
+ }
432
+ export async function AddUserToGroup(siteUrl: string, groupId: number, userIdOrLogin: number | string): Promise<void> {
433
+ let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups(${groupId})/users`;
434
+ if (isNumber(userIdOrLogin)) {
435
+ let ensured = await GetUser(siteUrl, userIdOrLogin);
436
+ userIdOrLogin = ensured.LoginName;
437
+ }
438
+
439
+ await GetJson(url, jsonStringify({
440
+ LoginName: userIdOrLogin
441
+ }), { allowCache: false, jsonMetadata: jsonTypes.nometadata });
442
+
443
+ }
444
+ export async function RemoveUserFromGroup(siteUrl: string, groupId: number, userId: number): Promise<void> {
445
+ let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups(${groupId})/users/removeById(${userId})`;
446
+ await GetJson(url, null, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata });
447
+ }
448
+
449
+ export async function SetGroupOwner(siteUrl: string, groupId: number, ownerId: number, ownerIsAGroup?: boolean) {
450
+ //https://github.com/SharePoint/sp-dev-docs/issues/5031#issuecomment-594710013
451
+ //if owner is a group - rest API doens't work.
452
+ if (ownerIsAGroup !== true) {
453
+ let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById('${groupId}')/SetUserAsOwner(${ownerId})`;
454
+ try {
455
+ await GetJson<{ 'odata.null': true }>(url, null, { jsonMetadata: jsonTypes.nometadata, method: "POST" });
456
+ return true;
457
+ } catch (e) {
458
+ logger.error(`SetGroupOwner ${groupId} ${ownerId} error:`);
459
+ logger.error(e);
460
+ return false;
461
+ }
462
+ }
463
+ else {
464
+ try {
465
+ let soapUrl = `${GetSiteUrl(siteUrl)}_vti_bin/client.svc/ProcessQuery`;
466
+ let siteId = await GetSiteId(siteUrl);
467
+ let serviceJSONResponse = await GetJson<{ ErrorInfo?: string }[]>(soapUrl, `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
468
+ <Actions>
469
+ <SetProperty Id="1" ObjectPathId="2" Name="Owner">
470
+ <Parameter ObjectPathId="3" />
471
+ </SetProperty>
472
+ <Method Name="Update" Id="4" ObjectPathId="2" />
473
+ </Actions>
474
+ <ObjectPaths>
475
+ <Identity Id="2" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${groupId}" />
476
+ <Identity Id="3" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${ownerId}" />
477
+ </ObjectPaths>
478
+ </Request>`, {
479
+ headers: {
480
+ Accept: jsonTypes.standard,
481
+ "content-type": contentTypes.xml
482
+ }
483
+ });
484
+ //logger.json(serviceJSONResponse, "soap result");
485
+ return isNullOrEmptyArray(serviceJSONResponse) || isNullOrEmptyString(serviceJSONResponse[0].ErrorInfo);
486
+ } catch (e) {
487
+ logger.error(`SetGroupOwner via SOAP ${ownerId} ${ownerId} error:`);
488
+ logger.error(e);
489
+ return false;
490
+ }
491
+ }
492
492
  }