PowerShell CSOM - Add custom user action



There are multiple approaches to add custom user action to your Office 365 SharePoint site In this post I'm using the Powershell CSOM, and will be creating a simple custom action to show in ribbon control for custom list template. The ribbon control custom action on click will show an alert with the number of items in the custom list. First, we'll start with adding DLL references for the SharePoint Client, which will help with the remote API calls.

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
# Authenticate with the SharePoint site.
#
$siteUrl = Read-Host -Prompt "Enter web url:"
$username = Read-Host -Prompt "Enter Username:"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
# SharePoint Online
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials

Next, lets get started with creating the client context and getting the existing user custom actions. Also check if the custom action already exists, and delete if already available.
$rootWeb = $ctx.Web
$ctx.Load($rootWeb)
$caColl = $rootWeb.get_userCustomActions()
$ctx.Load($caColl)
$ctx.ExecuteQuery()
$caColl | ForEach-Object {
if ($_.title -eq 'Invoke GetItemsCount Action') {
$_.DeleteObject()
Write-Host 'Existing item deleted.'
}
}

Lets initialize the properties for the new custom action, make sure that in CommandUIExtension the quotesare escaped properly. Also exercise care with the line breaks, when you are adding a complex JavaScript inside that block. If not it may prove costly later.
$cUIExtn = "<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location=""Ribbon.List.Share.Controls._children"">
<Button Id=""Ribbon.List.Share.GetItemsCountButton"" Alt=""Get list items count"" Sequence=""11"" Command=""Invoke_GetItemsCountButtonRequest"" LabelText=""Get Items Count"" TemplateAlias=""o1"" Image32by32=""_layouts/15/images/placeholder32x32.png"" Image16by16=""_layouts/15/images/placeholder16x16.png"" />
</CommandUIDefinition></CommandUIDefinitions><CommandUIHandlers>
<CommandUIHandler Command=""Invoke_GetItemsCountButtonRequest"" CommandAction=""javascript: alert('Total items in this list: '+ ctx.TotalListItems);""
EnabledScript=""javascript: function checkEnable() { return (true);} checkEnable();""/></CommandUIHandlers></CommandUIExtension>"
$newUCA = $caColl.Add()
$newUCA.set_registrationId("100");
$newUCA.set_registrationType("List");
$newUCA.set_location('CommandUI.Ribbon');
$newUCA.set_title('Invoke GetItemsCount Action');
$newUCA.set_commandUIExtension($cUIExtn)
$newUCA.update()
$ctx.ExecuteQuery()
Write-Host 'Custom action added.'

Lets put altogether, when executed you'll be prompted for web site url, username and password. Provide the web site where the custom action as to be added. To validate go to any custom list and in the ribbon click "Get Items Count" button, you'll prompted with number of items in that list.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
# Authenticate with the SharePoint site.
#
$siteUrl = Read-Host -Prompt "Enter web url:"
$username = Read-Host -Prompt "Enter Username:"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
# SharePoint Online
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$rootWeb = $ctx.Web
$ctx.Load($rootWeb)
$caColl = $rootWeb.get_userCustomActions()
$ctx.Load($caColl)
$ctx.ExecuteQuery()
$caColl | ForEach-Object {
if ($_.title -eq 'Invoke GetItemsCount Action') {
$_.DeleteObject()
Write-Host 'Existing item deleted.'
}
}
$cUIExtn = "<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location=""Ribbon.List.Share.Controls._children"">
<Button Id=""Ribbon.List.Share.GetItemsCountButton"" Alt=""Get list items count"" Sequence=""11"" Command=""Invoke_GetItemsCountButtonRequest"" LabelText=""Get Items Count"" TemplateAlias=""o1"" Image32by32=""_layouts/15/images/placeholder32x32.png"" Image16by16=""_layouts/15/images/placeholder16x16.png"" />
</CommandUIDefinition></CommandUIDefinitions><CommandUIHandlers>
<CommandUIHandler Command=""Invoke_GetItemsCountButtonRequest"" CommandAction=""javascript: alert('Total items in this list: '+ ctx.TotalListItems);""
EnabledScript=""javascript: function checkEnable() { return (true);} checkEnable();""/></CommandUIHandlers></CommandUIExtension>"
$newUCA = $caColl.Add()
$newUCA.set_registrationId("100");
$newUCA.set_registrationType("List");
$newUCA.set_location('CommandUI.Ribbon');
$newUCA.set_title('Invoke GetItemsCount Action');
$newUCA.set_commandUIExtension($cUIExtn)
$newUCA.update()
$ctx.ExecuteQuery()
Write-Host 'Custom action added.'


Happy coding! Peace!!

Comments