Custom Actions Using CSOM for SharePoint Online Part 3

In the previous posts Part1 and Part 2 we have how to creat the custom actions.  In this post we will see how to delete the custom actions using CSOM.

Remove All Custom Action:

1)  Add a new button(btn_RemoveAllActions) to the web form(CustomActions.aspx)

2)Add the below code in the button click

 string Pwd = ConfigurationManager.AppSettings["Password"].ToString();
 string UserName = ConfigurationManager.AppSettings["Username"].ToString();
 string spsiteurl = ConfigurationManager.AppSettings["SPSiteUrl"].ToString();
 var secure = new SecureString();
      foreach (char c in Pwd)
      {
         secure.AppendChar(c);
       }
       using (var clientContext = new ClientContext(spsiteurl))
          {
             clientContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
             var customlist = clientContext.Web.Lists.GetByTitle("SampleCustomList");
             UserCustomActionCollection collUCA = customlist.UserCustomActions;
             clientContext.Load(collUCA);
             clientContext.ExecuteQuery();
             List<UserCustomAction> ualist = new List<UserCustomAction>();
              for (int i = 0; i < collUCA.Count; i++)
                    {
                        ualist.Add(collUCA[i]);
                    }
               foreach (var aa in ualist)
                  {
                        aa.DeleteObject();
                  }
             clientContext.ExecuteQuery();
             lbl_Success.Text = "Custom  Actions Removed Successfully";

3)Now  run the web Application on click in the  btn_RemoveAllActions

removecustomaction
4)user action will be removed from the Lists

Remove Custom Action by Title:

we can delete the custom action by Title by updating the above code slightly

  if (collUCA[i].Title == "Publish To")
  ualist.Add(collUCA[i]);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s