Add, Update, And Remove User Custom Action For List And Library Items

Introduction

 
We use custom actions to extend the core features of SharePoint.
 
Here we are going to discuss the user custom action for the list item.
 

How to add user custom action for list item

 
We have added user custom action to the drop-down menu that is displayed for list items.
 
Step 1
 
First set the credentials of the site and then load the list in which you want to add the user custom action.
 
Step 2
 
Create the user custom action collection, and create the object for the user custom action for adding the properties.
 
Step 3
 
On the menu for placing the new custom action, we are using the Location property. EditControlBlock, Sequence specifies an order of placement in relation to other user custom actions, Title specifies the name of the custom action, you can also add the description to custom action by specifying a Description property, and URL specifies an absolute path to a page that defines the action. As per your requirement, you can also add more properties.
 
Follow the below code snippet,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using System.Security;  
  8. namespace AddCustomAction {  
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.                 string userName = "[email protected]";  
  12.                 string password = "passWord";  
  13.                 string siteUrl = "https://tenantName.sharepoint.com/sites/testSite";  
  14.                 SecureString securePassword = new SecureString();  
  15.                 foreach(char c in password) {  
  16.                     securePassword.AppendChar(c);  
  17.                 }  
  18.                 var cred = new SharePointOnlineCredentials(userName, securePassword);  
  19.                 using(ClientContext ctx = new ClientContext(siteUrl)) {  
  20.                     try {  
  21.                         ctx.Credentials = cred;  
  22.                         Web oWeb = ctx.Web;  
  23.                         ctx.Load(oWeb);  
  24.                         List oList = oWeb.Lists.GetByTitle("My list");  
  25.                         ctx.Load(oList);  
  26.                         ctx.ExecuteQuery();  
  27.                         Microsoft.SharePoint.Client.UserCustomActionCollection userCustomActioncoll = oList.UserCustomActions;  
  28.                         User CustomAction userCustomAction = userCustomActioncoll.Add();  
  29.                         user CustomAction.Location = "EditControlBlock";  
  30.                         user CustomAction.Sequence = 100;  
  31.                         user CustomAction.Title = "My First User Custom Action";  
  32.                         user CustomAction.Url = "https://tenantName.sharepoint.com/sites/testSite/SitePages/myPage.aspx";  
  33.                         user CustomAction.Update();  
  34.                         ctx.Load(oList, list => list.UserCustomActions);  
  35.                         ctx.ExecuteQuery();  
  36.                         Console.WriteLine("Successfully added user Custom Action");  
  37.                     } catch (Exception e) {  
  38.                         Console.WriteLine(e);  
  39.                     }  
  40.                 }  
In the below screenshot you can see the custom action 'My First User Custom Action' is added successfully to the list,
 
Add, Update, And Remove User Custom Action For List And Library Items U
 

How to update the user custom action for list

 
Here we are updating the title of the custom action. You can update any property of the custom action by using the below codes.
 
Retrieve the custom action from the collection by using its Title. Then update the Title.
 
Follow the below code snippet,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Security;  
  7. using Microsoft.SharePoint.Client;  
  8. namespace updateCustomAction {  
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             string userName = "[email protected]";  
  12.             string password = "passWord";  
  13.             string siteUrl = "https://tenantName.sharepoint.com/sites/testSite";  
  14.             SecureString securePassword = new SecureString();  
  15.             foreach(char c in password) {  
  16.                 securePassword.AppendChar(c);  
  17.             }  
  18.             var cred = new SharePointOnlineCredentials(userName, securePassword);  
  19.             using(ClientContext ctx = new ClientContext(siteUrl)) {  
  20.                 try {  
  21.                     ctx.Credentials = cred;  
  22.                     Web oWeb = ctx.Web;  
  23.                     ctx.Load(oWeb);  
  24.                     List oList = oWeb.Lists.GetByTitle("My list");  
  25.                     ctx.Load(oList);  
  26.                     ctx.ExecuteQuery();  
  27.                     Microsoft.SharePoint.Client.UserCustomActionCollection userCustomActioncoll = oList.UserCustomActions;  
  28.                     ctx.Load(userCustomActioncoll, userCustomActions => userCustomActions.Include(userCustomAction => userCustomAction.Title));  
  29.                     ctx.ExecuteQuery();  
  30.                     foreach(UserCustomAction userCustomAction in userCustomActioncoll) {  
  31.                         if (userCustomAction.Title == "My First User Custom Action") {  
  32.                             userCustomAction.Title = "Custom Action";  
  33.                             userCustomAction.Update();  
  34.                             ctx.ExecuteQuery();  
  35.                             Console.WriteLine("Successfully update user Custom Action");  
  36.                         }  
  37.                     }  
  38.                 } catch (Exception e) {  
  39.                     Console.WriteLine(e);  
  40.                 }  
  41.             }  
  42.         }  
  43.     }  
  44. }  
You can see the below screenshot custom action title is replaced with 'Custom Action'.
 
Add, Update, And Remove User Custom Action For List And Library Items U
 

How to delete the user custom action for the list item

 
Here we have retrieved the user custom action by its title and used delete object (); to remove the custom action.
 
Follow the below code snippet,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Security;  
  7. using Microsoft.SharePoint.Client;  
  8. namespace deleteCustomAction {  
  9.     class Program {  
  10.         static void Main(string[] args) {  
  11.             string userName = "[email protected]";  
  12.             string password = "passWord";  
  13.             string siteUrl = "https://tenantName.sharepoint.com/sites/testSite";  
  14.             Secure String securePassword = new SecureString();  
  15.             foreach(char c in password) {  
  16.                 securePassword.AppendChar(c);  
  17.             }  
  18.             var cred = new SharePointOnlineCredentials(userName, securePassword);  
  19.             using(ClientContext ctx = new ClientContext(siteUrl)) {  
  20.                 try {  
  21.                     ctx.Credentials = cred;  
  22.                     Web oWeb = ctx.Web;  
  23.                     ctx.Load(oWeb);  
  24.                     List oList = oWeb.Lists.GetByTitle("My list");  
  25.                     ctx.Load(oList);  
  26.                     ctx.ExecuteQuery();  
  27.                     Microsoft.SharePoint.Client.UserCustomActionCollection userCustomActioncoll = oList.UserCustomActions;  
  28.                     ctx.Load(userCustomActioncoll, user CustomActions => userCustomActions.Include(user CustomAction => userCustomAction.Title));  
  29.                     ctx.ExecuteQuery();  
  30.                     foreach(UserCustomAction userCustomAction in userCustomActioncoll) {  
  31.                         if (userCustomAction.Title == "Custom Action") {  
  32.                             user CustomAction.DeleteObject();  
  33.                             ctx.ExecuteQuery();  
  34.                             Console.WriteLine("Successfully deleted user Custom Action");  
  35.                         }  
  36.                     }  
  37.                 } catch (Exception e) {  
  38.                     Console.WriteLine(e);  
  39.                 }  
  40.             }  
  41.         }  
  42.     }  
  43. }  
Now you can see that custom action is removed.
 
Add, Update, And Remove User Custom Action For List And Library Items U