Delete User Custom Action using CSOM SharePoint Online

This SharePoint CSOM tutorial explains, how we can remove or delete 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

SharePoint delete user custom action using CSOM

Here I have a SharePoint Online list where we have added UserCustomActions buttons to Ribbon, FormRibbon and to ECB menu.

For one of our requirement, we need to remove the ribbon buttons. So I wrote a console application by using .Net managed object model code.

Below is the full code:

Here we are retrieving the user custom actions for the list by using the below client object model 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;
}
}

You may like following SharePoint CSOM tutorials:

Once you run the above code, it will delete the user custom action button from the SharePoint ribbon, form ribbon, and ECB menu from the SharePoint list.

>