Jan 29, 2015

Calling SharePoint2013 REST API in SharePoint 2013 Designer Workflow using CallHttpWebService action

Hi ,

In this post iam going to explain you how a webservice or SharePoint Rest service can be called using a newly introduced action with SharePoint designer 2013.

Scenario :
In this example, I am associating the workflow to a list called Customers.

Customers List Schema : 


The objective of this workflow is, when a new item is created in to  this list, the workflow will be triggered and displays its CustomerAddress' in the log. Here the CustomerAddress fields value will be queried using Rest Service of Customers list.

Before starting the workflow, have a look on the Rest API of 'Customers' list in your browser , we are going to call this service in our workflow to get the CustomerAddress fields value.

http://yoursiteurl/_api/web/lists/GetbyTitle('Customers')/items

You should get something like this




If you have any issue in viewing please follow my previous Post.

Now go and add a workflow in SharePoint designer for Customers List. Make sure that you selected the Sharepoint 2013 worklfow  Platform like below




Lets start building the workflow  logic.

Add a action called Build this Dictionary (Just type 'build' int he stage and select the action)





Rename variable dictionary to requestheader (Click on Local variabels tab in the ribbon and modify the name to 'requestheader', keep the type as 'Dictonary' only)



Click on 'this'  and add 'Accept' and 'Content-Type' headers like this

Name : Accept , Type : String , Value: application/json;odata=verbose



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




The reason to add these headers is , basically the response of 2013 api is encoded in xml format, to get the response in JSON format we will be adding these headers.


Now add the Request Action(type call and select 'CallHttpWebService



Click on 'this'  and give the Customers REST Url as mentioned above




Now add the request header to CallHttpWebService Action like below




Now set the request header and. 

Set  RequestHeaders attribute with the variable requestHeader.
And also create a new variable called 'ResultDictionary' for the ResponseContent  attribute like below.




Now our result(response of Rest API) is in 'ResultDictionry' variable.We would be parsing this dictionaryvariable using a action to get the required outcome.

Add action called "Get item by name or path from dictionary.." like below




Now we need to get the CustomerAddress column from this response dictionary, here is the way of parsing the dictionary

Click  on 'item by name or path' and write d/results(0)/CustomerAddress 





Now select 'ResultDictionary' at dictionary  and set the output to a varibale called 'varCustomerAddr'.(click on Output to item in the above action and create a new varible called varCustomerAddr type string) . 

Your action should look like this



Now Log the varibale varCustomerAddr




Add  Go to EndWorklof action . Now our entire worklfow should  look like this




Now Go to Workflow Settings (in ribbon) and set start options like this to start automatically.






Save the Workflow and Say Publish.

Now go and add a item in customers list and check the log is displaying the CustomerAddress




Here we are printing the first item's CustomerAddress in the log by using this parsing logic d/results(0)/CustomerAddress.


Hope this is helpful,

Thanks
Purna

1 comment: