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

8 thoughts on “Some tips and tricks of using SharePoint Client Object Model in PowerShell. Part 1

  1. 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

      • 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:

      • 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()

  2. Pingback: SharePoint Online Client Object Model and PowerShell: Three tips by @mattein

  3. Pingback: SPCAF | SharePoint Online Client Object Model and PowerShell: Three tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s