top of page
Writer's pictureNakkeeran Natarajan

Add Custom Tabs and Groups to the Ribbon on SharePoint using CSOM PowerShell


Introduction

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


Steps Involved

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

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

  1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

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

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

  • Initialize the client context object with the site URL.

  1. $siteURL = ""

  2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  • If you are trying to access SharePoint online site, you need to setup the site credentials with the 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

  • If you are trying to access SharePoint on premise site, 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

We will see how we can add the custom ribbon tabs and the other components to SharePoint list ribbon.

  • Get the existing user action collection. Access the Web and get the existing user actions, using UserCustomActions property. Load and execute the property.

  • Add new custom action to the user actions collection. The necessary parameters for the new action are registration ID, title, registration type and location. Here, we need to set the ribbon tab values.

a. Registration ID - Corresponding List template ID to add the user action. For example, here 100 denotes the custom list templates.

b. Registration Type - Denotes the association type.

c. Location - Custom action location.

  • Build the XML for the new extension. The following are the steps considered:

a. New custom tab has been added to the ribbon. 

b. The new group has been associated with the tab created. 

c. The custom control (button) is added to the group created.

  • Update and execute to see the changes.

The XML extension contains the following elements.

  • New definition for tab with the specified ribbon location. The tab will contain scaling and group components.

a. The scaling will denote the behavior of the tab (sizing). The size is associated to the layout title under the group template.


b. The groups can have one or more group elements. Each group will have the properties like title, description, template (associated to group template) and id. The group also contains the controls that are associated. The controls are button, label, etc. 


i) For each control, set the necessary parameters. The command parameter is associated to UIHandler. The TemplateAlias should match alias name, defined for the control under the group template.

  • Another definition for the group template needs to be created. Here, the new template details for the group, layout mapping and display mode (size) for the controls are defined.

  • The UIHandler section contains necessary JavaScript function for the controls defined. 

The following piece of code shows the operation with the necessary XML.

  1. $web = $ctx.Site.RootWeb  

  2. $userActions = $web.UserCustomActions  

  3. $ctx.Load($userActions)   

  4. $ctx.ExecuteQuery()    $newRibbonItem = $userActions.Add()  

  5. $newRibbonItem.RegistrationId = "100" $newRibbonItem.Title = "Custom Ribbon Tab" $newRibbonItem.RegistrationType = [Microsoft.SharePoint.Client.UserCustomActionRegistrationType]::List  #

  6. $newRibbonItem. Id = "Ribbon.CustomTab"

  7. $newRibbonItem.Location = "CommandUI.Ribbon"

  8. $ribbonUI = '<CommandUIExtension>                      

  9. <CommandUIDefinitions>                          

  10. <CommandUIDefinition Location="Ribbon.Tabs._children">                              

  11. <Tab Id="Ribbon.CustomTab" Title="Custom Tab" Description="Custom Tab with groups and user action" Sequence="100">                                  

  12. <Scaling Id="Ribbon.CustomTab.Scaling">                                      

  13. <MaxSize Id="Ribbon.CustomTab.CustomGroup1.MaxSize"                                         

  14. GroupId="Ribbon.CustomTab.CustomGroup1"                                         

  15. Size="LargeLayout" />                                  

  16. </Scaling>                                 

  17.  <Groups Id="Ribbon.CustomTab.CustomGroups">                                      

  18. <Group Id="Ribbon.CustomTab.CustomGroup1"                                         

  19. Description="Collection of user actions"                                         

  20. Title="CustomGroup1"                                         

  21. Sequence="20"                                         

  22. Template="Ribbon.Templates.LargeLayout">                                          

  23. <Controls Id="Ribbon.CustomTab.CustomGroup1.Controls">                                              

  24. <Button Id="Ribbon.CustomTab.CustomGroup1.ShowAlert" Alt="Show Alert" Sequence="101      Command="ShowRibbonAlert"                                                 

  25.  LabelText="Custom Alert"                                                  

  26. Image32by32="_layouts/15/images/alertme.png"                                                  

  27. Image16by16="_layouts/15/images/alertme.png"                                                  

  28. TemplateAlias="ButtonTemplate" />                                          

  29. </Controls>                                      

  30. </Group>                                  

  31. </Groups>                              

  32. </Tab>                          

  33. </CommandUIDefinition>                          

  34. <CommandUIDefinition Location="Ribbon.Templates._children">                               

  35. <GroupTemplate Id="Ribbon.Templates.LargeLayout">                                   

  36. <Layout Title="LargeLayout" LayoutTitle="LargeLayout">                                       

  37. <Section Alignment="Top" Type="OneRow">                                           

  38. <Row>                                               

  39. <ControlRef DisplayMode="Large" TemplateAlias="ButtonTemplate" />                                           

  40. </Row>                                       

  41. </Section>                                   

  42. </Layout>                               

  43. </GroupTemplate>                           

  44. </CommandUIDefinition>                      

  45. </CommandUIDefinitions>                      

  46. <CommandUIHandlers>                          

  47. <CommandUIHandler Command="ShowRibbonAlert"                              

  48. CommandAction="javascript:alert(''hi'');"/>                      

  49. </CommandUIHandlers>                  

  50. </CommandUIExtension >'  

  51. $newRibbonItem.CommandUIExtension = $ribbonUI  

  52. $newRibbonItem.Update()  

  53. $ctx.Load($newRibbonItem)  

  54. $ctx.ExecuteQuery() 

 The following image shows the tab along with the group and control.






Summary

Thus, you learned how to add the custom ribbon tab, ribbon group to the tab and user actions to the group for a SharePoint list, using CSOM PowerShell on SharePoint 2013 / SharePoint online sites.

0 comments

Comments


bottom of page