Some tips and tricks of using SharePoint Client Object Model in PowerShell. Part 1

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:

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 PSGenericMethods

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:

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:

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:

/// <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();
}
}

view raw
CreateWikiCSOM.cs
hosted with ❤ by GitHub

PowerShell version:

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

view raw
CreateWikiPage.ps1
hosted with ❤ by GitHub

MyFirstWikiPage

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.

References