Monday 25 June 2018

Making Office 365 SharePoint REST API calls on Microsoft Flow

In this post, let us look how we can work with the SharePoint data using the REST APIs explicitly in the Microsoft Flow. We will be using this approach when no action/connector are available OOB for processing the required SharePoint data on the MS Flow. Person who have knowledge on the APIs used will prefer to use this approach.

In my previous post, we have built the connectors/actions using the REST APIs. The objective of my previous post, was to show how any connector/action can be created and published on the portal. Such actions can be shared with other users and it is readily available as OOB actions for other/end users. Connectors/Actions will be created by developers and the business users will use such connectors on their flows.

At the end, both the approaches yield the same results. Both the approaches will be used only when no connector is available for users to work with the required data. Hope the above explanation differentiates two approaches available for working with the SharePoint data.

Let us look how we can retrieve the required SharePoint data using the REST APIs available. We can use the OOB action “Send an HTTP request to SharePoint”. This action supports any type (GET, PUT, POST, PATCH or DELETE) of service calls. In the sample below, let us only look at the GET operation.

Use Case: Retrieve the use profile properties of item author, whenever an item created on SharePoint list.


Using REST API on MS Flow


1. Create Trigger: Create a flow from blank template. From the SharePoint connector, select “SharePoint - whenever an item is created” trigger. Fill in the necessary site and list details.

2. Build the REST API: Add an action. From the SharePoint connector, select the “Send an HTTP request to SharePoint” action. Fill in the necessary details like,
  • Site Address
  • Method (GET, POST, etc.) – In this case GET.
  • Uri (SharePoint REST API) - /_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='PreferredName')?@v='encodeURIComponent({Created by Claims})' – Select the “Created by claims” from dynamic content to use it as parameter for the REST API call. The parameter has to be encoded using encodeURIComponent(string) available on the expressions pane. (Refer the below snapshot for building the correct API)
  • Headers - key: accept, value: application/json, key: content-type, value: application/json
Send an HTTP Request to SharePoint action - Fill in required REST API inputs

3. Parse JSON result of previous step: Parsing the result is necessary, so that the result (JSON) properties of previous step will be available in the dynamic content pane in the further steps.

Add an action. From the Data Operations connector, select the “Parse JSON” action. In the content, select the body of previous output (output of Send an HTTP request to SharePoint action) from dynamic content.
Parse result of REST API result content
Parse result of REST API result content


In the schema, paste the REST API output sample data. This will be helpful for differentiating keys and values of result JSON data for further steps. The JSON can be taken from the previous step, by running the flow partially.
Run Flow partially and copy the body JSON for generating the schema
Run Flow partially and copy the body JSON for generating the schema

Generate the schema using the output of 'send an HTTP request to SharePoint'
Generate the schema using the output of 'send an HTTP request to SharePoint'

The below snapshot shows the schema generated automatically.
Generated schema
Generated schema


4. Test the action (Getting preferred name of user): So we have successfully used the SharePoint REST API calls on the Microsoft Flow with the help of "Send an HTTP request to SharePoint" action. Let us test the output parameters of parseJSON action by creating one more action. Say for example, there is an action to update item with the preferred name of the item owner. The below snapshot shows the properties of parsed result content (REST API result content) being used from the previous step.  

Using the result properties of SharePoint REST API
Using the result properties of SharePoint REST API

The below snapshot shows the item with the updated value using update item operation.
Item updated with user profile property retrieved using REST API
Item updated with user profile property retrieved using REST API