2

I wanted to add an action button in the Site Actions menu and I found a code easy to incorporate :

    $(document).ready(function() {  
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', AddCustomUserAction);  
});  

function AddCustomUserAction() {  
    //Get the client context and web object   
    var clientContext = new SP.ClientContext();  
    var oWeb = clientContext.get_web();  
    //Get the custom user action collection and add the new custom action to it   
    var collUserCustomAction = oWeb.get_userCustomActions();  
    var oUserCustomAction = collUserCustomAction.add();  
    //Specify the location and properties for the new custom action   
    oUserCustomAction.set_location('Microsoft.SharePoint.StandardMenu');  
    oUserCustomAction.set_sequence(101);  
    oUserCustomAction.set_group('SiteActions');  
    oUserCustomAction.set_title("New Action Item");  
    oUserCustomAction.set_url("/site/linksite.html");  
    oUserCustomAction.update();  
    //Load the client context and execute the batch   
    clientContext.load(collUserCustomAction);  
    clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
}   

from Priyaranjan K S . Except putting the code in Site Asset, it put it in Style Library. Afterwards, I tried to modify the title, but it duplicated... I deleted the content editor web part containing the code but all the duplicated are still in my Site Actions menu..

I tried the code and changee ".add" but ".delete" or ".deleteobject" and a few codes from the internet without success... Can someone please help me? I am not sure where went wrong... Maybe I should have include the code in a content editor web part from the same page as where I put the "addcustomaction" even though I deleted it already? I am using SharePoint 2010 and i am really limited in usage.

Here is one of those I tried :

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));
    }

from Kodono

2 Answers 2

3

You must not just copy/paste the code from my blog, but you must customize it for your case.

This code is for this example so you have to adapt it to you.

I think it should result with something like:

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 Action 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()

I've just replaced 'New Menu Item' by 'New Action Item' because you have oUserCustomAction.set_title("New Action Item"); in your code.

2
  • Hello @AymKdn , first of all, thank you for your quick reply. I am sorry, that I copied your whole code. When I added the code, i had a problem and when I re-added it, I forgot to modify it to "New Action"... I tried your code again and it doesn't work :( You think maybe there is something I am missing? I copied your code in a .js then I added the file in a Content Editor WebPart...
    – JoJo
    Commented May 29, 2017 at 14:52
  • Try to copy/paste it directly into the web console of your browser, and look at what is happening.
    – AymKdn
    Commented May 29, 2017 at 18:40
2

Try this code, helped for my:

(function () {
'use strict';
var _clientContext, _userCustomActions;
function modifyUserCustomAction() {
    var web;
    _clientContext = new SP.ClientContext();
    web = _clientContext.get_web();
    _userCustomActions = web.get_userCustomActions();
    _clientContext.load(_userCustomActions);
    _clientContext.executeQueryAsync(Function.createDelegate(this, deleteCustomAction), Function.createDelegate(this, onQueryFailed));
}
function deleteCustomAction() {
    var customActionEnumerator = _userCustomActions.getEnumerator();
    var customAction;
    while (customActionEnumerator.moveNext())
    {
        var userCustomAction = customActionEnumerator.get_current();
        // Change only PopulateCustomIdScript
        if (userCustomAction.get_title() === 'PopulateCustomIdScript')
        {
            customAction = userCustomAction;
            break;
        }
    }
    if(customAction){
        customAction.deleteObject();
        _clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceededOnDeleting), Function.createDelegate(this, onQueryFailedOnDeleting));
    }
}
function onQueryFailed(sender, args) {
    alert('onQueryFailed');
}
function onQuerySucceededOnDeleting() {
    alert('Succeeded');
}
function onQueryFailedOnDeleting(sender, args) {
    alert('onQueryFailedFromDeleting');
}
modifyUserCustomAction();
})();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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