In my last post about workflow in SharePoint 2013, I mentioned this new data type called DynamicValue. This new data type, in combination with the new HTTP activities, makes it so much easier to work with services in SharePoint 2013 workflows. What Microsoft has given us are a few new activities that you use to acquire, create and parse these data types. Here's a high-level how it works:
You have a workflow that needs to collect the description of a category used in an item. When the workflow starts it gets the category name and calls an OData service to get the description of the category. It then takes the response and saves it to the list item.
Step 1: Create the Variable
In the new Visual Studio 2012 workflow designer, you'll find something at the bottom that sales Variable. Use that to create a new variable using the data type DynamicValue (full name: Microsoft.Activities.DynamicValue)… I'll call it CategoryDVResponse. I also create two more string variables named CategoryName and CategoryDescription. These are simply used to call the OData service and save the extracted results after parsing the returned value.
Step 2: Call the OData Service
Use an activity LookupCurrentSPListItemString to pull the category name from the list item you're using to trigger the workflow. Then, use a HttpGet activity to call the service. There are lot of properties you can set on this, but you're really only interest in two of them… set the values of these two properties to the following:
- ResponseContent: CategoryDVResponse (this is the DynamicValue variable you created earlier)
- Uri: Use the following string to concatenate a URL together to call a demo OData service filtering for a specific category name & retrieving just one field:
- “http://services.odata.org/Northwind/Northwind.svc/Categories?$format=json&$filter=substringof('" & CategoryName & “', CategoryName) eq true&$select=Description”
Step 3: Parse the Results & Write to the List Item
Last step is to parse the response from the OData service and write the result to the list item. Use a GetDynamicValueProperty<> activity to parse the results. The following picture shows what it would look like. Basically what you do is you give this activity the variable to parse, the variable to store the result in and the thing to extract (called PropertyName). You'll write this in XPATH notation since you're effectively pulling something out of a JSON result.
Step 4: Test!
Deploy the workflow and attach it to an announcement list to test it. The following picture shows I've entered a category called Beverages.
When I run the workflow notice that the body field has been updated with a new category description. Also notice that it says the workflow was updated by Workflow on behalf of a user (this is because Windows Azure Workflow is treated as an app and has been given permissions to do work on behalf of the user when you ran it).
If you want to see this in action, check out video #5 from my MSDN video from the Developer Training Workflow module. What I described above is similar to what I did in the video.comments powered by Disqus