Add And Delete Custom Action In Site Menu Using CSOM

Introduction

In this blog, we are going to learn how to add and delete the custom action in the site menu using CSOM. Custom actions are linked or custom menu items that we can add to the SharePoint UI. This can be added to a site or the list/library menu.

Add Custom Action to site menu

Here we have added the custom action in the site menu. Please copy the below code snippet to add the custom action in the site menu.

using System;
using System.Collections.Generic;
using System.Security;
using Microsoft.SharePoint.Client;
namespace CustomAction {
    class Program {
        static void Main(string[] args) {
            string userName = "user@contoso.onmicrosoft.com";
            string password = "Password";
            string siteUrl = "https://contoso.sharepoint.com/sites/SiteName";
            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);
                    UserCustomActionCollection userCustActionColl = oWeb.UserCustomActions;
                    UserCustomAction objUserCustAction = userCustActionColl.Add();
                    objUserCustAction.Location = "Microsoft.SharePoint.StandardMenu";
                    objUserCustAction.Group = "SiteActions";
                    objUserCustAction.Sequence = 101;
                    objUserCustAction.Title = "Site User Custom Action";
                    objUserCustAction.Description = "This description appears on the Site Actions menu.";
                    objUserCustAction.Url = "https://contoso.sharepoint.com/sites/SiteName/SitePages/myPage.aspx";
                    objUserCustAction.Update();
                    ctx.Load(oWeb, webSite => webSite.UserCustomActions);
                    ctx.ExecuteQuery();
                    Console.WriteLine("Successfully added user Custom Action");
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }
        }
    }
}

Delete Custom Action from the site menu

Here we have deleted the custom action from the site menu.

Please copy the below code snippet to delete the custom action from the site menu.

using System;
using System.Collections.Generic;
using System.Security;
using Microsoft.SharePoint.Client;
namespace CustomAction {
    class Program {
        static void Main(string[] args) {
                string userName = "user@contoso.onmicrosoft.com";
                string password = "Password";
                string siteUrl = "https://contoso.sharepoint.com/sites/SiteName";
                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);
                            UserCustomActionCollection userCustActionColl = oWeb.UserCustomActions;
                            ctx.Load(userCustActionColl, userCustomActions => userCustomActions.Include(userCustomAction => userCustomAction.Title));
                            ctx.ExecuteQuery();
                            foreach(UserCustomAction objUserCustomAction in userCustActionColl) {
                                if (objUserCustomAction.Title == "Site User Custom Action") {
                                    objUserCustomAction.DeleteObject();
                                    ctx.ExecuteQuery();
                                    Console.WriteLine("Successfully deleted user Custom Action");
                                }
                            }

After adding the custom action menu

Add And Delete Custom Action In Site Menu Using CSOM

After deleting the custom action

Add And Delete Custom Action In Site Menu Using CSOM