Delete a User Custom Action from Sharepoint [JavaScript]

The MSDN documentation is a nightmare…. For example Microsoft provides an example to create a User Custom Action for Sharepoint, however there is no code about how to delete it.

So here is the code to use to delete the usercustomaction just created with the above example:

var siteUrl = '/site collection/site/';

function deleteUserCustomAction() {
    this.clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.collUserCustomAction = oWebsite.get_userCustomActions();
    clientContext.load(oWebsite,'UserCustomActions','Title');
    clientContext.executeQueryAsync(Function.createDelegate(this, this.deleteCustomAction), Function.createDelegate(this, this.onQueryFailed));
}
function deleteCustomAction() {
    var customActionEnumerator = collUserCustomAction.getEnumerator();
    while (customActionEnumerator.moveNext())  {
      var oUserCustomAction = customActionEnumerator.get_current();
      if (oUserCustomAction.get_title() == 'New Menu Item') {
           oUserCustomAction.deleteObject();        
           clientContext.load(oUserCustomAction);
           clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }
}
function onQuerySucceeded() {
    alert('Custom action removed');
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
deleteUserCustomAction()

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*