Welcome to Open Future Associates

Training

Associates

Sharepoint

Academy

Consultancy

Profiling Tools

REST in a SharePoint 2013 Workflow


Introduction

These series of posts are to support what Laura GB presented at SharePoint Saturday UK 2014 in more detail than was possible at the event. It assumes some knowledge of workflows in the SharePoint Designer 2013.

The Aim

Workflows are written to map many processes in an organisation. Some of these processes do not just require access to a single item in SharePoint but require accessing a list of items. One example could be a KPI score that is automatically created based on how many tasks are late in a project plan, this involves counting items in a list. Another example could be creating tasks for a HR department based around an event, e.g. every time a new member of staff joins the company a list of tasks are created based upon a template list.

This post is the first part in the series.

Part 1 – REST URLs

Part 2 – Put the Data in a Dictionary

Part 3 – Loop through the items in the dictionary

Part 4 – Filter and Sort the data

Part 1 – REST Urls

SharePoint presents a list of data in a very clear, readable way that is pleasing to the eye. This is not what is required to process that data, for this the data needs to be in a more raw format, this can be accessed via a REST workflow. For a workflow, ie our next stage, it needs to be in JSON (JavaScript Object Notation) format. For a description and explanation of JSON head to http://www.w3schools.com/json/ XML Data

The data behind the list can be displayed in a browser window by using the following path formula

[Site URL] /api/web/lists/getbytitle(‘[List Name]’)/items

For example the URL below fetches all the items in the list called Tasks on lgb.sharepoint.com. Please be aware that it is the display name of the list or library not the URL name.

lgb.sharepoint.com/api/web/lists/getbytitle(‘Tasks’)/items

This will result in displaying the list in an XML format. This needs to be converted to a JSON format. In order to displayed the data in a JSON format a add-on is required to add a header to your request to the server. For this post I have used ModHeader in Chrome. There are alternatives.I downloaded Mod Header from the Chrome store. Once installed it adds a small icon next to the formula bar. Clicking on this icon a form is displayed to add header details. To convert from XML to JSON you need 2 lines added

Accept application/json;odata=verbose
Content-Type application/json;odata=verbose

With the header lines entered and ticked, refresh the url. The data displayed will now be in JSON format.

spworkflow002workflow3

JSON Viewer

Although the URL and header have extracted the data, it is hard to visual the structure of the data. One method is to use a JSON online viewer. Many can be found with a quick search on the web. The example shown is from http://jsonviewer.stack.hu/ JSON Viewer

So you now have your data in a format that a SharePoint 2013 workflow can handle and you have the details for the header to make sure the data is formatted correctly. Ready to move onto to Part 2