Below code was used to add Custom action to site

<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script>
function AddCustomActions() {
try{
var clientContext = new SP.ClientContext();
var site = clientContext.get_web();
var UserCustomActions = site.get_userCustomActions();
 newUserCustomAction = UserCustomActions.add();
newUserCustomAction.set_location('ScriptLink');
newUserCustomAction.set_scriptSrc('~SiteCollection/SiteAssets/alert.js');
newUserCustomAction.set_sequence(10);
newUserCustomAction.set_title('New Alert');
newUserCustomAction.set_description('Global Alert');
newUserCustomAction.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
catch(ex)
   {
   alert('Error: ' + ex);
   }
}
function onQuerySucceeded(sender, args) {
alert('New Support files added to Site.\n\nRefresh the page.');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
AddCustomActions()
</script>

I am facing the issue as i have worngly referenced url in source and my site is down giving error:

Cannot make a cache safe URL for http://ajax.aspnetcdn.com/ajax/knockout/knockout-3.3.0.js, file not found. Please verify that the file exists under the layouts directory."

up vote 4 down vote accepted

You could delete the specified UserCustomAction by Sequence property as demonstrated below:

function deleteCustomAction(sequenceNo,success,error) {

    var ctx = new SP.ClientContext();
    var site = ctx.get_web();
    var userCustomActions = site.get_userCustomActions();
    ctx.load(userCustomActions);

    ctx.executeQueryAsync(function(){
           var result = userCustomActions.get_data().filter(function(a){
               if(a.get_sequence() == sequenceNo)
                  return a;
           }); 
           if(result.length > 0){
              result[0].deleteObject(); 
              ctx.executeQueryAsync(success,error);
           } 
        },error);
}

Usage

deleteCustomAction(10,
     function(actions) {
        console.log('User custom action has been deleted');
    },
    function(sender, args) {
       console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    });
  • 1
    Thanks a Lot Vadim...Your code worked for me but had to run three times to remove userCustomAction deployed wrongly... – Ankitkumar Malde Dec 14 '15 at 9:21

Just delete the custom action and add again with correct details; some sample code to do this:

    Web web = context.Web;

    context.Load(web, x => x.UserCustomActions);
    context.ExecuteQuery();

    List<UserCustomAction> ualist = new List<UserCustomAction>();
    foreach (var ca in web.UserCustomActions)
    {
        if (ca.ScriptSrc != null && ca.ScriptSrc.Contains("alert.js"))
        {
            ualist.Add(ca);
        }
    }

    foreach (var aa in ualist)
    {
        aa.DeleteObject();
    }

    context.ExecuteQuery();

You can add more checks like for Title of custom action before deleting.

  • using csom he said... – ameliapond Apr 6 '17 at 14:32
  • and using csom I wrote :) – Prakash - Crow Canyon Apr 7 '17 at 17:16
  • Web web = context.Web; So now we have typed variables in CSOM ? sorry I didn't know... – ameliapond Apr 8 '17 at 14:14

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.