-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormUpdater.js
327 lines (265 loc) · 11.9 KB
/
FormUpdater.js
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
/*
* @name SP Custom Form Updater
* Version 1.0.0
* No dependencies!
* @description A way to utilize client side JSOM to make updates to custom .aspx SharePoint forms.
* @documentation https://github.com/samperrow/sp-custom-form-updater
* @author Sam Perrow https:sharepointhacks.com
*
* Copyright 2019 Sam Perrow (email : sam.perrow399@gmail.com)
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
// Example of how to initialize the script.
var init = function() {
var listName = 'Appointments';
var sourceFilePath = "https://example.sharepoint.com/assets";
var sourceFiles = [
{ filePath: sourceFilePath + '/NewForm.txt', title: 'NewForm' },
{ filePath: sourceFilePath + '/EditForm.txt', title: 'EditForm' },
{ filePath: sourceFilePath + '/DispForm.txt', title: 'DispForm' }
];
var sites = [
"https://example.sharepoint.com/subsite1",
"https://example.sharepoint.com/subsite2",
"https://example.sharepoint.com/subsite3",
"https://example.sharepoint.com/subsite4"
];
return CustomFormUpdater(listName, sourceFiles, sites);
}();
function CustomFormUpdater(listName, sourceFiles, sites) {
// if the sites parameter is available, use that, otherwise collect all subsites URL's.
(sites) ? GetListGuidsForSubsites(sites) : GetSubSiteUrls();
// Part 1.
// Collect all URL's of subsites in a site collection
function GetSubSiteUrls() {
// borrowed from Vadim Gremyachev: https://sharepoint.stackexchange.com/questions/137996/get-all-subsites-and-rootweb-of-a-site-collection-using-jsom#answer-137998
function enumWebs(propertiesToRetrieve, success, error) {
var ctx = new SP.ClientContext.get_current();
var rootWeb = ctx.get_site().get_rootWeb();
var result = [];
var level = 0;
ctx.load(rootWeb, propertiesToRetrieve);
result.push(rootWeb);
var colPropertiesToRetrieve = String.format('Include({0})', propertiesToRetrieve.join(','));
var enumWebsInner = function (web, result, success, error) {
level++;
var ctx = web.get_context();
var webs = web.get_webs();
ctx.load(webs, colPropertiesToRetrieve);
ctx.executeQueryAsync(function () {
for (var i = 0; i < webs.get_count(); i++) {
var web = webs.getItemAtIndex(i);
result.push(web);
enumWebsInner(web, result, success, error);
}
level--;
if (level == 0 && success)
success(result);
}, fail);
};
enumWebsInner(rootWeb, result, success, error);
}
function success(sites) {
var urls = [];
for (var i = 1; i < sites.length; i++) {
urls.push(sites[i].get_url());
}
return GetListGuidsForSubsites(urls);
}
function fail(sender, args) {
console.log(args.get_message());
}
enumWebs(['Url', 'Fields'], success, fail);
}
/*
* Part 2.
* Filter down array of subsite URL's and return the ones that have the specified list.
* @param is an array of strings, each of which is a subsite URL.
*/
function GetListGuidsForSubsites(sites) {
var targetSites = [];
var siteIndex = 0;
function controller() {
if (siteIndex < sites.length) {
getListGuid(sites[siteIndex]);
} else if (targetSites.length > 0) {
console.log('Done collecting list guids.');
return GetListAndFormData(targetSites);
}
}
controller();
function createTargetFiles(siteURL) {
var arr = [];
for (var i = 0; i < sourceFiles.length; i++) {
arr.push({ filePath: siteURL + '/Lists/' + listName + '/' + sourceFiles[i].title + '.aspx', title: sourceFiles[i].title });
}
return arr;
}
function getListGuid(siteURL) {
var currentcontext = new SP.ClientContext(siteURL);
var list = currentcontext.get_web().get_lists().getByTitle(listName);
currentcontext.load(list, 'Id');
currentcontext.executeQueryAsync(
function() {
var listGuid = list.get_id().toString();
console.log('List found at ' + siteURL);
targetSites.push({
subsiteURL: siteURL,
listGUID: listGuid,
listName: listName,
targetForms: createTargetFiles(siteURL)
});
siteIndex++
return controller();
},
function (sender, args) {
console.warn(args.get_message());
siteIndex++
return controller();
});
}
}
/*
* Part 3.
* create new target forms for each target site.
* a) get source file content (from .txt files)
* b) get web part ID for each form
* c) create new form with the web part ID's, listGuids, and siteURL
* @param is an array of objects, each containing the subsite URL, list name, and the list GUID specified to that site.
*/
function GetListAndFormData(targetSites) {
var sourceFormData = [];
var siteIndex = 0;
var formIndex = 0;
if (siteIndex === 0 && formIndex === 0) {
getSourceFormData();
}
function getSourceFormData() {
return (sourceFormData.length < sourceFiles.length) ? getFileContent(sourceFiles[formIndex].filePath, createSourceFile) : init();
}
function createSourceFile(sourceFileUrl, responseText) {
var _title = sourceFiles[formIndex].title;
sourceFormData.push({
url: sourceFileUrl,
title: _title,
fileContent: responseText
});
formIndex++;
return getSourceFormData();
}
function init() {
if (siteIndex < targetSites.length) {
console.log('Finished collecting all source forms. Proceeding to prepare form data for: ' + targetSites[siteIndex].subsiteURL);
formIndex = 0;
controller();
} else {
console.log( targetSites );
console.log( 'Finished preparing all form data. Ready to begin updating...');
UpdateForms(targetSites);
}
}
function controller() {
return (formIndex < sourceFiles.length && formIndex < targetSites[siteIndex].targetForms.length) ? getFileContent(targetSites[siteIndex].targetForms[formIndex].filePath, getWebPartId) : updateSiteObjsWithNewFile();
}
function getWebPartId(index, response) {
var webPartId = '';
var regex = new RegExp(/(?<=\<div\sWebPartID=")(.*?)(?=\")/, 'ig');
if (regex.test(response)) {
webPartId = response.match(regex)[1].toUpperCase(); // the first match is a guid with all zero's. need to improve regex search.
}
targetSites[siteIndex].targetForms[formIndex].webPartId = webPartId;
formIndex++;
return controller();
}
function getFileContent(siteURL, callback) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
return callback(siteURL, this.responseText);
}
}
xhttp.open("GET", siteURL, true);
return xhttp.send();
}
function updateSiteObjsWithNewFile() {
for (var i = 0; i < targetSites[siteIndex].targetForms.length; i++) {
for (var j = 0; j < sourceFormData.length; j++) {
if (targetSites[siteIndex].targetForms[i].title === sourceFormData[j].title) {
targetSites[siteIndex].targetForms[i].newContent = sourceFormData[j].fileContent;
break;
}
}
}
parseSourceFileContent(targetSites[siteIndex]);
console.log('Finished preparing form data for: ' + targetSites[siteIndex].subsiteURL);
siteIndex++;
return init();
}
function parseSourceFileContent(targetSite) {
for (var i = 0; i < targetSite.targetForms.length; i++) {
var targetFile = targetSite.targetForms[i].newContent;
var webPartElem = targetFile.match(/<WebPartPages:DataFormWebPart(.*?)>/i)[1];
var oldGuid = webPartElem.match(/ListName="{(.*?)}"/i)[1];
var oldWebPartId = webPartElem.match(/__WebPartId="{(.*?)}"/i)[1];
var newWebPartId = targetSite.targetForms[i].webPartId;
var oldGuidRegex = new RegExp(oldGuid, 'ig');
var oldWebPartIdRegex = new RegExp(oldWebPartId, 'ig');
var originalSiteName = targetFile.match(/<ParameterBinding Name="weburl" Location="None" DefaultValue="(.*?)"\/>/i)[1];
var origSiteNameRegex = new RegExp(originalSiteName, 'ig');
if (newWebPartId && targetSite.subsiteURL !== originalSiteName) {
targetSite.targetForms[i].newContent = targetFile.replace(origSiteNameRegex, targetSite.subsiteURL).replace(oldGuidRegex, targetSite.listGUID).replace(oldWebPartIdRegex, newWebPartId);
}
}
}
}
/*
* Part 4.
* update the forms for each target site, one at a time.
* @param is an array of objects, each of which
*/
var UpdateForms = function(targetSites) {
var siteIndex = 0;
var formIndex = 0;
function init() {
return (siteIndex < targetSites.length) ? controller() : console.log('Finished the update!');
}
init();
function controller() {
if (formIndex < targetSites[siteIndex].targetForms.length) {
console.log( 'Updating: ' + targetSites[siteIndex].targetForms[formIndex].title );
return updateFile(targetSites[siteIndex]);
} else {
formIndex = 0;
siteIndex++;
return init();
}
}
function updateFile(targetSite) {
var clientContext = new SP.ClientContext(targetSite.subsiteURL);
var list = clientContext.get_web().get_lists().getByTitle(targetSite.listName);
clientContext.load(list);
var fileCreateInfo = new SP.FileCreationInformation();
fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
fileCreateInfo.set_url(targetSite.targetForms[formIndex].filePath);
fileCreateInfo.set_overwrite(true);
for (var i = 0; i < targetSite.targetForms[formIndex].newContent.length; i++) {
fileCreateInfo.get_content().append(targetSite.targetForms[formIndex].newContent.charCodeAt(i));
}
var newFile = list.get_rootFolder().get_files().add(fileCreateInfo);
clientContext.load(newFile);
clientContext.executeQueryAsync(
function() {
console.log('Successfully updated: ' + targetSites[siteIndex].targetForms[formIndex].title + ' on: ' + targetSites[siteIndex].subsiteURL);
formIndex++;
controller();
},
Function.createDelegate(this, errorHandler));
}
function errorHandler(sender, args) {
formIndex++;
controller();
console.log(args.get_message());
}
}
}