-
Notifications
You must be signed in to change notification settings - Fork 0
/
k.js
162 lines (143 loc) · 6.89 KB
/
k.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
// ==UserScript==
// @name k - the SharePoint console helper library
// @namespace k
// @version 0.1
// @description do hard task easily in sharepoint
// @author @damsleth
// @match https://*/*
// @grant none
// ==/UserScript==
(function() {
window.k = { }
// logs and reloads
k.logAndReload = (str, lvl = "log") => {
console[lvl](str);
window.setTimeout(() => location.reload(), 500)
}
k.fetch = async (endPoint) => {
let response = await fetch(`${_spPageContextInfo.siteAbsoluteUrl}/_api/${endPoint}`, {
credentials: "include", headers: { accept: "application/json;odata=verbose" }
})
if (response.ok) {
let r = await response.json()
return r ? r.d ? r.d.d ? r.d.d : r.d : r : null
} else { return null }
}
// Change web properties - eg, k.ChangeWebProp("Title", "LolSite")
k.ChangeWebProp = (prop, value) => fetch(`${_spPageContextInfo.webAbsoluteUrl}/_api/web`, {
method: "MERGE", credentials: "include", headers: {
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"Content-type": "application/json;odata=verbose",
}, body: `{"__metadata":{"type": "SP.Web"},"${prop}":"${value}"}`
})
.then(res => res.ok ? k.logAndReload(`Successfully set property \n "${prop}": "${value}"`) : k.logAndReload(`Failed setting "${prop}"`, "err"))
.then(window.setTimeout(() => location.reload(), 500))
// Sets the value of a property on a sitefield - eg. the "indexed" property of "Title" - k.SetFieldPropValue("Title","indexed","true")
k.SetFieldPropValue = (field, prop, value) => fetch(`${_spPageContextInfo.webAbsoluteUrl}/_api/web/fields/GetByInternalNameOrTitle('${field}')`, {
method: "MERGE",
credentials: "include",
headers: {
"X-RequestDigest": document.getElementById("__REQUESTDIGEST").value,
"Content-type": "application/json;odata=verbose",
},
body: `{"__metadata":{"type": "SP.Field"},"${prop}":"${value}"}`
}).then(res => res.ok ? k.logAndReload(`Successfully set "${prop}" for "${field}" to "${value}"`) : k.logAndReload(`Failed setting "${prop}" on "${field}"`, "err"))
// Adds a scriptlink customaction with the given name (key) and url - e.g. k.AddCustomAction("k.lol.js", "/siteassets/js/k.lol.js")
k.AddCustomAction = (name,scriptblock,sequence) => {
let seq = sequence;
if(!sequence){ seq = Math.floor(Math.random() * 1000) };
let ctx = SP.ClientContext.get_current();
let uc = ctx.get_site().get_userCustomActions();
let uca = uc.add();
uca.set_location("ScriptLink");
uca.set_sequence(seq);
uca.set_title(name);
uca.set_name(name)
uca.set_description(`Adds ${name}`);
uca.set_scriptBlock(`${scriptblock}`);
uca.update();
ctx.executeQueryAsync(() => k.logAndReload(`Added CustomAction ${name}`), () => k.logAndReload(`Failed adding customaction ${name}`, "err"));
}
// Adds a SOD scriptlink customaction with the given name (key) and url - e.g. k.AddCustomAction("k.lol.js", "/siteassets/js/k.lol.js")
k.AddCustomActionSOD = (name, url) => {
let seq = Math.floor(Math.random() * 1000)
let ctx = SP.ClientContext.get_current();
let uc = ctx.get_site().get_userCustomActions();
let uca = uc.add();
uca.set_location("ScriptLink");
uca.set_sequence(seq);
uca.set_title(name);
uca.set_name(name);
uca.set_description(`Adds ${name}`);
uca.set_scriptBlock(`SP.SOD.registerSod('${name}', '${url}?rev=' + new Date().getDay());LoadSodByKey('${name}');`);
uca.update();
ctx.executeQueryAsync(() => k.logAndReload(`Added CustomAction ${name}`), () => k.logAndReload(`Failed adding customaction ${name}`, "err"));
}
// Deletes a custom action by name. If none is specified, lists all customactions
k.DeleteCustomAction = (customActionName) => {
let ucaID;
const ctx = SP.ClientContext.get_current();
let ucaColl = ctx.get_site().get_userCustomActions();
ctx.load(ucaColl);
ctx.executeQueryAsync(() => {
let en = ucaColl.getEnumerator();
if (!customActionName) { console.log(`No User Custom Action Specified, listing all UCAs in site`) }
while (en.moveNext()) {
let currentUCA = en.get_current();
let ucaName = currentUCA.get_name();
if (!customActionName) { console.log(ucaName); }
if (ucaName === customActionName) {
ucaID = currentUCA.get_id();
}
}
if (ucaID) {
let ucaToDelete = ucaColl.getById(ucaID);
ucaToDelete.deleteObject();
ctx.load(ucaToDelete);
ctx.executeQueryAsync(() => {
let ucaToDeleteName = ucaToDelete.get_name();
k.logAndReload(`Custom action ${ucaToDeleteName} deleted`);
}, () => k.logAndReload(`Unable to delete customaction ${ucaToDelete.get_name()}`,"err"));
} else {
if (customActionName) {
k.logAndReload(`User custom action ${customActionName} not found`, "warn");
}
}
});
}
// Enables a web feature by guid
k.AddWebFeature = (guid) => {
const ctx = SP.ClientContext.get_current()
let web = ctx.get_web()
ctx.load(web)
let feats = web.get_features()
let featGuid = new SP.Guid(`{${guid}}`)
feats.add(featGuid, true, SP.FeatureDefinitionScope.web)
ctx.load(feats)
ctx.executeQueryAsync(() => k.logAndReload("enabled suitenav,maybe?"))
}
// Sets the JSLink on a given field - e.g. k.SetJsLinkOnField("Title","/siteassets/jslink/jsLolLink.js")
k.SetJsLinkOnField = (fieldName, jsLink) => {
const JSLinkPrefix = "clienttemplates.js|";
const ctx = new SP.ClientContext.get_current();
let field = ctx.get_web().get_availableFields().getByInternalNameOrTitle(fieldName);
ctx.load(field);
ctx.executeQueryAsync(() => {
field.set_jsLink(`${JSLinkPrefix}${jsLink}`);
field.updateAndPushChanges(true);
ctx.executeQueryAsync(() => {
k.logAndReload(`Set JSLink with url ${jsLink} for field ${fieldName}.\n${field.get_jsLink()}`);
});
});
}
// Returns the given jslink for a sitefield
k.GetJSLinkForField = (fieldName) => {
const ctx = SP.ClientContext.get_current();
let field = ctx.get_web().get_availableFields().getByInternalNameOrTitle(fieldName);
ctx.load(field);
ctx.executeQueryAsync(() => {
console.log(field.get_jsLink());
});
}
console.log("%c k ","background:#000")
})();