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

Jan 3, 2015

Update Vs SystemUpdate in SharePoint

Hi Guys,

In this post i am going to explain what is the difference between Update and SystemUpdate methods. As you all know, these two methods are used to update/add the item in the sharepoint list like below. Here I am adding some more points on the same..


  1.        SPList lstProducts = web.Lists.TryGetList("Products");  
  2.        SPListItem prodItem = lstProducts.Items.Add();  
  3.        prodItem["ProdName"] = "PCMMigrator";  
  4.        prodItem["Category"] = "Sharepoint";  
  5.        prodItem.Update();
  6.       //prodItem.SystemUpdate();


Update() :

In the above code the Update() method adds the data to the SharePoint Products list. When this is happening it also internally updates the built in fields the "Modified  with the timestamp of the server when it is updating" and "Modified By with the user who logged in" . And also a new version is created for the item (if versioning enabled for the list).

SystemUpdate() :

It updates the data to the SharePoint list without changing the built in Modified and Modified By fields. It will not create any new version for the item. Basically this method does all the updates to the content database directly.

This method also can be written with boolean argument.

SystemUpdate(false) is equivalent to SystemUpdate()
SystemUpdate(true) is equivalent to SystemUpdate() but creates the new version for the item.

The SystemUpdate also triggers all  the list events as the update does.

In Server Object Model we can use these both Update and SystemUpdate methods. But the client object model will not allow the SystemUpdate method as it is changing data in content db directly without server knowing the changes made. If really we want to achieve this in csom, there are some alternative ways like writing a custom webservice and calling it in csom code.

Hope this is useful,

Thanks,
Purna