Last active
April 8, 2016 01:10
-
-
Save rjesh-git/4bd8ea9f94bb8dca2811 to your computer and use it in GitHub Desktop.
Add UserCustomAction using Powershell CSOM in Office 365
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 site 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() | |
$cUIExtn = "<CommandUIExtension><CommandUIDefinitions><CommandUIDefinition Location=""Ribbon.List.Share.Controls._children""> | |
<Button Id=""Ribbon.List.Share.ExportToExcelButton"" Alt=""Export to Excel"" Sequence=""11"" Command=""Invoke_ExportToExcelButtonRequest"" LabelText=""Export to Excel"" TemplateAlias=""o1"" Image32by32=""_layouts/15/images/placeholder32x32.png"" Image16by16=""_layouts/15/images/placeholder16x16.png"" /> | |
</CommandUIDefinition></CommandUIDefinitions><CommandUIHandlers> | |
<CommandUIHandler Command=""Invoke_ExportToExcelButtonRequest"" CommandAction=""javascript: function exportSchema() { var field = function (Name, Type, ID, rFieldName) {this.DisplayName = Name; this.FieldType = Type; this.ID = ID; this.RealFieldName = rFieldName; }; | |
var listFields = []; for (var i=0; i<ctx.ListSchema.Field.length; i++) {listFields.push(new field(ctx.ListSchema.Field[i].DisplayName, ctx.ListSchema.Field[i].FieldType, ctx.ListSchema.Field[i].ID, ctx.ListSchema.Field[i].RealFieldName));} | |
var form = document.createElement('form'); form.setAttribute('method', 'post'); var hField = document.createElement('input'); hField.setAttribute('type', 'hidden'); | |
hField.setAttribute('name', 'ListFields'); hField.setAttribute('value', JSON.stringify(listFields)); form.appendChild(hField); | |
var hViewID = document.createElement('input'); hViewID.setAttribute('type', 'hidden'); hViewID.setAttribute('name', 'ViewID'); hViewID.setAttribute('value', ctx.view.replace('{','').replace('}','')); form.appendChild(hViewID); | |
var hListID = document.createElement('input'); hListID.setAttribute('type', 'hidden'); hListID.setAttribute('name', 'ListID'); hListID.setAttribute('value', ctx.listName.replace('{','').replace('}','')); form.appendChild(hListID); | |
var hlistName = document.createElement('input'); hlistName.setAttribute('type', 'hidden'); hlistName.setAttribute('name', 'ListName'); hlistName.setAttribute('value', ctx.ListTitle); form.appendChild(hlistName); | |
var hItemIDs = document.createElement('input'); hItemIDs.setAttribute('type', 'hidden'); hItemIDs.setAttribute('name', 'ItemIDs'); hItemIDs.setAttribute('value', '{SelectedItemId}'); form.appendChild(hItemIDs); | |
form.setAttribute('action', 'http://localhost:3000/export'); | |
document.body.appendChild(form); form.submit(); } exportSchema();"" | |
EnabledScript=""javascript: function exporttoexcelenable() { return (true);} exporttoexcelenable();""/></CommandUIHandlers></CommandUIExtension>" | |
$newUCA = $caColl.Add() | |
$newUCA.set_registrationId("100"); | |
$newUCA.set_registrationType("List"); | |
$newUCA.set_location('CommandUI.Ribbon'); | |
$newUCA.set_title('Invoke ExportToExcel Action'); | |
$newUCA.set_commandUIExtension($cUIExtn) | |
$newUCA.update() | |
$ctx.ExecuteQuery() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment