Michael Vasilevsky
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:
The result:
Capture.PNG
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:
Capture.PNG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I got it to work with:
Thank you!
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());
});
}
Thank you!
congratulations!
ASKER
Open in new window