2

I'm adding a Context Menu (ECB) item to a SharePoint Online list using the code example found here.

My code works, the problem is it adds a new item each time the page is refreshed, not overwriting the previously added item, so I get duplicates. How to prevent this?

My code, added via Script Editor on the SharePoint list page:

<script  src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>

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

  function AddCustomUserActionToECB() {  
    var clientContext = new SP.ClientContext();  
    var oWeb = clientContext.get_web();  
    var oList = oWeb.get_lists().getByTitle('ProjectCommunications');  
    var userCustomActionColl = oList.get_userCustomActions();  
    var oUserCustomAction = userCustomActionColl.add();  
    oUserCustomAction.set_location('EditControlBlock');  
    oUserCustomAction.set_sequence(100);  
    oUserCustomAction.set_title("Add Attachment");  
    oUserCustomAction.set_url("myURL");   
    oUserCustomAction.update(); 
    clientContext.load(userCustomActionColl);
    clientContext.executeQueryAsync();  
  }  
</script>

The result:enter image description here

How to prevent duplicate entries?

1
  • You have to add your own code to check if the Menu Item already exists because the add() method... adds Dec 17, 2016 at 19:55

1 Answer 1

3
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>

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

  function AddCustomUserActionToECB() {  
    var clientContext = new SP.ClientContext();  
    var oWeb = clientContext.get_web();  
    var oList = oWeb.get_lists().getByTitle('ProjectCommunications');  
    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() == 'Add Attachment') 
        {
            foundAction=1;
            break;
        }
    }   
    if(foundAction==0){
        var oUserCustomAction = userCustomActionColl.add();  
        oUserCustomAction.set_location('EditControlBlock');  
        oUserCustomAction.set_sequence(100);  
        oUserCustomAction.set_title("Add Attachment");  
        oUserCustomAction.set_url("myURL");   
        oUserCustomAction.update(); 
        clientContext.load(userCustomActionColl);
        clientContext.executeQueryAsync();  
    }   
   },
   function(sender,args){
       console.log('Request failed. ' + args.get_message() + '\n' +   args.get_stackTrace());
   });
}
</script>
3
  • 1
    What does Function.createDelegate do? Dec 18, 2016 at 11:48
  • It works with "ctx.executeQueryAsync" changed to "clientContext.executeQueryAsync" - thank you!
    – dikuw
    Dec 18, 2016 at 16:32
  • 1
    You are correct, my bad. Have updated the code :) Dec 18, 2016 at 16:34

Your Answer

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

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