Get SharePoint Online (Office 365 Enterprise E3) Subscription for your Business NOW

How to add and remove item from the Site Actions menu in SharePoint online Office 365 using the JavaScript object model?

add item to Site Actions menu in SharePoint online

This SharePoint tutorial we will discuss, how we can add an item into the site actions menu in SharePoint online Office 365 using the JavaScript object model? We will also see how we can remove an item from the site actions menu in SharePoint Online Office 365 using jsom.

Add item to Site Actions menu in SharePoint online Office 365 using the JavaScript object model (jsom)

Below is the jsom code to add an item to site actions menu in SharePoint Online Office 365 using JavaScript Object model (jsom). We can add the below code inside a script editor web part which we can put inside a web part page.


Below is the full code:

<script language=”javascript” type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js”></script>
<script language=”javascript” type=”text/javascript”>
$(document).ready(function() {
SP.SOD.executeFunc(‘sp.js’, ‘SP.ClientContext’, AddCustomMenuAction);
});

function AddCustomMenuAction() {

var clientContext = new SP.ClientContext();
var oWeb = clientContext.get_web();

var menuActions = oWeb.get_userCustomActions();
var menuAction = menuActions.add();
menuAction.set_location(‘Microsoft.SharePoint.StandardMenu’);
menuAction.set_sequence(101);
menuAction.set_group(‘SiteActions’);
menuAction.set_title(“Go to EnjoySharePoint”);
menuAction.set_url(“https://www.enjoysharepoint.com/”);
menuAction.update();

clientContext.load(menuActions);
clientContext.executeQueryAsync(OnSuccess, OnFailure);
}

function OnSuccess() {
alert(‘Custom menu action added in site actions’);
}

function OnFailure() {
alert(‘Fail’);
}
</script>

Once we add the below code then we can see the menu options like below:

add item to Site Actions menu in SharePoint online
add item to Site Actions menu in SharePoint online

Remove items from Site Actions menu using JavaScript object model in SharePoint Online Office 365

Now, we will discuss how we can remove items from the Site Actions menu using JavaScript object model (jsom) in SharePoint Online Office 365.

Here in my SharePoint online site, I have added a few items to the Site Actions menu like below:

Remove items from Site Actions menu using JavaScript object model
Remove items from Site Actions menu using JavaScript object model

We can add the below jsom code inside a script editor web part which we can add into a web part page.

Below is the full code

<script language=”javascript” type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js”></script>
<script language=”javascript” type=”text/javascript”>
var customActions;
var clientContext;
$(document).ready(function() {
SP.SOD.executeFunc(‘sp.js’, ‘SP.ClientContext’, DeleteCustomMenuAction);
});

function DeleteCustomMenuAction() {
clientContext = new SP.ClientContext();
var web = clientContext.get_web();
customActions = web.get_userCustomActions();
clientContext.load(customActions);
clientContext.load(web);
clientContext.executeQueryAsync(OnSuccess, OnFailure);
}

function OnSuccess() {
var enumerator = customActions.getEnumerator();
var removeThese = []
while (enumerator.moveNext()) {
var action = enumerator.get_current();
alert(action.get_title());
if (action.get_title() == “Go to EnjoySharePoint”)
{
action.deleteObject();
}
clientContext.load(action);
clientContext.executeQueryAsync();
}
}

function OnFailure() {
alert(‘Fail’);
}
</script>

Once you add the code and execute, the menu items will be removed from the Site Actions menu.

SharePoint online Remove items from Site Actions menu using JavaScript object model
SharePoint online Remove items from Site Actions menu using JavaScript object model

You may like following SharePoint tutorials:

Hope this SharePoint tutorial helps to add or remove items from the site actions menu in SharePoint Online using the JavaScript object model (jsom).

Check out Best Alternative to InfoPath -> Try Now

EnjoySharePoint YouTube Channel

You May Also like the Following SharePoint 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 →