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,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using System.Security;
- namespace AddCustomAction {
- class Program {
- static void Main(string[] args) {
- string userName = "[email protected]";
- string password = "passWord";
- string siteUrl = "https://tenantName.sharepoint.com/sites/testSite";
- SecureString securePassword = new SecureString();
- foreach(char c in password) {
- securePassword.AppendChar(c);
- }
- var cred = new SharePointOnlineCredentials(userName, securePassword);
- using(ClientContext ctx = new ClientContext(siteUrl)) {
- try {
- ctx.Credentials = cred;
- Web oWeb = ctx.Web;
- ctx.Load(oWeb);
- List oList = oWeb.Lists.GetByTitle("My list");
- ctx.Load(oList);
- ctx.ExecuteQuery();
- Microsoft.SharePoint.Client.UserCustomActionCollection userCustomActioncoll = oList.UserCustomActions;
- User CustomAction userCustomAction = userCustomActioncoll.Add();
- user CustomAction.Location = "EditControlBlock";
- user CustomAction.Sequence = 100;
- user CustomAction.Title = "My First User Custom Action";
- user CustomAction.Url = "https://tenantName.sharepoint.com/sites/testSite/SitePages/myPage.aspx";
- user CustomAction.Update();
- ctx.Load(oList, list => list.UserCustomActions);
- ctx.ExecuteQuery();
- Console.WriteLine("Successfully added user Custom Action");
- } catch (Exception e) {
- Console.WriteLine(e);
- }
- }
In the below screenshot you can see the custom action 'My First User Custom Action' is added successfully to the list,
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,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Security;
- using Microsoft.SharePoint.Client;
- namespace updateCustomAction {
- class Program {
- static void Main(string[] args) {
- string userName = "[email protected]";
- string password = "passWord";
- string siteUrl = "https://tenantName.sharepoint.com/sites/testSite";
- SecureString securePassword = new SecureString();
- foreach(char c in password) {
- securePassword.AppendChar(c);
- }
- var cred = new SharePointOnlineCredentials(userName, securePassword);
- using(ClientContext ctx = new ClientContext(siteUrl)) {
- try {
- ctx.Credentials = cred;
- Web oWeb = ctx.Web;
- ctx.Load(oWeb);
- List oList = oWeb.Lists.GetByTitle("My list");
- ctx.Load(oList);
- ctx.ExecuteQuery();
- Microsoft.SharePoint.Client.UserCustomActionCollection userCustomActioncoll = oList.UserCustomActions;
- ctx.Load(userCustomActioncoll, userCustomActions => userCustomActions.Include(userCustomAction => userCustomAction.Title));
- ctx.ExecuteQuery();
- foreach(UserCustomAction userCustomAction in userCustomActioncoll) {
- if (userCustomAction.Title == "My First User Custom Action") {
- userCustomAction.Title = "Custom Action";
- userCustomAction.Update();
- ctx.ExecuteQuery();
- Console.WriteLine("Successfully update user Custom Action");
- }
- }
- } catch (Exception e) {
- Console.WriteLine(e);
- }
- }
- }
- }
- }
You can see the below screenshot custom action title is replaced with 'Custom Action'.
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,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Security;
- using Microsoft.SharePoint.Client;
- namespace deleteCustomAction {
- class Program {
- static void Main(string[] args) {
- string userName = "[email protected]";
- string password = "passWord";
- string siteUrl = "https://tenantName.sharepoint.com/sites/testSite";
- Secure String securePassword = new SecureString();
- foreach(char c in password) {
- securePassword.AppendChar(c);
- }
- var cred = new SharePointOnlineCredentials(userName, securePassword);
- using(ClientContext ctx = new ClientContext(siteUrl)) {
- try {
- ctx.Credentials = cred;
- Web oWeb = ctx.Web;
- ctx.Load(oWeb);
- List oList = oWeb.Lists.GetByTitle("My list");
- ctx.Load(oList);
- ctx.ExecuteQuery();
- Microsoft.SharePoint.Client.UserCustomActionCollection userCustomActioncoll = oList.UserCustomActions;
- ctx.Load(userCustomActioncoll, user CustomActions => userCustomActions.Include(user CustomAction => userCustomAction.Title));
- ctx.ExecuteQuery();
- foreach(UserCustomAction userCustomAction in userCustomActioncoll) {
- if (userCustomAction.Title == "Custom Action") {
- user CustomAction.DeleteObject();
- ctx.ExecuteQuery();
- Console.WriteLine("Successfully deleted user Custom Action");
- }
- }
- } catch (Exception e) {
- Console.WriteLine(e);
- }
- }
- }
- }
- }
Now you can see that custom action is removed.