Skip to content

Instantly share code, notes, and snippets.

@BigRaj
Created October 26, 2018 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRaj/226a81cd16406dc95b6d21e5fdfc0d23 to your computer and use it in GitHub Desktop.
Save BigRaj/226a81cd16406dc95b6d21e5fdfc0d23 to your computer and use it in GitHub Desktop.
Creates a site scoped scriptlink custom action
var siteurl = _spPageContextInfo.webAbsoluteUrl;
var formDigest;
var generateCustomAction = function(location,sequence,title,description,url){
location = location || 'ScriptLink';
sequence = sequence || 1000;
title = title || 'Test Custom Action';
description = description || 'Test Description';
url = url || '~sitecollection/Style Library/scripts/jquery-3.3.1.js';
return JSON.stringify({
'__metadata': {
'type': 'SP.UserCustomAction'
},
'Location':location.toString(),
'Sequence':sequence.toString(),
'Name':title.toString(),
'Description':description.toString(),
'ScriptSrc': url.toString()
});
}
var getFormDigest = function(){
return $.ajax({
type: 'Post',
url: siteurl + '/_api/contextinfo',
dataType: 'json',
headers: {
'Accept': 'application/json;odata=verbose'
}
}).then(function(data){
return data.d.GetContextWebInformation.FormDigestValue;
});
};
var set_formDigest = (function(callback){callback = callback || function(){console.log('FormDigest Set');};getFormDigest().then(function(digest){formDigest = digest;callback();})})();
var addSiteCustomAction = function(){
$.ajax({
url: siteurl + "/_api/site/UserCustomActions",
headers: {
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json; odata=verbose",
"X-RequestDigest": formDigest
},
method: "POST",
data: generateCustomAction(),
success: function(data){
console.log(data);
if(data.d.results){
console.log(data.d.results);
}
},
error: function(data){
console.log(data);
console.error("Error: " + data);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment