Add/Remove Custom Ribbon Items on SharePoint List Using CSOM PowerShell

Introduction

In this article, you will learn how to add or remove custom ribbon items (user actions) on SharePoint list using CSOM Powershell on SharePoint 2013 / SharePoint online sites.

Steps Involved

The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.

  1. Add the references using the Add-Type command with the necessary reference paths. The necessary references are Client.dll, Client.Runtime.dll and publishing.dll.

    Add-Type -Path

    "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

    Add-Type -Path

    "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

    Add-Type -Path

    "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"

  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  3. If you are trying to access SharePoint Online site then you need to setup the site credentials with credentials parameter and load it to the client context. 
    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Not required for on premise site - End  
  4. If you are trying to access the SharePoint on premise site then the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.
    1. #Credentials for on premise site - Start  
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    3. $creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)  
    4. $ctx.Credentials = $creds  
    5. #Credentials for on premise site - End  

Add Custom Ribbon Items (User Actions) to List

We will see how we can add the custom ribbon items (actions) to the SharePoint list. 
  • Get the existing user action collection. Access the web and get the existing user actions using UserCustomActions property. Load and execute the property.
    1. $web = $ctx.Site.RootWeb  
    2. $userActions = $web.UserCustomActions  
    3. $ctx.Load($userActions)   
    4. $ctx.ExecuteQuery()   
  • Then to the user actions collection, add new custom action. The necessary parameters for new action are registration id, title, registration type and location.

    • Registration Id - Corresponding List template id to add the user action. For example, here 100 denotes the custom list templates.
    • Registration Type -  Denotes association type.
    • location - Custom Action location.
      1. $newRibbonItem = $userActions.Add()  
      2. $newRibbonItem.RegistrationId = "100"  
      3. $newRibbonItem.Title = "Custom Ribbon"  
      4. $newRibbonItem.RegistrationType = [Microsoft.SharePoint.Client.UserCustomActionRegistrationType]::List  
      5. $newRibbonItem.Location = "CommandUI.Ribbon"  
  • Build the xml for the new definition with correspoding handler. In this case, I have considered button as a custom action and associated alert box to the action. You can build your own logic on command action parameter. Location in the xml denotes the place where item will be placed.

  • Update and execute the query.
    1. $ribbonUI = '<CommandUIExtension>  
    2.                     <CommandUIDefinitions>  
    3.                         <CommandUIDefinition Location="Ribbon.List.Actions.Controls._children">  
    4.                             <Button Id="Ribbon.List.Actions.ShowAlert" Alt="Show Alert" Sequence="100"  
    5.                                  Command="ShowRibbonAlert"  
    6.                                  LabelText="Custom Alert"  
    7.                                  TemplateAlias="o1"  
    8.                                  Image32by32="_layouts/15/images/alertme.png"  
    9.                                  Image16by16="_layouts/15/images/alertme.png" />  
    10.                        </CommandUIDefinition>  
    11.                     </CommandUIDefinitions>  
    12.                     <CommandUIHandlers>  
    13.                         <CommandUIHandler Command="ShowRibbonAlert"  
    14.                              CommandAction="javascript:alert(''hi'');"/>  
    15.                     </CommandUIHandlers>  
    16.                 </CommandUIExtension >'  
    17.   
    18. $newRibbonItem.CommandUIExtension = $ribbonUI  
    19. $newRibbonItem.Update()  
    20. $ctx.Load($newRibbonItem)  
    21. $ctx.ExecuteQuery()  
After executing the above steps you can find the new ribbon item on the sharepoint custom list (list tab). The following snapshot depicts the custom ribbon action.

Remove Custom Ribbon Items (User Actions) from List

We will see how we can remove the custom ribbon items (actions) from the SharePoint list.
  • Get the existing user action collection. Access the web and get the existing user actions using UserCustomActions property. Load and execute the property.
    1. $web = $ctx.Site.RootWeb  
    2. $userActions = $web.UserCustomActions  
    3. $ctx.Load($userActions)   
    4. $ctx.ExecuteQuery()   
  • Delete items

    • If items exists, use filter conditions and filter the ribbon items and assign items in to an array to delete. (Multiple items can not be deleted from the result variable, since the items are present in the enumeration variable. So we are assigning it to array and deleting the items or actions)

    • From the array, delete the items and finally execute the query.
      1. $itemsToDelete = @()  
      2. if($userActions.Count -le 0){  
      3.     Write-Host "No Ribbon Items found to delete"  
      4. }  
      5. else{  
      6.     foreach($userAction in $userActions){  
      7.         $itemsToDelete += $userAction                  
      8.     }  
      9.     foreach($item in $itemsToDelete){  
      10.         Write-Host "Deleting Ribbon Item : " $item.Title  
      11.         $item.DeleteObject()  
      12.     }  
      13.     $ctx.ExecuteQuery()  
      14. }  
The above operation deletes all the custom ribbon items from the site. You can filter out to the exact ribbon item from the for each loop and delete the item(s).
 
Summary

Thus, you have learned how to add or remove custom ribbon items to/from the SharePoint lists using CSOM PowerShell on SharePoint 2013 / SharePoint online sites.

Up Next
    Ebook Download
    View all
    Learn
    View all