Add custom actions in Site Actions menu in SharePoint Online using JavaScript Object Model and Delete custom action menu option in Site Actions menu in SharePoint online using JavaScript Object Model

Add custom actions in Site Actions menu in SharePoint Online using JavaScript Object Model and Delete custom action menu option in Site Actions menu in SharePoint online using JavaScript Object Model

In this post, we will discuss how we can add custom actions menu options in Site Actions in SharePoint online using JavaScript Object Model and also we will discuss how we can delete custom action menu option in Site Actions menu in SharePoint online using JavaScript Object Model.


The menu will appear when you click on the gear icon.

Also, read:

Steps to create a custom timer job in SharePoint 2016 using Visual Studio 2015

You can use the below code inside a script editor web part.

Add custom actions in Site Actions menu:
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>

<input name=”ADD” id=”btnAdd” type=”submit” value=”Add new custom actions” />

<script language=”javascript” type=”text/javascript”>

$(document).ready(function () {

$(“#btnAdd”).click(function () {

AddCustomActions();
});
});
function AddCustomActions() {
var clientContext = new SP.ClientContext();
var web=clientContext.get_web();
var customActions = web.get_userCustomActions();
var newCustomActionMenu = customActions.add();
newCustomActionMenu.set_location(‘Microsoft.SharePoint.StandardMenu’);
newCustomActionMenu.set_group(‘SiteActions’);
newCustomActionMenu.set_sequence(3000);
newCustomActionMenu.set_title(‘EnjoySharePoint.com’);
newCustomActionMenu.set_description(‘Go to EnjoySharePoint.com’);
newCustomActionMenu.set_url(‘https://www.enjoysharepoint.com’);
newCustomActionMenu.update();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
Function.createDelegate(this, this.onFailure));
}
function onSuccess() {
alert(‘New Item Added to Site Actions Menu’);
}
function onFailure(sender, args) {
alert(‘Error Occurred. ‘ + args.get_message());
}
</script>

Once you will add, it will come like below:

 Add custom actions in Site Actions menu in SharePoint Online using JavaScript Object Model and Delete custom action menu option in Site Actions menu in SharePoint online using JavaScript Object Model
Add custom actions in Site Actions menu in SharePoint Online using JavaScript Object Model and Delete custom action menu option in Site Actions menu in SharePoint online using JavaScript Object Model

Also read:

Add item to SharePoint list using JavaScript object model in SharePoint Online

Delete custom action menu option in Site Actions menu:

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js”></script>

<input name=”Delete” id=”btnDelete” type=”submit” value=”Delete custom actions” />

<script language=”javascript” type=”text/javascript”>
var customActions;
var clientContext;
$(document).ready(function () {

$(“#btnDelete”).click(function () {

DeleteCustomActions();
});
});
function DeleteCustomActions() {
clientContext = new SP.ClientContext();
var web=clientContext.get_web();
customActions = web.get_userCustomActions();
clientContext.load(web, ‘UserCustomActions’, ‘Title’);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess),
Function.createDelegate(this, this.onFailure));
}
function onSuccess() {
var customActionEnumerator = customActions.getEnumerator();
while (customActionEnumerator.moveNext())
{
var oUserCustomAction = customActionEnumerator.get_current();
alert(oUserCustomAction.get_title());
if (oUserCustomAction.get_title() == ‘EnjoySharePoint.com’)
{
oUserCustomAction.deleteObject();
clientContext.load(oUserCustomAction);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededdelete), Function.createDelegate(this, this.onQueryFailed));
}
}
}
function onQuerySucceededdelete()
{
alert(‘Custom Actions Deleted Successfully’);
}
function onQueryFailed(sender, args) {
alert(‘Error Occurred. ‘ + args.get_message()+ ‘\n’ + args.get_stackTrace());
}
function onFailure(sender, args) {
alert(‘Error Occurred. ‘ + args.get_message());
}
</script>

You can see like below the menu will be removed.

 Add custom actions in Site Actions menu in SharePoint Online using JavaScript Object Model and Delete custom action menu option in Site Actions menu in SharePoint online using JavaScript Object Model
Add custom actions in Site Actions menu in SharePoint Online using JavaScript Object Model and Delete custom action menu option in Site Actions menu in SharePoint online using JavaScript Object Model


Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →