Skip to content

Instantly share code, notes, and snippets.

@rjesh-git
Created October 27, 2015 17:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjesh-git/4f48698371c55655cc10 to your computer and use it in GitHub Desktop.
Save rjesh-git/4f48698371c55655cc10 to your computer and use it in GitHub Desktop.
Add custom user action through Powershell CSOM
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.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment