Calling the SharePoint 2013 Rest API from a SharePoint Designer Workflow

At the moment you will find many examples illustrating how to call external JSon web services from  SharePoint Designer 2013 workflows by using the Call activity, but we should keep in mind that the Call action works very well with the SharePoint REST api and this opens the door to plenty of possibilities. However you won’t find many examples illustrating this. Sooo let’s go  ; –)

  • Create an announcements list and let’s say we want to create a workflow that returns the first one by using the REST api.
  • Add a few announcement
  • Start SharePoint Designer 2013 and add a list workflow on this announcement list; make sure the workflow is a SharePoint 2013 workflow

image

  • Test your REST call in the browser, for instance mine is the following :

http://litware/_api/web/Lists/GetbyTitle(‘announcements’)/items

You should get something like this:

 

image

You will notice that the response is encoded in xml format; since the $json option  is not supported in the SharePoint 2013 REST api, to get the the response in a JSON format you will have to update the Accept and Content-Type http headers of your Rest http call; more specifically :

Accept : application/json;odata=verbose

Content-Type : application/json;odata=verbose

Even if my favorite browser is IE10, I will user Google Chrome to achieve it; start Chrome and install the following Chrome add-in :

 

image

When the add-in is installed, you should be able to find the following icon that will allow you to set up your headers

image

After typing your request request in Chrome you should get the info encoded in JSON

image

To have a better understanding of structure of the Json response , I usually paste the results in the following JSon parser available online : http://Json.parser.online.fr

 

image

d/results(0)/Title should be the path of the first announcement title.

Now, let’s get back to our workflow in SharePoint Designer 2013 :

we need to create the Request header, and this is possible by using a Dictionnary :

  • In the existing Stage type Build

image

  • rename the variable dictionnary as JSonRequestHeader :

image

  • Click on this and click on Build your dictionnary ; add the Accept and the Content-Type headers :

image

  • Now let’s focus on the Http request by adding a Call action

image

  • Click on this and paste your http request

image

 

  • To associate the JSonRequestHeader variable, select the Call action property:

image

  • set the RequestHeaders property to JSonRequestHeader:

image

Click ok.

  • In the  Call action, click on response and associate the response to a new variable : JSonResult (of type Dictionnary).
  • After the Call action add a new Get action.

image

  • Add a log activity and display the myTitle variable in the log :

image

  • The final Stage should look like this :

image

Publish and run the workflow; in the workflow history, you should find your first title :

image

About these ads

2 responses to “Calling the SharePoint 2013 Rest API from a SharePoint Designer Workflow

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