3

I've found many code examples online showing how to add a User Custom Action to ECB from JavaScript. Take this one, for example.

I've adapted and used the code, and it successfully adds a menu item to the ECB menu.

The problem I have now is that I need to add a user custom action to the drop down menu that shows up when more than one item is checked (selected) from the document list.

My guess is that the ECB menu that is shown when 1 item is selected is not the same that the one shown when more than 1 item are selected.

For reference purposes, here is a code example (which adds the menu item to ECB menu shown when only 1 item is selected from the list):

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

function AddCustomUserAction() {
    var clientContext = new SP.ClientContext();  
    var oWeb = clientContext.get_web(); 
    var oList = oWeb.get_lists().getByTitle("Documents");  
    var userCustomActionColl = oList.get_userCustomActions(); 

    clientContext.load(oList,'UserCustomActions','Title');

    clientContext.executeQueryAsync(function() {
        var customActionEnumerator = userCustomActionColl.getEnumerator();
        var foundAction = 0;
        while (customActionEnumerator.moveNext()) 
        {
            var oUserCustomAction = customActionEnumerator.get_current();            
            if (oUserCustomAction.get_title() == "A custom action") 
            {
                foundAction=1;
                break;
            }
        }
        if(foundAction == 0){               
            var oUserCustomAction = userCustomActionColl.add();

            oUserCustomAction.set_location('EditControlBlock');  
            oUserCustomAction.set_sequence(100);
            oUserCustomAction.set_title("A custom action");
            oUserCustomAction.set_url("javascript:console.log('A custom action taking place...');");

            oUserCustomAction.update(); 
            clientContext.load(userCustomActionColl);
            clientContext.executeQueryAsync(function () {
                console.log('Success.');
            }, function (sender, args) {
                console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            });

        } 
   },
   function(sender,args){
       console.log('Request failed. ' + args.get_message() + '\n' +   args.get_stackTrace());
   });
}

Also, if this cannot be achieved from JavaScript, what about by manually editing an xml or ascx SharePoint source file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.