Created
October 26, 2018 15:22
-
-
Save BigRaj/226a81cd16406dc95b6d21e5fdfc0d23 to your computer and use it in GitHub Desktop.
Creates a site scoped scriptlink custom action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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