How to remove UserCustomActions from Ribbon in SharePoint online list using csom?

Here we will discuss how we can 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 requirement, we need to remove the ribbon buttons. So I wrote console application by using.Net managed object model code.

SharePoint deveopment training course

Also you can read:

Free SharePoint Migration Tool from Microsoft step by step migration from SharePoint 2016 on premise to SharePoint Online

SharePoint Approved and Rejected buttons not appearing in task form in Visual Studio 2015 workflow

Below is the full 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;

}

}

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

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 →