SharePoint 2013 brings a new workflow engine with several enhancements such as support to REST calls, elastic services and more. The complete list of what is new in SharePoint 2013 workflows can be found here.
One of the most celebrated new feature by the developers is the possibilities to make REST call directly from the workflows. With that, the workflows can be easily integrated with other system and even with other SharePoint sites. Just a simple search on the internet and you can find several examples illustrating how to call external JSON web services and how to read and write SharePoint lists items using the SharePoint REST API as well. However, update an existing item in a SharePoint lists has some tricks which we will show you in this post.
The approach will be follow the Microsoft operation guide using SharePoint 2013 REST endpoints (link here) and convert it to run on our workflow. Assuming that you know which list you want to update and you already started to create the workflow, here are the steps:
1. Build a dictionary that will contain the headers for the REST call. We willl call this dictionary POST_RequestHeader and add the attributes according to the picture below
According to SharePoint 2013 REST guide, when you are trying to update an item you do need to use the term X-HTTP-Method in the REST header. Also the header IF-MATCH has to be presented, but whenever you don’t have to worry about concurrency, the value (*) should be uses. Otherwise you should obtain the etag value of a list item by performing a GET request before update the item.
2. Create another dictionary called POST_Metadata where we will store the type of the item
You should find the correct data type for the item you are trying to update. One way to find it is creating an item into the list you are trying to edit and query it using the REST API. The URL would look like something like that:
If you are using the browser to open this URL you will see a bunch of XML. Just look for the property Category term=”SP.Data.<yourdatatype>” and you will have the data type. For example, if I am trying to update an item in a list called “Tasks”, my data type will be SP.Data.TasksListItem. The data type name might change so don’t assume it is always your list name.
Previously we talked about the etag value that should be used when you might have concurrency scenario. You can see this value in the m:etag property
TIP: In case you don’t like reading XML you can use some tools to visualize the REST response in a better way. Currently I’ve been using a Google chrome extension called “Advanced REST client” where you can enter your URL and the tool let you specify also some headers for the query. For instance, if you specify the key Accept: application/json;odata=verbose to the REST header you will get the query result in a readable JSON format.
3. Create another dictionary called POST_Parameters and add the POST_Metadata as parameter as well as the list fields you want to update
__metadata: <POST_Metadata dictionary>
Title: My new updated title (Example of a list field that I am updating)
4. Configure the web service call. When you are configuring the parameters, make sure you are using the properties windows from the “Call HTTP web service” action
RequestType: HTTP POST
ResponseStatusCode: POST_ResponseCode (just a string variable)
Than you should be all set to try it. Don’t forget that this is an UPDATE routine, so the item should exist in a list. If the web service call successfully updates the item, your response code will be the string “NoConten”, otherwise you will have the error message.
Never miss an article by subscribing to our monthly digest!