Consuming the SharePoint 2013 REST API from PowerShell

Introduction

SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the  SharePoint CSOM and in addition to CSOM, REST API opens up a huge capabilities, in particular for administering and automating SharePoint Online when used with PowerShell.

Sending  REST requests to a SharePoint Online 

In the previous post we’ve already covered how to perform read operations by sending HTTPS requests to SharePoint RESTful web services. This time we are going to extend PowerShell script  in order to support all the CRUD operations.

The Invoke-RestSPO function sends  HTTPS requests to SharePoint REST web services that returns richly structured data (JSON)

Request Digests

Since SharePoint requires the user to include a request digest value with each create, update and delete operation, an additional request is invoked using Get-SPOContextInfo function to request Context Info entity that contains request digest value.

 ETag

In order to avoid an additional request, “*” eTag value is used to match any eTag value resulting in the operation being performed regardless of the actual value.

Lists manipulation using REST API in PowerShell

This section contains sample code for all of the CRUD operations.

References

 

Advertisements

6 thoughts on “Consuming the SharePoint 2013 REST API from PowerShell

  1. Pingback: Working with folders and files via SharePoint 2013 REST in PowerShell | YASP Blog

  2. Great post Vadim !
    Though I was wondering how to add a list item using PowerShell and REST API ?
    Do you have any code example ?
    Thanks

    If I fill a “body” like shown below, I got a 400 message … (Content-Lengh calculated and RequestDigest retrieved)

    $body = @{d =
    @{
    __metadata=@{ ‘type’= ‘SP.Data.EntreListItem’
    ‘etag’= “1”
    };

    Title = “Sir”;
    NomId = 1;
    Name= “Bertrand”;

    };
    }

  3. I too am trying to use the Invoke-RestSPO call to add items to a sharepoint online list with no success. I get:

    Invoke-RestSPO : Cannot process argument transformation on parameter ‘Body’. Cannot convert value “{ ‘__metadata’: { ‘type’:
    ‘SP.Data.CongregationContactsListItem’ }, ‘Title’: ‘SomeData2’}” to type “System.Byte[]”. Error: “Cannot convert value “{ ‘__metadata’: { ‘type’:
    ‘SP.Data.CongregationContactsListItem’ }, ‘Title’: ‘SomeData2’}” to type “System.Byte”. Error: “Input string was not in a correct format.””
    At C:\Users\x\Documents\x\import.ps1:185 char:8
    + -Body $Body `
    + ~~~~~
    + CategoryInfo : InvalidData: (:) [Invoke-RestSPO], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Invoke-RestSPO

    • Hi Dion, Can you please share more details on how could you add the List Item using Power Shell with REST. Awaiting you reply.

  4. Awesome post Vadim. Been trying to figure this out for a while. One suggestion on the results coming back I had to add this:
    #fix for ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys ‘Id’ and ‘ID
    #http://stackoverflow.com/questions/22080350/convertfrom-json-cannot-convert-the-json-string-because-a-dictionary-that-was
    $data = $data.ToString().Replace(‘ID’, ‘_ID’)
    Just before the Convertyfrom-Json

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