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 Commented 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? Commented Dec 18, 2016 at 11:48
  • It works with "ctx.executeQueryAsync" changed to "clientContext.executeQueryAsync" - thank you! Commented Dec 18, 2016 at 16:32
  • 1
    You are correct, my bad. Have updated the code :) Commented Dec 18, 2016 at 16:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.