How To Change UserCustomAction in SharePoint with Javascript (JSOM)


If you customized some part of SharePoint with a User Custom Action, Location=Scriptlink and need to update the js file at some point, having users use the new version is achieved by changing the scriptSrc. Otherwise, the old version of the JS file is cached by the browser even after F5. Only a SHIFT+F5 would refresh it to the new version. To avoid having all users the pain, you can easily update the path to you js by adding some date code in the path, like http://path-to-file/siteassets/file.js?v=2017112702

var ideaCtx = SP.ClientContext.get_current();
var web = ideaCtx.get_web();
var oCustomActions = web.get_userCustomActions();
ideaCtx.load(oCustomActions);
ideaCtx.executeQueryAsync(
function () {
var enumerator = oCustomActions.getEnumerator();
while (enumerator.moveNext()) {
// first log all Custom Actions to identify the one you want to delete
console.log(enumerator.get_current().get_title() + " -> " + enumerator.get_current().get_location() + ' -> ' + enumerator.get_current().get_scriptSrc());
}
var ca = oCustomActions.getItemAtIndex(IDENTIFIED_ZERO_BASED_INDEX_IN_ABOVE_ENUM); // choose this after
ca.set_scriptSrc(UPDATE_THIS_WITH_newPath)
ca.update();
ideaCtx.executeQueryAsync(
function () { console.log('CA modified'); },
function (sender, args) { console.log(args.get_message()); }
)
},
function (sender, args) { console.log(args.get_message()); });