Consume SharePoint Online REST service using .NET


Since the introduction of REST interface in SharePoint 2010 you have probably used WebClient or HttpWebRequest in .NET applications.
HttpClient is a modern HTTP client for .NET, it provides a flexible and extensible API for accessing resources via HTTP(S).
HttpClient offers some advantages over WebClient/HttpWebRequest such as:

  1. An HttpClient instance is the place to configure extensions, set default headers, cancel outstanding requests and more.
  2. You can issue as many requests as you like through a single HttpClient instance.
  3. HttpClients are not tied to particular HTTP server or host; you can submit any HTTP request using the same HttpClient instance.
  4. You can derive from HttpClient to create specialized clients for particular sites or patterns
  5. HttpClient uses the new Task-oriented pattern for handling asynchronous requests making it dramatically easier to manage and coordinate multiple outstanding requests.

HttpClient for SharePoint Online

The solution consists of:

  1. SPHttpClient class that inherits from HttpClient and provides some additional SharePoint specific functionaly such as getting request digest
  2. SPHttpClientHandler class that basically hides all the intricacies related to SharePoint Online authentication

SharePoint Online client implementation

SharePoint Online HTTP handler implementation

Working with list items with REST

The following example shows how to retrieve all of a list’s items:

The following example shows how to retrieve a specific list item:

The following example shows how to create a list item:

The following example shows how to update a list item:

The following example shows how to delete a list item:

Consuming the SharePoint Online REST API from PowerShell: Part 2


A while back ago we already discussed how to  consume SharePoint Online (SPO) REST in PowerShell. Here is a brief recap:

This time I would like demonstrate another approach, in particular  how PowerShell can gain authorization to SharePoint resources by passing an access token to SharePoint with each HTTP request. To issue an access token from Microsoft Azure Access Control Service (ACS) that allows the app access to the resources in the SharePoint tenancy we will implement the corresponding PowerShell function. Let’s get started.

Getting Access Token from Microsoft Azure Access Control Service

The Get-SPOAccessToken function demonstrates how to obtain the access token from a Microsoft Azure Access Control Service (ACS) account that is associated with the customer’s Microsoft Office 365 tenancy:

Get-SPOAccessToken function is intended for requesting an access token from Azure ACS, it accepts  Client Id and Client Secret parameters that are generated while App registration with Azure ACS (see “How to register App” for a more details).

Using Invoke-RestMethod in Office 365

Invoke-SPORestMethod function demonstrates how to  include the access token to make a REST API call  to SharePoint, passing the OAuth access token in the HTTP Authorization header:


The following example demonstrates how to retrieve List resource properties:

But before running the specified script we need to perform one more step in order to grant permissions to the app principal otherwise the unauthorized error will occur as shown on picture below:

  • Navigate to http://<SharePointWebsite>/_layouts/15/AppInv.aspx
  • Look up the app based on the Client ID that you just generated and click Lookup, it will find the app principal.  Then paste the AppPermissionRequests XML into the Permissions text box and click CreateAppInv
    Once you click Create, the Trust dialog will appear, click Trust

That’s it.  Now, after executing the specified script, the output will look like shown below


How to register App

Below is provided a step by step instruction how to register an App, for a complete guide follow this article:

  • To create the app identity, navigate to http://<SharePointWebsite>/_layouts/15/AppRegNew.aspx on the tenancy or farm
  • Enter values for the form fields as shown below on picture
    App ID: App ID, also known as client ID, is a GUID that can be generated (when you click Generate) or pasted into AppRegNew.aspx. The value must be unique for each app, and must be lower case
    App Secret: The app secret, also known as the client secret, is an opaque string. It is generated on the AppRegNew.aspx page by using the Generate button. The following is an example of an app secret: Ywjaoz7DJBGhoLQ2t0IbVCA5pfqqI722ZIVt+ENLk0g=
    Title: Choose your own user-friendly title; for example, PowerShell Console
    App Domain:
    The host name of the remote component of the app for SharePoint
    Redirect URI: The endpoint in your remote application or service to which ACS sends an authentication code
  • Click Create on the form. The page will reload and show a confirmation of the values you entered as shown on picture below
  • Save Client Id and Client Secret values. After that you could verify whether Get-SPOAccessToken function returns access token. The picture below shows  the output after executing the command:
    Get-SPOAccessToken -ClientId “1523cf05-b437-4e73-9ad1-a652da8f2ae5” -ClientSecret “Ywjaoz7DJBGhoLQ2t0IbVCA5pfqqI722ZIVt+ENLk0g=” -WebUri “;


