Add/Remove User Custom Actions (Site Actions Menu) Programmatically using JSOM in SharePoint Online

This SharePoint jsom tutorial, we will discuss, how to add user custom actions (site actions menu) programmatically using JSOM (JavaScript Object Model) in SharePoint Online Office 365. We will also check, how to remove user custom actions (site actions menu) programmatically using JSOM (JavaScript Object Model) in SharePoint Online.

We will also see, how to remove UserCustomActions from Ribbon in the SharePoint Online list using csom?

The links will be added to the SharePoint site actions menu in SharePoint Online Office 365 using the JavaScript object model.

Add User Custom Actions (Site Actions Menu) Programmatically using JSOM in SharePoint Online

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

Below is the full code to add user custom actions (site actions menu) programmatically using javascript.

<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("http://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 User Custom Actions (Site Actions Menu) Programmatically using JSOM in SharePoint Online

Remove User Custom Actions (Site Actions Menu) Programmatically using JSOM in SharePoint Online

Now, we will see how to remove user custom actions (Site actions menu) programmatically using the javascript object model in SharePoint Online Office 365.

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

Remove User Custom Actions (Site Actions Menu) Programmatically using JSOM in SharePoint Online

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

Below is the full code to remove user custom actions in SharePoint using jsom.

<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.

Remove User Custom Actions (Site Actions Menu) Programmatically using JSOM in SharePoint Online

Remove UserCustomActions from Ribbon in SharePoint online list using csom

Here we will see, how to remove UserCustomActions from Ribbon, FormRibbon and ECB Menu using csom .Net managed object model code in SharePoint Online. We will use here C#.Net managed object model code where we will use below dlls:

  • Microsoft.SharePoint.Client.Dll
  • Microsoft.SharePoint.Client.Runtime.Dll

Here I have a list where we have added UserCustomActions buttons to Ribbon, FormRibbon and to ECB menu. For one of our requirements, we need to remove the ribbon buttons. So I wrote a console application by using .Net managed object model code.

Here we are retrieving the user custom actions for the list by using the below code:

var userCustomActions = list.UserCustomActions;

Then we are calling the DeleteCustomAction method which will check the particular custom action name and then delete it.

public static void DeleteCustomRibbonActionFromList()
{
using (ClientContext ctx = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
List list = ctx.Web.Lists.GetByTitle("MyTestList");
ctx.Load(list);
ctx.ExecuteQuery();
var userCustomActions = list.UserCustomActions;
ctx.Load(userCustomActions);
ctx.ExecuteQuery();
DeleteCustomAction(userCustomActions, "RibbonDocumentApproval", ctx);
DeleteCustomAction(userCustomActions, "FormRibbonDocumentApproval", ctx);
DeleteCustomAction(userCustomActions, "ECBDocumentApproval", ctx);
}
}

private static void DeleteCustomAction(UserCustomActionCollection userCustomActions, string customActionName, ClientContext ctx)
{
for (int i = userCustomActions.Count – 1; i >= 0; i–)
{
if (userCustomActions[i].Name == customActionName)
{
userCustomActions[i].DeleteObject();
ctx.ExecuteQuery();
}
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}
catch
{
throw;
}
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}

catch
{
throw;
}
}

It will remove the user custom actions from Ribbon in SharePoint online list using csom.

You may like following SharePoint jsom tutorials:

This SharePoint tutorial, we learned how to add user custom actions (site actions menu) programmatically using JSOM (JavaScript Object Model) in SharePoint Online Office 365.

We will also check, how to remove user custom actions (site actions menu) programmatically using JSOM (JavaScript Object Model) in SharePoint Online.

Also, we saw how to remove UserCustomActions from Ribbon in the SharePoint Online list using csom?

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

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

>