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:

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:

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:

Example: create Wiki page via CSOM

The example below demonstrates how to create wiki page via CSOM.

C# version:

PowerShell version:

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

Advertisements