Looping Through List Items in SharePoint 2013 Designer Workflows
SharePoint 2013 Designer workflows now has two new interesting options: the ability to call HTTP web services and the option to loop over some code a number of times. This, together with the new REST API, which supports querying lists and returning data in JSON, allows iterating through list items in a workflow, something that was not possible before.
In order to demonstrate this, let’s create a new Site Workflow in SharePoint Designer, that will iterate through the Tasks list:
Call it Process Tasks, for example, and make sure you select SharePoint 2013 as the platform type.
In the workflow designer, let’s start by creating a new stage, call it Retrieve Tasks:
In it, we add a new Set Workflow Variable action which creates a new String variable called url with the value “http://sp2013/_api/web/lists/getbytitle('Tasks')/items”. This uses the new REST API, and you can pass in additional options, such as for ordering by the DueDate field in descending order:
http://sp2013/_api/web/lists/getbytitle('Tasks')/items?$orderby=DueDate desc
or filtering:
http://sp2013/_api/web/lists/getbytitle('Tasks')/items?$filter=DueDate gt DateTime’2014-07-31T00:00:00’
or paging:
http://sp2013/_api/web/lists/getbytitle('Tasks')/items?$top=20
Next, we add a Dictionary variable (Build a Dictionary action), call it requestHeaders, and initialize it as this:
Both “Accept” and “Content-Type” entries are of the String type and they both contain the value “application/json;odata=verbose”, SharePoint REST API understands this and sets the response content type appropriately as JSON. If we don’t pass these values, the output would come as XML.
Following, add an Call an HTTP Web Service action and set its properties. The request will be the url variable:
Response content will go to a new variable called responseContent:
Response headers will go to a new variable called responseHeaders:
And the same goes for the response code (variable responseCode):
Then we set the request headers to be the requestHeaders variable we created just now, by clicking on the properties for the Call an HTTP Web Service action:
Now, create a new stage, call it Process Tasks, and, at the end of the initial stage, add a Go to Process Tasks action.
On the Process Tasks stage, add a Get an Item from a Dictionary action, set the item as d/results, the source variable reponseContent and the output to a new variable of type Dictionary called list. Then count items from this list variable using a Count Items in Dictionary action and store the result in a new Integer variable called count. This variable will tell us how many times we have to loop. Finally, create a new Integer variable called index and set it to 0 (Set Workflow Variable), this will be the loop index.
Next, add a loop (Loop n Times), call it Loop Task Items, and set the loop variable to count. Inside the loop, get value d/results([%Variable: index%]) using a Get an Item from a Dictionary action from responseContent and store it in a new Dictionary variable called item. Get some fields (Get an Item from a Dictionary) from the item variable, such as Title and DueDate (mind you, these will be task item fields) and store them in appropriate variables and do whatever you want with them, like logging its contents (Log). Time to increment the loop counter: add a Do Calculation action and in it increment the index variable into a new Integer variable called indexPlusOne. Then set the index variable to be the indexPlusOne (Set Workflow Variable). Finally, exit the loop and set the workflow status (Set Workflow Status action) to Finished. At the end of the stage, select Go to End of Workflow.
That’s it. Your workflow should look like this:
The new functionality makes SharePoint Designer workflows much more powerful than before. I will continue to talk about it in the following posts.