Enterprise Keywords management in Office 365 via CSOM


SharePoint 2013 has introduced Microsoft.SharePoint.Client.Taxonomy namespace for metadata management via CSOM. Since Taxonomy API also allows to manage Keywords, i would like to demonstrate how to get/set Enterprise Keywords field values in this post.

Adding Enterprise Keywords column into List

The first example demonstrates how to add Enterprise Keywords column into List:

Setting  Enterprise Keywords field value

The below example demonstrates how to set Enterprise Keywords field value:

Key points:

  • The operation of setting Enterprise Keywords field value consists of two steps, first one to resolve Keyword in Managed Metadata service application (MMS), this what  EnsureKeyword method is intended for.  After Keyword has been retrieved or created if it not existed, the value of Enterprise Keyword field is set (TaxonomyFieldValueCollection type).

DocumentsWithTaxKeyword TermStore_Keywords

An alternative way of getting Client Object properties using SharePoint REST and CSOM APIs

When working with client APIs such as JSOM or REST you have probably noticed that certain properties of objects are not available compared to SSOM counterparts. For example, SPList class exposes SPList.Author property for getting an SPUser object that represents information about the user who created the list which in turn is not available for SP.List object. Hence the question arises, how those properties could be retrieved using client APIs?

The solution that I would like to demonstrate is based on retrieving client object properties from XML schema.

Getting SP.List object properties using JSOM

The function getListProperties is indented for loading list schema of the SP.List using SP.List.schemaXml property and extracting properties from it’s value:

The following example demonstrates how to retrieve Author property  using the specified method:


Getting SP.List object properties using REST

The following REST endpoint is used for retrieving SP.List.schemaXml property:



The same example that demonstrates how to retrieve Author property of List resource using REST:

Manage User Custom Actions in Office 365


Custom Actions offer a flexible way to extend capabilities of the SharePoint. The possibilities span the range of including custom JavaScript on every page to extending the Ribbon. In SharePoint 2013/SharePoint Online you can leverage the CSOM/REST  to manage custom actions. Below are demonstrated two simple examples of using custom actions in real world scenarios and I hope you you’ll find them useful.

Example 1. Enable jQuery

Let’s get started with an example that demonstrate how to add jQuery library to Office 365/SharePoint Online site. Unfortunately  it is not supported to reference external resources, for example from Microsoft Ajax Content Delivery Network (CDN) that hosts popular third party JavaScript libraries including jQuery. The prerequisite for referencing JavaScript files is that they could only be accesible when located within the site collection. So, the first step would be to save a jQuery library into Style Library: /Style Library/Scripts/jQuery/jquery-2.1.1.js.

The following Activate-JQuery.ps1 script  demonstrates how to enable jQuery library  in Office 365/SharePoint Online site

Dependencies: UserCustomActions.ps1

Example 2. Enable Google Analytics

The following Activate-GoogleAnalytics.ps1 script demonstrates how to activate tracking code in Office 365/SharePoint Online site

Dependencies: UserCustomActions.ps1

Follow these instructions to use Google Analytics to collect data from Office 365/SharePoint Online sites.

