forked from sadhack/spcrud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spcrud.ts
411 lines (370 loc) · 14.3 KB
/
spcrud.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* Library with Angular 2+ operations for CRUD operations to SharePoint 2013/2016/Online lists over REST api
*
* Contains 6 core functions and other misc helper functions
*
* 1) Create - add item to List
* 2) Read - find all items or single item from List
* 3) Update - update item in List
* 4) Delete - delete item in List
* 5) jsonRead - read JSON to List
* 6) jsonWrite - write JSON to List ("upsert" = add if missing, update if exists)
*
* NOTE - 5 and 6 require the target SharePoint List to have two columns: "Title" (indexed) and "JSON" (mult-text). These are
* intendend to save JSON objects for JS internal application needs. For example, saving user preferences to a "JSON-Settings" list
* where one row is created per user (Title = current user Login) and JSON multi-text field holds the JSON blob.
* Simple and flexible way to save data for many scenarios.
*
* @spjeff
* http://spjeff.com
*
* version 0.2.10
* last updated 11-24-2018
*
*/
import { Injectable } from '@angular/core';
// RxJS dependency
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class Spcrud {
// Data
jsonHeader = 'application/json; odata=verbose';
headers = new Headers({ 'Content-Type': this.jsonHeader, 'Accept': this.jsonHeader });
options = new RequestOptions({ headers: this.headers });
baseUrl: String;
apiUrl: String;
currentUser: String;
login: String;
constructor(private http: Http) {
this.setBaseUrl(null);
}
// HTTP Error handling
private handleError(error: Response | any) {
// Generic from https://angular.io/docs/ts/latest/guide/server-communication.html
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status || ''} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
// String ends with
private endsWith(str: string, suffix: string) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
// ----------SHAREPOINT GENERAL----------
// Set base working URL path
setBaseUrl(webUrl?: string) {
if (webUrl) {
// user provided target Web URL
this.baseUrl = webUrl;
} else {
// default local SharePoint context
const ctx = window['_spPageContextInfo'];
if (ctx) {
this.baseUrl = ctx.webAbsoluteUrl;
}
}
// Default to local web URL
this.apiUrl = this.baseUrl + '/_api/web/lists/GetByTitle(\'{0}\')/items';
// Request digest
const el = document.querySelector('#__REQUESTDIGEST');
if (el) {
// Digest local to ASPX page
// this.headers.delete('X-RequestDigest');
this.headers.append('X-RequestDigest', el.nodeValue);
}
}
// Refresh digest token
refreshDigest(): Promise<any> {
const svc = this;
return this.http.post(this.baseUrl + '/_api/contextinfo', null, this.options).toPromise().then(function (res: Response) {
svc.headers.delete('X-RequestDigest');
svc.headers.append('X-RequestDigest', res.json().d.GetContextWebInformation.FormDigestValue);
});
}
// Send email
sendMail(to: string, ffrom: string, subj: string, body: string): Promise<any> {
// Append metadata
const tos: string[] = to.split(',');
const recip: string[] = (tos instanceof Array) ? tos : [tos];
const message = {
'properties': {
'__metadata': {
'type': 'SP.Utilities.EmailProperties'
},
'To': {
'results': recip
},
'From': ffrom,
'Subject': subj,
'Body': body
}
};
const url = this.baseUrl + '/_api/SP.Utilities.Utility.SendEmail';
const data = JSON.stringify(message);
return this.http.post(url, data, this.options).toPromise();
}
// ----------SHAREPOINT USER PROFILES----------
// Lookup SharePoint current web user
getCurrentUser(): Promise<any> {
const url = this.baseUrl + '/_api/web/currentuser?$expand=Groups';
return this.http.get(url, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Lookup my SharePoint profile
getMyProfile(): Promise<any> {
const url = this.baseUrl + '/_api/SP.UserProfiles.PeopleManager/GetMyProperties?select=*';
return this.http.get(url, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Lookup any SharePoint profile
getProfile(login: string): Promise<any> {
const url = this.baseUrl + '/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v=\'' + login + '\'&select=*';
return this.http.get(url, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Lookup any SharePoint UserInfo
getUserInfo(id: string): Promise<any> {
const url = this.baseUrl + '/_api/web/getUserById(' + id + ')';
return this.http.get(url).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Ensure SPUser exists in target web
ensureUser(login: string): Promise<any> {
const url = this.baseUrl + '/_api/web/ensureuser';
return this.http.post(url, login, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// ----------SHAREPOINT LIST AND FIELDS----------
// Create list
createList(title: string, baseTemplate: string, description: string): Promise<any> {
const data = {
'__metadata': { 'type': 'SP.List' },
'BaseTemplate': baseTemplate,
'Description': description,
'Title': title
};
const url = this.baseUrl + '/_api/web/lists';
return this.http.post(url, data, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Create field
createField(listTitle: string, fieldName: string, fieldType: string): Promise<any> {
const data = {
'__metadata': { 'type': 'SP.Field' },
'Type': fieldType,
'Title': fieldName
};
const url = this.baseUrl + '/_api/web/lists/GetByTitle(\'' + listTitle + '\')/fields';
return this.http.post(url, data, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// ----------SHAREPOINT FILES AND FOLDERS----------
// Create folder
createFolder(folderUrl: string): Promise<any> {
const data = {
'__metadata': {
'type': 'SP.Folder'
},
'ServerRelativeUrl': folderUrl
};
const url = this.baseUrl + '/_api/web/folders';
return this.http.post(url, data, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Upload file to folder
// https://kushanlahiru.wordpress.com/2016/05/14/file-attach-to-sharepoint-2013-list-custom-using-angular-js-via-rest-api/
// http://stackoverflow.com/questions/17063000/ng-model-for-input-type-file
// var binary = new Uint8Array(FileReader.readAsArrayBuffer(file[0]));
uploadFile(folderUrl: string, fileName: string, binary: any): Promise<any> {
const url = this.baseUrl + '/_api/web/GetFolderByServerRelativeUrl(\''
+ folderUrl + '\')/files/add(overwrite=true, url=\'' + fileName + '\')';
return this.http.post(url, binary, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Upload attachment to item
uploadAttach(listName: string, id: string, fileName: string, binary: any, overwrite?: boolean): Promise<any> {
let url = this.baseUrl + '/_api/web/lists/GetByTitle(\'' + listName + '\')/items(' + id;
const options = this.options;
if (overwrite) {
// Append HTTP header PUT for UPDATE scenario
options.headers.append('X-HTTP-Method', 'PUT');
url += ')/AttachmentFiles(\'' + fileName + '\')/$value';
} else {
// CREATE scenario
url += ')/AttachmentFiles/add(FileName=\'' + fileName + '\')';
}
return this.http.post(url, binary, options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Get attachment for item
getAttach(listName: string, id: string): Promise<any> {
const url = this.baseUrl + '/_api/web/lists/GetByTitle(\'' + listName + '\')/items(' + id + ')/AttachmentFiles';
return this.http.get(url, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Copy file
copyFile(sourceUrl: string, destinationUrl: string): Promise<any> {
const url = this.baseUrl + '/_api/web/GetFileByServerRelativeUrl(\''
+ sourceUrl + '\')/copyto(strnewurl=\'' + destinationUrl + '\',boverwrite=false)';
return this.http.post(url, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// ----------SHAREPOINT LIST CORE----------
// CREATE item - SharePoint list name, and JS object to stringify for save
create(listName: string, jsonBody: any): Promise<any> {
const url = this.apiUrl.replace('{0}', listName);
// append metadata
if (!jsonBody.__metadata) {
jsonBody.__metadata = {
'type': 'SP.ListItem'
};
}
const data = JSON.stringify(jsonBody);
return this.http.post(url, data, this.options).toPromise().then(function (res: Response) {
return res.json();
}).catch(this.handleError);
}
// Build URL string with OData parameters
readBuilder(url: string, options: any): string {
if (options) {
if (options.filter) {
url += ((url.indexOf('?') == -1) ? '?' : '&') + '$filter=' + options.filter;
}
if (options.select) {
url += ((url.indexOf('?') == -1) ? '?' : '&') + '$select=' + options.select;
}
if (options.orderby) {
url += ((url.indexOf('?') == -1) ? '?' : '&') + '$orderby=' + options.orderby;
}
if (options.expand) {
url += ((url.indexOf('?') == -1) ? '?' : '&') + '$expand=' + options.expand;
}
if (options.top) {
url += ((url.indexOf('?') == -1) ? '?' : '&') + '$top=' + options.top;
}
if (options.skip) {
url += ((url.indexOf('?') == -1) ? '?' : '&') + '$skip=' + options.skip;
}
}
return url;
}
// READ entire list - needs $http factory and SharePoint list name
read(listName: string, options?: any): Promise<any> {
// Build URL syntax
// https://msdn.microsoft.com/en-us/library/office/fp142385.aspx#bk_support
let url = this.apiUrl.replace('{0}', listName);
url = this.readBuilder(url, options);
return this.http.get(url, this.options).toPromise().then(function (resp: Response) {
return resp.json();
});
}
// READ single item - SharePoint list name, and item ID number
readItem(listName: string, id: string): Promise<any> {
let url = this.apiUrl.replace('{0}', listName) + '(' + id + ')';
url = this.readBuilder(url, null);
return this.http.get(url, this.options).toPromise().then(function (resp: Response) {
return resp.json();
});
}
// UPDATE item - SharePoint list name, item ID number, and JS object to stringify for save
update(listName: string, id: string, jsonBody: any): Promise<any> {
// Append HTTP header MERGE for UPDATE scenario
const localOptions: RequestOptions = this.options;
localOptions.headers.append('X-HTTP-Method', 'MERGE');
localOptions.headers.append('If-Match', '*');
// Append metadata
if (!jsonBody.__metadata) {
jsonBody.__metadata = {
'type': 'SP.ListItem'
};
}
const data = JSON.stringify(jsonBody);
const url = this.apiUrl.replace('{0}', listName) + '(' + id + ')';
return this.http.post(url, data, localOptions).toPromise().then(function (resp: Response) {
return resp.json();
});
}
// DELETE item - SharePoint list name and item ID number
del(listName: string, id: string): Promise<any> {
// append HTTP header DELETE for DELETE scenario
const localOptions: RequestOptions = this.options;
localOptions.headers.append('X-HTTP-Method', 'DELETE');
localOptions.headers.append('If-Match', '*');
const url = this.apiUrl.replace('{0}', listName) + '(' + id + ')';
return this.http.post(url, '', localOptions).toPromise().then(function (resp: Response) {
return resp.json();
});
}
// JSON blob read from SharePoint list - SharePoint list name
jsonRead(listName: string): Promise<any> {
const svc = this;
return this.getCurrentUser().then(function (res: any) {
// GET SharePoint Current User
svc.currentUser = res.d;
svc.login = res.d.LoginName.toLowerCase();
if (svc.login.indexOf('\\')) {
// Parse domain prefix
svc.login = svc.login.split('\\')[1];
}
// GET SharePoint List Item
const url = svc.apiUrl.replace('{0}', listName) + '?$select=JSON,Id,Title&$filter=Title+eq+\'' + svc.login + '\'';
return svc.http.get(url, svc.options).toPromise().then(function (res2: Response) {
// Parse JSON response
const d2 = res2.json().d;
if (d2.results.length) {
return d2.results[0];
} else {
return null;
}
}).catch(svc.handleError);
});
}
// JSON blob upsert write to SharePoint list - SharePoint list name and JS object to stringify for save
jsonWrite(listName: string, jsonBody: any) {
const svc = this;
return this.refreshDigest().then(function (res: Response) {
return svc.jsonRead(listName).then(function (item: any) {
// HTTP 200 OK
if (item) {
// update if found
item.JSON = JSON.stringify(jsonBody);
return svc.update(listName, item.Id, item);
} else {
// create if missing
item = {
'__metadata': {
'type': 'SP.ListItem'
},
'Title': svc.login,
'JSON': JSON.stringify(jsonBody)
};
return svc.create(listName, item);
}
});
});
}
// **
}