Add, Update and Remove user custom action for list and library items using CSOM

Introduction- We use custom actions to extend the core features of SharePoint.

Here we have discussed about the user custom action for 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.

Step1- First set the credentials of the site and then load the list in which you want to add the user custom action.

Step2- Create the user custom action collection, and create the object for the user custom action for adding the properties.

Step3- On the menu for placing the new custom action we are using the Location property specifies 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 Description property,  and Url specifies an absolute path to a page that defines the action. You can add more properties as per your requirement.

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 = “userName@tenantName.onmicrosoft.com”;

            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;

                    UserCustomAction userCustomAction = userCustomActioncoll.Add();

                    userCustomAction.Location = “EditControlBlock”;

                    userCustomAction.Sequence = 100;

                    userCustomAction.Title = “My First User Custom Action”;

                    userCustomAction.Url = “https://tenantName.sharepoint.com/sites/testSite/SitePages/myPage.aspx“;

                    userCustomAction.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 update the title of the custom action.you can upadate any property of the custom action by using the below codes

-Retrive 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 = “userName@tenantName.onmicrosoft.com”;

            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 replace to ‘Custom Action’.

How to Delete the user custom action for the list item

Here we have retrived the user custom action by its title and used DeleteObject(); 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 = “userName@tenantName.onmicrosoft.com”;

            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 == “Custom Action”)

                        {

                            userCustomAction.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.

Key Words:

1.Add, Update and Remove user custom action for list and library items using CSOM.
2.How to add, modify and delete custom action for list and library items using CSOM.
3.How to add,remove,update user custom action for list or library menu using CSOM.

Share

Add Your Comments

Your email address will not be published. Required fields are marked *