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:
- SharePoint Online: Add site column to list programmatically using csom
- Get ModerationStatus of document using client managed object model (csom) in SharePoint online
- SharePoint Online: Activate Workflows can use app permissions Feature programmatically using CSOM
- Upload large files to SharePoint online document library using PowerShell csom
- SharePoint 2016 CSOM Check if Column Exists or Not in SharePoint List
- The file is not checked out. You must first check out this document before making changes SharePoint 2013 CSOM
- SharePoint Online: Get content type id by name programmatically using CSOM
- SharePoint online Delete list programmatically using CSOM C#.Net managed object model code
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.
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 EnjoySharePoint.com