To set up the web tracking code:

  1. Find the tracking code snippet for your property.
    Sign in to your Google Analytics account, and click Admin in the top menu bar. From the Account and Propertycolumns, select the property you’re working with. Click Tracking Info / Tracking Code.
  2. Find your tracking code snippet. It’s in a box with several lines of JavaScript in it. Everything in this box is your tracking code snippet. It starts with <script> and ends with </script>.
    The tracking code contains a unique ID that corresponds to each Google Analytics property. Don’t mix up tracking code snippets from different properties, and don’t reuse the same tracking code snippet on multiple domains. Click to expand this image and see where the tracking code snippet is in the interface.
  3. Open , paste the tracking code into $TrackingCode variable and run the script to register tracking code in Office 365/SharePoint Online site
  4. Check your set up.
    Make sure that the tracking snippet installed on your website matches the code shown in the view, and see more ways you can verify your set up.

Working with folders and files via SharePoint 2013 REST in PowerShell


In the previous post we’ve already discussed how to perform CRUD operations by sending HTTPS requests to SharePoint RESTful web services in PoweShell. The Invoke-RestSPO function was introduced for that purpose since  Invoke-RestMethod cmdlet does not support claims based authentication and it makes this cmdlet impossible to use in O365 and SharePoint Online scenarios.

This time I am going to demonstrate how  to perform basic create, read, update, and delete (CRUD) operations on folders and files with the SharePoint 2013 REST interface using Invoke-RestSPO function.

Explore the REST service files and folder syntax

SharePoint 20123 Files and Folders REST syntax


Working with folders

Folder resource: represents a folder on a SharePoint Web site

Endpoint URI: http://<site url>/_api/web/getfolderbyserverrelativeurl(‘/<folder name>‘)

Supported HTTP methods:  GET  |  POST  |  DELETE  |  MERGE  |  PUT

The following examples demonstrates how to perform basic CRUD operations with Folder resource.



Working with files

Folder resource: represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.

Endpoint URI: http://<site url>/_api/web/getfilebyserverrelativeurl(‘/<folder name>/<file name>‘)

Supported HTTP methods:  GET  |  DELETE  |  POST  (File resource)

The following examples demonstrates how to perform basic operations with File resource including:

  • upload file into SharePoint
  • download  file from a SharePoint



To summarize, it was demonstrates how to perform basic operations with files and folders, in particular how to  download and upload files via REST. For that purpose we  utilized Invoke-RestSPO function  that is intended for sending HTTPS requests to O365/SharePoint Online REST service.


How to: Upload files into Office 365 via PowerShell


Being one of the most common questions “How to upload files to SharePoint Library?”, i noticed that usually folder structure is not taken into account in the provided solutions.
So i decided to fill the gap and implement another version that allows to preserve folder structure while uploading files.


PowerShell script

PowerShell script demonstrates how to upload  files within a specified local directory to a Document Library within a Site in an O365 tenant.


SharePoint Online client for PHP

In this post I would like to introduce you to a SharePoint client for PHP available on GitHub. The library provides a SharePoint Online (SPO) client for PHP applications. It allows you to performs CRUD operations on SharePoint data using an SharePoint 2013 REST/OData based API.


PHP:cURL library is used to make HTTP requests to performs CRUD operations on SharePoint data using SharePoint 2013 REST API.

Getting started

A first example

Key points:

  • SPOClient class represents a REST Service client for the specified SharePoint Online (SPO) site
  • The signIn method performs a claims-based authentication. You can find more details about remote authentication in Remote authentication in SharePoint Online post.

The following examples demonstrates how to perform the remaining CRUD operations on SharePoint list data.

 List data generator

And finally one more example that demonstrates how to populate Contacts list in SharePoint. For generating fake data we will utilize Faker PHP library, below is provided the complete example:


The library currently supports CRUD operations against SharePoint list data. But since SharePoint 2013 REST covers a much broader set of API, the library could be extended.

Hopefully this helps you get started how you can interact with a SharePoint Online site from a remote Web application written in PHP!

Consuming the SharePoint 2013 REST Service from SharePoint Designer

