How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint Office 365


Sathish Nadarajan
SharePoint MVP
Published On :   23 Oct 2016
Visit Count
Today :  1    Total :   4401
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In some of the requirements, we might be creating the custom components, pages, links to external applications, APPs (Add-in), which require some link from the home page. Either we can provide a hyperlinks on the Screens or we can attach those links on the Site Actions Menu. Obviously, adding to the Site Actions menu, is purely discrete to the customer’s wish. But, even then, let us see how to Create the Menu Items, programmatically. This will be useful, during the time of deployment.

In the Site Actions Menu, the default items will be listed as below.

clip_image002

Now, we are going to add a link at the bottom as below.

clip_image004

The piece of code to Add and Remove is as follows.

 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using Newtonsoft.Json.Linq;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
 
     class Program
     {
         static void Main(string[] args)
         {
 		RemoveLinkOnCustomAction();
             AddLinkOnCustomAction();
         }
 
         
 
         
 
         public static void RemoveLinkOnCustomAction()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "http:// **********.sharepoint.com/sites/communitysite";
             string userName = "Sathish@**********.onmicrosoft.com";
             string password = "**********";
 
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web);
                 ctx.Load(web.UserCustomActions);
                 ctx.ExecuteQueryRetry();
                 foreach (var customAction in web.UserCustomActions)
                 {
                     if (customAction.Name == "Test Action")
                     {
                         customAction.DeleteObject();
                         break;
                     }
                 }
                 ctx.Web.Update();
                 ctx.Web.UserCustomActions.RefreshLoad();
                 ctx.ExecuteQueryRetry();
             }
         }
 
         public static void AddLinkOnCustomAction()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https:// **********.sharepoint.com/sites/communitysite";
             string userName = "Sathish@**********.onmicrosoft.com";
             string password = "******";
 
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web);
                 ctx.Load(web.UserCustomActions);
                 ctx.ExecuteQueryRetry();
 
                 BasePermissions permission = new BasePermissions();
                 permission.Set(PermissionKind.ManageWeb);
 
                 UserCustomAction siteAction = ctx.Web.UserCustomActions.Add();
 
                 siteAction.Group = "SiteActions";
                 siteAction.Location = "Microsoft.SharePoint.StandardMenu";
                 siteAction.Name = "Test Action";
                 siteAction.Sequence = 1000;
                 siteAction.Rights = permission;
 
                 siteAction.Url = "www.google.com";
                 siteAction.Title = "Link to Google";
                 siteAction.Update();
 
                 ctx.ExecuteQueryRetry();
             }
         }
 
 
     }
 
 }
 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Protect Your SharePoint