@kwiz/common 1.0.2 → 1.0.3
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.
- package/package.json +1 -1
- package/src/_dependencies.ts +0 -2
- package/src/helpers/Guid.ts +0 -182
- package/src/helpers/_dependencies.ts +0 -3
- package/src/helpers/base64.ts +0 -174
- package/src/helpers/browser.test.js +0 -9
- package/src/helpers/browser.ts +0 -1299
- package/src/helpers/browserinfo.ts +0 -293
- package/src/helpers/collections.base.test.js +0 -26
- package/src/helpers/collections.base.ts +0 -439
- package/src/helpers/collections.ts +0 -108
- package/src/helpers/color.ts +0 -55
- package/src/helpers/cookies.ts +0 -55
- package/src/helpers/date.test.js +0 -97
- package/src/helpers/date.ts +0 -163
- package/src/helpers/debug.ts +0 -187
- package/src/helpers/emails.ts +0 -7
- package/src/helpers/eval.ts +0 -5
- package/src/helpers/file.test.js +0 -51
- package/src/helpers/file.ts +0 -59
- package/src/helpers/flatted.ts +0 -150
- package/src/helpers/functions.ts +0 -17
- package/src/helpers/graph/calendar.types.ts +0 -11
- package/src/helpers/http.ts +0 -70
- package/src/helpers/images.ts +0 -23
- package/src/helpers/index.ts +0 -29
- package/src/helpers/json.ts +0 -39
- package/src/helpers/md5.ts +0 -190
- package/src/helpers/objects.test.js +0 -27
- package/src/helpers/objects.ts +0 -256
- package/src/helpers/promises.test.js +0 -18
- package/src/helpers/promises.ts +0 -102
- package/src/helpers/random.ts +0 -27
- package/src/helpers/scheduler/scheduler.test.js +0 -104
- package/src/helpers/scheduler/scheduler.ts +0 -132
- package/src/helpers/sharepoint.ts +0 -681
- package/src/helpers/strings.test.js +0 -43
- package/src/helpers/strings.ts +0 -288
- package/src/helpers/typecheckers.test.js +0 -35
- package/src/helpers/typecheckers.ts +0 -263
- package/src/helpers/url.test.js +0 -18
- package/src/helpers/url.ts +0 -202
- package/src/helpers/urlhelper.ts +0 -104
- package/src/index.ts +0 -24
- package/src/types/common.types.ts +0 -16
- package/src/types/flatted.types.ts +0 -60
- package/src/types/globals.types.ts +0 -7
- package/src/types/graph/calendar.types.ts +0 -81
- package/src/types/graph/index.ts +0 -1
- package/src/types/index.ts +0 -12
- package/src/types/knownscript.types.ts +0 -19
- package/src/types/libs/datajs.types.ts +0 -29
- package/src/types/libs/ics.types.ts +0 -31
- package/src/types/libs/index.ts +0 -4
- package/src/types/libs/msal.types.ts +0 -79
- package/src/types/locales.ts +0 -125
- package/src/types/localstoragecache.types.ts +0 -9
- package/src/types/moment.ts +0 -7
- package/src/types/regex.types.ts +0 -17
- package/src/types/rest.types.ts +0 -94
- package/src/types/sharepoint.types.ts +0 -1413
- package/src/types/sharepoint.utils.types.ts +0 -246
- package/src/utils/_dependencies.ts +0 -4
- package/src/utils/base64.ts +0 -27
- package/src/utils/consolelogger.ts +0 -315
- package/src/utils/date.ts +0 -36
- package/src/utils/emails.ts +0 -25
- package/src/utils/index.ts +0 -13
- package/src/utils/knownscript.ts +0 -280
- package/src/utils/localstoragecache.ts +0 -419
- package/src/utils/rest.ts +0 -465
- package/src/utils/script.ts +0 -168
- package/src/utils/sharepoint.rest/common.ts +0 -149
- package/src/utils/sharepoint.rest/date.ts +0 -61
- package/src/utils/sharepoint.rest/file.folder.ts +0 -368
- package/src/utils/sharepoint.rest/index.ts +0 -11
- package/src/utils/sharepoint.rest/item.ts +0 -456
- package/src/utils/sharepoint.rest/list.ts +0 -1144
- package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +0 -750
- package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +0 -265
- package/src/utils/sharepoint.rest/listutils/common.ts +0 -202
- package/src/utils/sharepoint.rest/user-search.ts +0 -254
- package/src/utils/sharepoint.rest/user.ts +0 -447
- package/src/utils/sharepoint.rest/web.ts +0 -1031
- package/src/utils/sod.ts +0 -193
- package/tsconfig.json +0 -21
|
@@ -1,447 +0,0 @@
|
|
|
1
|
-
import { IGroupInfo, ISPPeoplePickerControlFormEntity, IUserInfo, IsSPPeoplePickerControlFormEntity, PrincipalType, encodeURIComponentEX, getPrincipalTypeFromPickerEntity, isNullOrEmptyString, isNullOrNaN, isNullOrUndefined, isNumber, jsonStringify, jsonTypes } from "../_dependencies";
|
|
2
|
-
//import { ConsoleLogger } from "../consolelogger";
|
|
3
|
-
import { GetJson, GetJsonSync, longLocalCache, shortLocalCache } from "../rest";
|
|
4
|
-
import { GetRestBaseUrl, GetSiteUrl } from "./common";
|
|
5
|
-
|
|
6
|
-
//const logger = ConsoleLogger.get("utils/sharepoint/user");
|
|
7
|
-
var __currentUserId: number = null;
|
|
8
|
-
const groupSelect = "Id,Title,Description,CanCurrentUserViewMembership,OnlyAllowMembersViewMembership,IsHiddenInUI,OwnerTitle";
|
|
9
|
-
const userSelect = "PrincipalType,Id,LoginName,UserPrincipalName,Title,IsSiteAdmin,Email";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/** Get user login name */
|
|
13
|
-
export function GetUserLoginName(siteUrl?: string): Promise<string> {
|
|
14
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
15
|
-
|
|
16
|
-
if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
|
|
17
|
-
//issue 6309 _spPageContextInfo.userLoginName is wrong for external users
|
|
18
|
-
return Promise.resolve(_spPageContextInfo.userPrincipalName);
|
|
19
|
-
|
|
20
|
-
return GetJson<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache })
|
|
21
|
-
.then(r => r.d.LoginName)
|
|
22
|
-
.catch<string>(() => null);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** Get user login name syncronously */
|
|
26
|
-
export function GetUserLoginNameSync(siteUrl?: string): string {
|
|
27
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
28
|
-
|
|
29
|
-
if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
|
|
30
|
-
//issue 6309 _spPageContextInfo.userLoginName is wrong for external users
|
|
31
|
-
return _spPageContextInfo.userPrincipalName;
|
|
32
|
-
|
|
33
|
-
let res = GetJsonSync<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache });
|
|
34
|
-
if (res.success)
|
|
35
|
-
return res.result.d.LoginName;
|
|
36
|
-
else return null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function _getCurrentUserRequestUrl(siteUrl: string, expandGroups: boolean) {
|
|
40
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
41
|
-
|
|
42
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/currentUser${expandGroups ? '?$expand=Groups' : ''}`;
|
|
43
|
-
return url;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export async function GetCurrentUser(siteUrl?: string, options?: { expandGroups: boolean; refreshCache?: boolean; }): Promise<IUserInfo> {
|
|
47
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
48
|
-
|
|
49
|
-
return GetJson<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
|
|
50
|
-
{
|
|
51
|
-
...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
|
|
52
|
-
allowCache: !options || options.refreshCache !== true
|
|
53
|
-
})
|
|
54
|
-
.then(user => {
|
|
55
|
-
if (user)
|
|
56
|
-
__currentUserId = user.Id;
|
|
57
|
-
return user;
|
|
58
|
-
})
|
|
59
|
-
.catch<IUserInfo>(() => null);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function GetCurrentUserSync(siteUrl?: string, options?: { expandGroups: boolean; }): IUserInfo {
|
|
63
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
64
|
-
|
|
65
|
-
let res = GetJsonSync<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
|
|
66
|
-
{ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
|
|
67
|
-
if (res.success) {
|
|
68
|
-
let user = res.result;
|
|
69
|
-
if (user)
|
|
70
|
-
__currentUserId = user.Id;
|
|
71
|
-
|
|
72
|
-
return user;
|
|
73
|
-
}
|
|
74
|
-
else return null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function _getUserRequestUrl(siteUrl: string, userId: number, expandGroups: boolean) {
|
|
78
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
79
|
-
|
|
80
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/GetUserById(${userId})${expandGroups ? '?expand=Groups' : ''}`;
|
|
81
|
-
return url;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export async function GetUser(siteUrl?: string, userId?: number, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
|
|
85
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
86
|
-
|
|
87
|
-
if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUser(siteUrl, options);
|
|
88
|
-
return GetJson<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null, {
|
|
89
|
-
...shortLocalCache, jsonMetadata: jsonTypes.nometadata
|
|
90
|
-
}).then(user => {
|
|
91
|
-
return user;
|
|
92
|
-
}).catch<IUserInfo>(() => null);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function GetUserSync(siteUrl?: string, userId?: number, options?: { expandGroups: boolean; }): IUserInfo {
|
|
96
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
97
|
-
|
|
98
|
-
if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUserSync(siteUrl, options);
|
|
99
|
-
|
|
100
|
-
let res = GetJsonSync<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null,
|
|
101
|
-
{ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
|
|
102
|
-
if (res.success) {
|
|
103
|
-
let user = res.result;
|
|
104
|
-
return user;
|
|
105
|
-
}
|
|
106
|
-
else return null;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function _getUserByLoginNameRequestUrl(siteUrl: string, loginName: string, expandGroups: boolean) {
|
|
110
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
111
|
-
|
|
112
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/siteUsers/getByLoginName(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
|
|
113
|
-
return url;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export async function GetUserByLogin(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
|
|
117
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
118
|
-
|
|
119
|
-
if (isNullOrEmptyString(loginName)) {
|
|
120
|
-
return GetCurrentUser(siteUrl, options);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return GetJson<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
|
|
124
|
-
{ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata })
|
|
125
|
-
.then(user => user)
|
|
126
|
-
.catch<IUserInfo>(() => null);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function GetUserByLoginSync(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): IUserInfo {
|
|
130
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
131
|
-
|
|
132
|
-
if (isNullOrEmptyString(loginName)) {
|
|
133
|
-
return GetCurrentUserSync(siteUrl, options);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
let res = GetJsonSync<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
|
|
137
|
-
{ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
|
|
138
|
-
if (res.success) {
|
|
139
|
-
let user = res.result;
|
|
140
|
-
return user;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function _getEnsureUserRequestUrl(siteUrl: string, loginName: string, expandGroups?: boolean) {
|
|
147
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
148
|
-
|
|
149
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/ensureUser(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
|
|
150
|
-
return url;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export async function EnsureUser(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
|
|
154
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
155
|
-
|
|
156
|
-
if (isNullOrEmptyString(userLogin)) return null;
|
|
157
|
-
|
|
158
|
-
return GetJson<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
|
|
159
|
-
{ method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
|
|
160
|
-
.then(user => {
|
|
161
|
-
return user;
|
|
162
|
-
})
|
|
163
|
-
.catch<IUserInfo>(() => null);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function EnsureUserSync(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): IUserInfo {
|
|
167
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
168
|
-
|
|
169
|
-
if (isNullOrEmptyString(userLogin)) return null;
|
|
170
|
-
|
|
171
|
-
let res = GetJsonSync<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
|
|
172
|
-
{ method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
|
|
173
|
-
if (res.success) {
|
|
174
|
-
let user = res.result;
|
|
175
|
-
return user;
|
|
176
|
-
}
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export function GetOrEnsureUserByLoginSync(siteUrl: string, key: string, options?: { expandGroups: boolean; }) {
|
|
181
|
-
let userValue = GetUserByLoginSync(siteUrl, key, options);
|
|
182
|
-
if (!userValue) {
|
|
183
|
-
userValue = EnsureUserSync(siteUrl, key, options);
|
|
184
|
-
}
|
|
185
|
-
return userValue;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export async function GetSecurityGroupByTitle(siteUrl: string, title: string): Promise<IUserInfo> {
|
|
189
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
190
|
-
//on premise the title/name of security group could be as domain login
|
|
191
|
-
//for example, 'KWIZCOM\ad_qa_group'
|
|
192
|
-
//split[0] = will contain the domain name (KWIZCOM)
|
|
193
|
-
//split[1] = will contain the title (ad_qa_group)
|
|
194
|
-
//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
|
|
195
|
-
var split = title.split("\\");
|
|
196
|
-
var groupTitle = (split[1] || split[0]).toLowerCase();
|
|
197
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
|
|
198
|
-
|
|
199
|
-
return GetJson<{ value: IUserInfo[]; }>(url, null,
|
|
200
|
-
{ method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
|
|
201
|
-
.then(securityGroupsResult => {
|
|
202
|
-
var securityGroup: IUserInfo = null;
|
|
203
|
-
if (securityGroupsResult && securityGroupsResult.value && securityGroupsResult.value.length) {
|
|
204
|
-
//first match the full title and fall back to the split title/name
|
|
205
|
-
securityGroup = securityGroupsResult.value.filter((secGroup) => {
|
|
206
|
-
//this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
|
|
207
|
-
//but will not match when exporting from on premise to online
|
|
208
|
-
return secGroup.Title.toLowerCase() === title.toLowerCase();
|
|
209
|
-
})[0] || securityGroupsResult.value.filter((secGroup) => {
|
|
210
|
-
//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'
|
|
211
|
-
return secGroup.Title.toLowerCase() === groupTitle;
|
|
212
|
-
})[0];
|
|
213
|
-
}
|
|
214
|
-
return securityGroup;
|
|
215
|
-
})
|
|
216
|
-
.catch<IUserInfo>(() => null);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export function GetSecurityGroupByTitleSync(siteUrl: string, title: string): IUserInfo {
|
|
220
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
221
|
-
//on premise the title/name of security group could be as domain login
|
|
222
|
-
//for example, 'KWIZCOM\ad_qa_group'
|
|
223
|
-
//split[0] = will contain the domain name (KWIZCOM)
|
|
224
|
-
//split[1] = will contain the title (ad_qa_group)
|
|
225
|
-
//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
|
|
226
|
-
var split = title.split("\\");
|
|
227
|
-
var groupTitle = (split[1] || split[0]).toLowerCase();
|
|
228
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
|
|
229
|
-
|
|
230
|
-
let securityGroupsResult = GetJsonSync<{ value: IUserInfo[]; }>(url, null,
|
|
231
|
-
{ method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
|
|
232
|
-
|
|
233
|
-
if (securityGroupsResult && securityGroupsResult.success) {
|
|
234
|
-
var securityGroup: IUserInfo = null;
|
|
235
|
-
if (securityGroupsResult && securityGroupsResult.result && securityGroupsResult.result.value && securityGroupsResult.result.value.length) {
|
|
236
|
-
//first match the full title and fall back to the split title/name
|
|
237
|
-
securityGroup = securityGroupsResult.result.value.filter((secGroup) => {
|
|
238
|
-
//this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
|
|
239
|
-
//but will not match when exporting from on premise to online
|
|
240
|
-
return secGroup.Title.toLowerCase() === title.toLowerCase();
|
|
241
|
-
})[0] || securityGroupsResult.result.value.filter((secGroup) => {
|
|
242
|
-
//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'
|
|
243
|
-
return secGroup.Title.toLowerCase() === groupTitle;
|
|
244
|
-
})[0];
|
|
245
|
-
}
|
|
246
|
-
return securityGroup;
|
|
247
|
-
}
|
|
248
|
-
return null;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function _getGroupRequestUrl(siteUrl: string, groupId: number) {
|
|
252
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
253
|
-
|
|
254
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})?$select=${groupSelect}`;
|
|
255
|
-
return url;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function _getGroupUsersRequestUrl(siteUrl: string, groupId: number) {
|
|
259
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
260
|
-
|
|
261
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})/Users?$select=${userSelect}`;
|
|
262
|
-
return url;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export async function GetGroup(siteUrl?: string, groupId?: number, options?: {
|
|
266
|
-
expandUsers: boolean;
|
|
267
|
-
refreshCache?: boolean;
|
|
268
|
-
}): Promise<IGroupInfo> {
|
|
269
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
270
|
-
|
|
271
|
-
return GetJson<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
|
|
272
|
-
{
|
|
273
|
-
...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
|
|
274
|
-
allowCache: !options || options.refreshCache !== true
|
|
275
|
-
})
|
|
276
|
-
.then(async group => {
|
|
277
|
-
if (group) {
|
|
278
|
-
group.PrincipalType = PrincipalType.SharePointGroup;
|
|
279
|
-
group.LoginName = group.Title;
|
|
280
|
-
if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
|
|
281
|
-
let users = await GetJson<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, {
|
|
282
|
-
...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
|
|
283
|
-
allowCache: !options || options.refreshCache !== true
|
|
284
|
-
});
|
|
285
|
-
group.Users = users && users.value;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
return group;
|
|
289
|
-
})
|
|
290
|
-
.catch<IGroupInfo>(() => null);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
export function GetGroupSync(siteUrl?: string, groupId?: number, options?: { expandUsers: boolean; }): IGroupInfo {
|
|
294
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
295
|
-
|
|
296
|
-
let res = GetJsonSync<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
|
|
297
|
-
{ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
|
|
298
|
-
if (res.success) {
|
|
299
|
-
let group = res.result;
|
|
300
|
-
if (group) {
|
|
301
|
-
group.PrincipalType = PrincipalType.SharePointGroup;
|
|
302
|
-
group.LoginName = group.Title;
|
|
303
|
-
if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
|
|
304
|
-
let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
|
|
305
|
-
group.Users = users.success && users.result && users.result.value;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
return group;
|
|
309
|
-
}
|
|
310
|
-
else return null;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
function _getGroupsRequestUrl(siteUrl: string) {
|
|
314
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
315
|
-
var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups?$select=${groupSelect}`;
|
|
316
|
-
return url;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function _getGroupByNameRequestUrl(siteUrl: string, groupName: string) {
|
|
320
|
-
var url = `${_getGroupsRequestUrl(siteUrl)}&$filter=LoginName eq '${encodeURIComponentEX(groupName, { singleQuoteMultiplier: 2 })}'`;
|
|
321
|
-
return url;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export async function GetGroupByName(siteUrl: string, groupName: string, options?: {
|
|
325
|
-
expandUsers: boolean;
|
|
326
|
-
refreshCache?: boolean;
|
|
327
|
-
}): Promise<IGroupInfo> {
|
|
328
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
329
|
-
|
|
330
|
-
let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
|
|
331
|
-
{ ...shortLocalCache, allowCache: !options || options.refreshCache !== true });
|
|
332
|
-
|
|
333
|
-
if (res) {
|
|
334
|
-
let group = res && res.d && res.d.results && res.d.results[0];
|
|
335
|
-
if (group) {
|
|
336
|
-
group.PrincipalType = PrincipalType.SharePointGroup;
|
|
337
|
-
group.LoginName = group.Title;
|
|
338
|
-
if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
|
|
339
|
-
let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, {
|
|
340
|
-
...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
|
|
341
|
-
allowCache: !options || options.refreshCache !== true
|
|
342
|
-
});
|
|
343
|
-
group.Users = users.success && users.result && users.result.value;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
return group;
|
|
347
|
-
}
|
|
348
|
-
else return null;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
export function GetGroupByNameSync(siteUrl: string, groupName: string, options?: { expandUsers: boolean; }): IGroupInfo {
|
|
352
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
353
|
-
|
|
354
|
-
let res = GetJsonSync<{ value: IGroupInfo[]; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
|
|
355
|
-
{ ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
|
|
356
|
-
if (res.success) {
|
|
357
|
-
let group = res.result && res.result.value && res.result.value[0];
|
|
358
|
-
if (group) {
|
|
359
|
-
group.PrincipalType = PrincipalType.SharePointGroup;
|
|
360
|
-
group.LoginName = group.Title;
|
|
361
|
-
if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
|
|
362
|
-
let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
|
|
363
|
-
group.Users = users.success && users.result && users.result.value;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
return group;
|
|
367
|
-
}
|
|
368
|
-
else return null;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
export async function GetSiteGroups(siteUrl: string, refreshCache?: boolean) {
|
|
372
|
-
siteUrl = GetSiteUrl(siteUrl);
|
|
373
|
-
let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupsRequestUrl(siteUrl), null,
|
|
374
|
-
{ ...shortLocalCache, allowCache: refreshCache !== true });
|
|
375
|
-
|
|
376
|
-
if (res) {
|
|
377
|
-
let groups = res && res.d && res.d.results || [];
|
|
378
|
-
groups.forEach(g => {
|
|
379
|
-
g.PrincipalType = PrincipalType.SharePointGroup;
|
|
380
|
-
g.LoginName = g.Title;
|
|
381
|
-
});
|
|
382
|
-
return groups;
|
|
383
|
-
}
|
|
384
|
-
else return [];
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
export function GetInfoFromSPPeoplePickerControlFormEntity(entity: ISPPeoplePickerControlFormEntity): IUserInfo | IGroupInfo {
|
|
388
|
-
if (IsSPPeoplePickerControlFormEntity(entity)) {
|
|
389
|
-
var principalType = getPrincipalTypeFromPickerEntity(entity);
|
|
390
|
-
if (isNullOrUndefined(principalType)) {
|
|
391
|
-
let userValue = GetOrEnsureUserByLoginSync(null, entity.Key);
|
|
392
|
-
if (userValue) {
|
|
393
|
-
return userValue;
|
|
394
|
-
}
|
|
395
|
-
let groupValue = GetGroupByNameSync(null, entity.Key);
|
|
396
|
-
if (groupValue) {
|
|
397
|
-
return groupValue;
|
|
398
|
-
}
|
|
399
|
-
} else if (principalType === PrincipalType.SharePointGroup) {
|
|
400
|
-
return GetGroupByNameSync(null, entity.Key);
|
|
401
|
-
} else {
|
|
402
|
-
return GetOrEnsureUserByLoginSync(null, entity.Key);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
return null;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
interface ICreateSiteGroupResult {
|
|
409
|
-
Id: number,
|
|
410
|
-
IsHiddenInUI: boolean,//false,
|
|
411
|
-
LoginName: string,
|
|
412
|
-
Title: string,
|
|
413
|
-
PrincipalType: PrincipalType.SharePointGroup,
|
|
414
|
-
AllowMembersEditMembership: boolean,//false,
|
|
415
|
-
AllowRequestToJoinLeave: boolean,//false,
|
|
416
|
-
AutoAcceptRequestToJoinLeave: boolean,//false,
|
|
417
|
-
Description: string,//"Admins can change site settings, permissions, perform updates, import and export",
|
|
418
|
-
OnlyAllowMembersViewMembership: boolean,//true,
|
|
419
|
-
OwnerTitle: string//"Shai Petel"
|
|
420
|
-
}
|
|
421
|
-
export async function CreateSiteGroup(siteUrl: string, info: { name: string, description: string }): Promise<ICreateSiteGroupResult> {
|
|
422
|
-
let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups`;
|
|
423
|
-
let createGroup = await GetJson<{ d: ICreateSiteGroupResult }>(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
|
-
}
|