forked from w-pacheco/J3WebpartManagerPROD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure-page.aspx
326 lines (310 loc) · 12.6 KB
/
configure-page.aspx
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Custom Actions Configuration</title>
<script src="MicrosoftAjax.js" type="text/javascript"></script>
<script src="jquery-3.4.1.js" type="text/javascript"></script>
<!-- inject:head:js -->
<script type="text/javascript">
(function ($, spg) {
"use strict";
var hostweburl;
var hostsiteurl;
var srcid;
var srcurl;
var srcsequence;
//load the SharePoint resources
$(document).ready(function () {
// get src of script we want to install/uninstall
srcurl = ($.getUrlVar("src") ? decodeURIComponent($.getUrlVar("src")) : 0) || "HomePageRepo/custom.js";
srcsequence = 1000;
$("#scriptlink-name").val(srcurl);
$("#scriptlink-sequence").val(srcsequence);
findHostWebUrl(document.location.pathname).then(function (url) {
hostweburl = url;
if (hostweburl == "/") hostweburl = ""; // blank it if top web
// The SharePoint js files URL are in the form:
// web_url/_layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js file and continue to the
// success handler
$.getScript(scriptbase + "sp.runtime.js");
$.when(
$.getScript(scriptbase + "sp.js")
).done(function () {
// attach executeQueryPromise when SP.ClientContext is loaded
SP.ClientContext.prototype.executeQueryPromise = function () {
var deferred = $.Deferred();
this.executeQueryAsync(function () {
deferred.resolve(arguments);
}, function () {
deferred.reject(arguments);
});
return deferred.promise();
};
var webContext = SP.ClientContext.get_current();
webContext.load(webContext.get_site());
var getSite = webContext.executeQueryPromise();
getSite.then(function () {
hostsiteurl = webContext.get_site().get_serverRelativeUrl();
if (hostsiteurl == "/") hostsiteurl = ""; // blank it if top web
});
});
$.when(
$.getScript(scriptbase + "SP.UI.Controls.js")
// load SPChromeControl when SP.UI.Controls is loaded
).done(function () {
// The Help, Account and Contact pages receive the
// same query string parameters as the main page
var options = {
"appIconUrl": hostweburl + "/_layouts/15/IMAGES/LINKS.GIF",
"appTitle": "Custom Actions Configuration",
"siteUrl": (hostweburl || "/"),
// The onCssLoaded event allows you to
// specify a callback to execute when the
// chrome resources have been loaded.
"onCssLoaded": "window.spg.chromeLoaded()"
};
var nav = new SP.UI.Controls.Navigation("chrome_ctrl_placeholder", options);
nav.setVisible(true);
});
// read custom actions and set up button clicks
spg.listUserCustomActions();
$("button#install-site-user-custom-action").click(function () {
spg.installUserCustomAction("site");
});
$("button#uninstall-site-user-custom-action").click(function () {
spg.uninstallUserCustomAction("site");
});
$("button#install-web-user-custom-action").click(function () {
spg.installUserCustomAction("web");
});
$("button#uninstall-web-user-custom-action").click(function () {
spg.uninstallUserCustomAction("web");
});
});
});
function findHostWebUrl(url) {
// trying to find the nearest CurrentWeb with a bit of recursive promise fun
var defer = $.Deferred();
url = url.substring(0, url.lastIndexOf("/"));
var p = $.ajax({
url: url + "/_api/web",
dataType: "json",
contentType: 'application/json',
headers: {"Accept": "application/json; odata=verbose"},
method: "GET",
cache: false
});
p.done(function (response) {
defer.resolve(response.d.ServerRelativeUrl);
});
p.fail(function () {
if (!url) {
defer.reject();
return;
}
// hmm not a web, go higher
var p1 = findHostWebUrl(url);
p1.then(defer.resolve, defer.reject);
});
return defer.promise();
}
spg.chromeLoaded = function () {
// Callback for the onCssLoaded event defined
// in the options object of the chrome control
// When the page has loaded the required
// resources for the chrome control,
// display the page body.
$("body").show();
};
$.extend({
getUrlVars: function () {
var vars = [], hash;
var hashes = window.location.search.slice(window.location.search.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function (name) {
return $.getUrlVars()[name];
}
});
// refresh both site and web custom actions
spg.listUserCustomActions = function () {
function listUserCustomAction(siteOrWeb) {
siteOrWeb = (siteOrWeb == "site" ? "site" : "web");
var p1 = $.ajax({
url: hostweburl + "/_api/" + siteOrWeb + "/userCustomActions?$orderby=Sequence",
dataType: "json",
contentType: 'application/json',
headers: {"Accept": "application/json; odata=verbose"},
method: "GET",
cache: false
});
p1.then(function (response) {
$("ul#" + siteOrWeb + "-user-custom-actions").empty();
$.each(response.d.results, function (i, result) {
$("ul#" + siteOrWeb + "-user-custom-actions").append(
"<li>" +
" [" + result.Location + "] " +
(result.Title || result.Name || "") +
" ScriptSrc=" + result.ScriptSrc +
" Sequence=" + result.Sequence +
"</li>"
);
});
});
return p1;
}
return $.when(
listUserCustomAction("site"),
listUserCustomAction("web")
);
};
// install site or web custom action
spg.installUserCustomAction = function (siteOrWeb) {
var webContext = SP.ClientContext.get_current();
var userCustomActions;
if (siteOrWeb == "site") {
userCustomActions = webContext.get_site().get_userCustomActions();
}
else {
userCustomActions = webContext.get_web().get_userCustomActions();
}
webContext.load(userCustomActions);
srcurl = $("#scriptlink-name").val();
srcid = $("#scriptlink-id").val();
srcsequence = parseInt($("#scriptlink-sequence").val()) || 1000;
var action = userCustomActions.add();
action.set_location("ScriptLink");
action.set_title(srcurl);
var isCss = /\.css$/gi.test(srcurl);
var block;
if (isCss) {
block = [
"(function(){",
"var head1 = document.getElementsByTagName('head')[0];",
"var link1 = document.createElement('link');",
"link1.type = 'text/css';",
"link1.rel = 'stylesheet';",
"link1.href = '" + hostsiteurl + "/" + srcurl + "';",
(srcid ? "link1.id = '" + srcid + "';" : ""),
"head1.appendChild(link1);",
"})();"
].join("");
action.set_scriptBlock(block);
}
else if (srcid) {
block = [
"(function(){",
"var head1 = document.getElementsByTagName('head')[0];",
"var script1 = document.createElement('script');",
"script1.type = 'text/javascript';",
"script1.src = '" + hostsiteurl + "/" + srcurl + "';",
"script1.id = '" + srcid + "';",
"head1.appendChild(script1);",
"})();"
].join("");
action.set_scriptBlock(block);
}
else {
action.set_scriptSrc("~sitecollection/" + srcurl);
}
action.set_sequence(srcsequence);
action.update();
webContext.load(action);
return webContext.executeQueryPromise().pipe(function () {
return spg.listUserCustomActions();
});
};
// uninstall site or web custom action
spg.uninstallUserCustomAction = function (siteOrWeb) {
var webContext = SP.ClientContext.get_current();
var userCustomActions = webContext.get_site().get_userCustomActions();
if (siteOrWeb == "site") {
userCustomActions = webContext.get_site().get_userCustomActions();
}
else {
userCustomActions = webContext.get_web().get_userCustomActions();
}
webContext.load(userCustomActions);
srcurl = $("#scriptlink-name").val();
var p1 = webContext.executeQueryPromise();
var p2 = p1.pipe(function () {
var count = userCustomActions.get_count(), action = null;
for (var i = count - 1; i >= 0; i--) {
action = userCustomActions.get_item(i);
if (action.get_scriptSrc() == "~sitecollection/" + srcurl || action.get_title() == srcurl) {
action.deleteObject();
}
}
return webContext.executeQueryPromise().pipe(function () {
return spg.listUserCustomActions();
});
});
return p2;
};
})(window.jQuery, window.spg = window.spg || {});
</script>
<!-- endinject -->
<!-- inject:head:css -->
<style type="text/css">
button {
width: 150px;
margin-bottom: 5px;
}
.left {
float: left;
}
.clear-left {
clear: left;
}
</style>
<!-- endinject -->
</head>
<body style="display: none">
<!-- Chrome control placeholder -->
<div id="chrome_ctrl_placeholder"></div>
<!-- The chrome control also makes the SharePoint
Website style sheet available to your page. -->
<div id="MainContent" style="padding-left:20px;">
<h1 class="ms-accentText">User Custom Actions Configuration</h1>
This page lists the current user custom actions configured for the current site and site collection.
<br />
<br />
<h2 class="ms-accentText">Site Collection User Custom Actions</h2>
<ul id="site-user-custom-actions">
</ul>
<br />
<h2 class="ms-accentText">Site User Custom Actions</h2>
<ul id="web-user-custom-actions">
</ul>
<br />
<h2 class="ms-accentText">Install User Custom Action</h2>
<br />
<div class="left">
<label for="scriptlink-id" style="display:block;float:left;width:165px;">ID (usually leave blank)</label>
<label for="scriptlink-name" style="display:block;float:left;width:315px;">Url (e.g. HomePageRepo/custom.js)</label>
<label for="scriptlink-sequence" style="display:block;float:left;width:165px;">Sequence (e.g. 1000)</label>
</div>
<div class="clear-left left">
<input type="text" id="scriptlink-id" style="width:150px;" />
<input type="text" id="scriptlink-name" style="width:300px;" />
<input type="number" id="scriptlink-sequence" style="width:150px;" value="1000" />
</div>
<div class="clear-left left">
<button id="install-site-user-custom-action" type="button">Install Site Collection</button>
<button id="uninstall-site-user-custom-action" type="button">Uninstall Site Collection</button>
<br />
<button id="install-web-user-custom-action" type="button">Install Current Web</button>
<button id="uninstall-web-user-custom-action" type="button">Uninstall Current Web</button>
</div>
<br />
<br />
</div>
</body>
</html>