In SharePoint 2013 workflows  was introduced  a new action named Call HTTP Web Service. This action is flexible enough  to make requests to a variety web services including SharePoint REST service.

Figure 1 shows you the Call HTTP Web Service action on the SharePoint Designer 2013 surface.

Call HTTP Web Service Get Operation


Figure 1. SPD Workflow that demonstrates how to leverage Call HTTP Web Service action for Read operation

The specified example demonstrates how to perform Read operation for retrieving webs information:

  1. Create a workflow variable named Url and set it’s value to [%Workflow Context:Current Site URL%]_api/web/webinfos
  2. Insert Build Dictionary action to construct request headers. Add key value pairs  from table 1. Save results into a variable of type Dictionary and named  RequestHeaders
  3. Insert Call HTTP Web Service action. Open Properties window and specify it’s properties as shown in table 2.


Table 1. Request Headers for a RequestHeaders dictionary variable

Name Type Value
 Accept String  application/json;odata=verbose
 Content-Type String  application/json;odata=verbose


Table 2. Call HTTP Web Service action properties for Read operation

Property Name Value
 Address Set value to variable Url that was created in step 1
 RequestType Set value to HTTP GET
 RequestHeaders Set value to dictionary variable RequestHeaders that we’ve constructed to store request header properties in step 2 
 ResponseContent Create a new dictionary variable named Result for storing the response returned by the REST service and set value to variable Result

Scenario: Create Site Workflow

The below example demonstrates how to  perform Create operation using Call HTTP Web Service action, in particular how to create web site from a workflow. 

Note: In order to perform Create/Update/Delete operations using  Call HTTP Web Service action, App permissions for workflow have to be configured.  Please follow the steps in article Create a workflow with elevated permissions by using the SharePoint 2013 Workflow platform to configure App permissions for workflow.


Figure 2 shows you the configured Call HTTP Web Service action for creating web site

Call HTTP Web Service Create Operation

SPD Workflow

Figure 2. SharePoint Designer workflow for creating web site


1.   Create a workflow variable named Url and set it’s value to [%Workflow Context:Current Site URL%]_api/web/webinfos/add

2,3 and 5.   Those steps serve for building payload to submit to the service.  RequestContent parameter of  Call HTTP Web Service action is used for setting payloads to submit to the service.  See tables 4,5 and 6 for a details.

4.   Insert Build Dictionary action to construct request headers. Add key value pairs  from table 1. Save results into a variable of type Dictionary and named  RequestHeaders

6.   Insert Call HTTP Web Service action. Open Properties window and specify it’s properties as shown in table 3.

Table 3. Call HTTP Web Service action properties for Create operation

Property Name Value
 Address Set value to variable Url that was created in step 1
 RequestType Set value to HTTP POST
 RequestHeaders Set value to dictionary variable RequestHeaders that we’ve constructed to store request header properties in step 4 
 RequestContent Set value to dictionary variable RequestContent that was created for storing payload in step 5
 ResponseContent Create a new dictionary variable named Result for storing the response returned by the REST service and set value to variable Result

 Specifying  web site  creation information

For building payload to be submitted to the service the following dictionary variables are initialized in the workflow

Table 4. Metadata dictionary variable

Name Type Value
 type String  SP.WebInfoCreationInformation


Table 5. WebParameters dictionary variable

Name Type Value
 __metadata Dictionary  Variable:Metadata
 Url  String  Parameter:WebTitle
 Title  String  Parameter:WebTitle
 Description  String  Parameter:WebDesc
 Language  Integer  Parameter:Language
 WebTemplate  String  sts
 UseIniquePermissions  Boolean  false


Table 6. RequestContent dictionary variable

Name Type Value
 parameters Dictionary  Variable:WebParameters

For passing information about web site, an Initiation form is used as shown on figure 3


Figure 3. Initiation Form





Consuming the SharePoint 2013 REST API from PowerShell


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.


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.