Overview
When it comes to using SharePoint 2010 Client Object Model (CSOM) we need to be ready for certain kind of limitations in PowerShell. First of all, it concerns the usage of Generics Methods, for the example ClientRuntimeContext.Load<T> method:
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
public void Load<T>( | |
T clientObject, | |
params Expression<Func<T, Object>>[] retrievals | |
) | |
where T : ClientObject |
An attempt to call the method ClientRuntimeContext.Load<T> directly will result in the following error
This is a limitation of PowerShell (V1, V2) AFIK. There are several options how to bypass this limitation but in this post I would like to concentrate only on one technique that was originally described in the post Invoking Generic Methods on Non-Generic Classes in PowerShell. The basic idea is to replace the call for ClientRuntimeContext.Load<T> method with the following one:
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
Function Invoke-LoadMethod() { | |
param( | |
$ClientObject = $(throw "Please provide an Client Object instance on which to invoke the generic method") | |
) | |
$ctx = $ClientObject.Context | |
$load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") | |
$type = $ClientObject.GetType() | |
$clientObjectLoad = $load.MakeGenericMethod($type) | |
$clientObjectLoad.Invoke($ctx,@($ClientObject,$null)) | |
} |
For invoking a generic methods we utilize MethodInfo.MakeGenericMethod method. Below are demonstrated some examples of usage SharePoint 2010 Client Object Model (CSOM) in PowerShell.
Example: load Web client object
Let’s start with a simple example for loading Web Client Object:
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\14\ISAPI\Microsoft.SharePoint.Client.dll' | |
Add-Type –Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.Runtime.dll' | |
function PrintWebProperties() | |
{ | |
param( | |
[Parameter(Mandatory=$true)][string]$url, | |
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials | |
) | |
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url) | |
$ctx.Credentials = $credentials | |
$web = $ctx.Web | |
Invoke-LoadMethod –ClientObject $web | |
$ctx.ExecuteQuery() | |
Write-Host "Web Properties:" | |
Write-Host "Title: $($web.Title)" | |
Write-Host "Url: $($web.ServerRelativeUrl)" | |
} | |
$credentials = New-Object System.Net.NetworkCredential('username', 'password','domain') | |
$url = 'http://contoso.intranet.com/' | |
PrintWebProperties $url $credentials |
Example: create Wiki page via CSOM
The example below demonstrates how to create wiki page via CSOM.
C# version:
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
/// <summary> | |
/// Create Wiki page via CSOM | |
/// </summary> | |
/// <param name="webUrl"></param> | |
/// <param name="pageName"></param> | |
/// <param name="pageContent"></param> | |
public static void CreateWikiPage(string webUrl, string pageName,string pageContent) | |
{ | |
const string templateRedirectionPageMarkup = "<%@ Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%@ Reference VirtualPath=\"~TemplatePageUrl\" %> <%@ Reference VirtualPath=\"~masterurl/custom.master\" %>"; | |
using (var ctx = new ClientContext(webUrl)) | |
{ | |
var wikiPages = ctx.Web.Lists.GetByTitle("Pages"); | |
ctx.Load(wikiPages); | |
ctx.ExecuteQuery(); | |
var file = new FileCreationInformation | |
{ | |
Url = pageName, | |
Content = Encoding.UTF8.GetBytes(templateRedirectionPageMarkup), | |
Overwrite = true | |
}; | |
var wikiFile = wikiPages.RootFolder.Files.Add(file); | |
ctx.Load(wikiFile); | |
ctx.ExecuteQuery(); | |
var wikiPage = wikiFile.ListItemAllFields; | |
wikiPage["PublishingPageContent"] = pageContent; | |
wikiPage["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page"; | |
wikiPage.Update(); | |
ctx.ExecuteQuery(); | |
} | |
} |
PowerShell version:
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\14\ISAPI\Microsoft.SharePoint.Client.dll' | |
Add-Type –Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.Runtime.dll' | |
function CreateWikiPage() | |
{ | |
param( | |
[Parameter(Mandatory=$true)][string]$webUrl, | |
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials, | |
[Parameter(Mandatory=$true)][string]$pageName, | |
[Parameter(Mandatory=$true)][string]$pageContent | |
) | |
$templateRedirectionPageMarkup = '<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> <%@ Reference VirtualPath="~TemplatePageUrl" %> <%@ Reference VirtualPath="~masterurl/custom.master" %>'; | |
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl) | |
$ctx.Credentials = $credentials | |
$wikiPages = $ctx.Web.Lists.GetByTitle("Pages") | |
Invoke-LoadMethod –ClientObject $wikiPages | |
$ctx.ExecuteQuery() | |
$file = New-Object Microsoft.SharePoint.Client.FileCreationInformation | |
$file.Url = $pageName | |
$file.Content = [System.Text.Encoding]::UTF8.GetBytes($templateRedirectionPageMarkup) | |
$file.Overwrite = $true | |
$wikiFile = $wikiPages.RootFolder.Files.Add($file) | |
Invoke-LoadMethod –ClientObject $wikiFile | |
$wikiPage = $wikiFile.ListItemAllFields | |
$wikiPage["PublishingPageContent"] = $pageContent | |
$wikiPage["PublishingPageLayout"] = "/_catalogs/masterpage/EnterpriseWiki.aspx, Basic Page" | |
$wikiPage.Update() | |
$ctx.ExecuteQuery(); | |
} | |
$credentials = New-Object System.Net.NetworkCredential('username', 'password','domain') | |
$webUrl = 'http://contoso.intranet.com/knowledgebase/' | |
$pageName = 'MyFirstWikiPage.aspx' | |
$pageContent = '<h1>Welcome to the Knowledge Base!</h1>' | |
CreateWikiPage $webUrl $credentials $pageName $pageContent |
Summary
In contrary to article Using PowerShell to Get Data from a SharePoint 2010 List that explains how to execute generic methods via inline C# in PowerShell, this post demonstrates how to utilize Generics Methods in PowerShell natively.
Reblogged this on Sutoprise Avenue, A SutoCom Source.
Hi Vadim,
sounds good, but unfortunately doesn’t work at my side. I’m doing this on Win2008 R2 x64 in PowerShell v2 and getting following error in the line “$clientObjectLoad.Invoke($ctx,@($ClientObject,$null))”
Exception calling “Invoke” with “2” argument(s): “Object of type ‘System.Management.Automation.PSObject’ cannot be converted to type ‘Microsoft.SharePoint.Client.Web’.”
At XXXXXX-Fileinformation
+ $clientObjectLoad.Invoke <<<< ($ctx,@($ClientObject,$null))
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Do you have an idea why?
Greetings
Anatoli
Hi Anatoli, I faced same problem. it’s resolved by installing powershell 3.0. your powershell version is old.
Thanks Terry, you’re right, the specified script is compatible with PowerShell 3.0 or above.
Anatoli, sorry for a late response, for PowerShell 2.0 you could utilize the following script:
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
Invoke-LoadMethodV2.ps1
hosted with ❤ by GitHub
Hi Vadim, thank you for reply.
I’ll try it!
Greetings
With the Invoke-LoadMethodV2() I get error “An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.”
E.g., when getting content types with
$contentTypes = $web.AvailableContentTypes
#$ClientContext.Load($contentTypes)
Invoke-LoadMethodV2 -Object $contentTypes -Context $ClientContext
$ClientContext.ExecuteQuery()
Pingback: SharePoint Online Client Object Model and PowerShell: Three tips by @mattein
Pingback: SPCAF | SharePoint Online Client Object Model and PowerShell: Three tips
Would this be feasible to use for Sharepoint Online as well?
I haven’t verified it against SPO but my guess it still should be relevant
Seems like it works, had to change credentials to Microsoft.SharePoint.Client.SharePointOnlineCredentials from System.Net.NetworkCredential. Now to figure out how to automatically add tags…
Pingback: SharePoint Online PowerShell and the Client Object Model: Three tips - Rencore