Link to home
Start Free TrialLog in
Avatar of Michael Vasilevsky
Michael VasilevskyFlag for United States of America

asked on

Add Context Menu Item

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>

Open in new window


The result:
Capture.PNG
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael Vasilevsky

ASKER

I'm getting "collUserCustomAction is not defined" on:

var customActionEnumerator = collUserCustomAction.getEnumerator();

Open in new window

I got it to work with:

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

Open in new window


Thank you!
congratulations!