Recently, I was asked to come up with a solution for retrieving the value of a Managed Metadata column for items in a SharePoint list. This solution was needed as part of a SharePoint Designer 2013 Workflow and, initially, I thought that it would be fairly simple to do by building a Custom Workflow Activity in Visual Studio 2013. As is often the case, it was more difficult than I expected and, therefore, I thought I would share my approach.
The challenge is that if we take the logical approach and use LookupListItem to query the list item in our custom action, the values that are returned for a Managed Metadata column in the JSON payload look something like this:
{\"__metadata\":{\"type\":\"SP.Taxonomy.TaxonomyFieldValue\"},\"Label\":\"1\",\"TermGuid\":\"c8111e66-f7b3-410e-a699-4e0b15e5de08\",\"WssId\":1}
As you can see, there are three parts to this value:
Label:"1" TermGuid:"c8111e66-f7b3-410e-a699-4e0b15e5de08" WssId:"1"
Unfortunately, none of these contain the actual text value we need for the selected term, which in this case is “Accounting”.
Fortunately, the problem can be solved quite simply using the httpSend activity.
The first step is to create the necessary variables:
and arguments:
The next step is to add WebUri and GetCurrentlListId activities to populate the currentSiteUrl and currentListId variables, respectively.
We will be accessing the SharePoint 2013 REST endpoints in our httpSend request, so the actual Uri we will be using will need to look like this:
{Site Url}/_api/Web/Lists(guid'{List Guid}')/Items({Item Id})/{Field Name}
Consequently, we now need to add an Assign activity and set the httpUri variable to the following value:
String.Format("{0}/_api/Web/Lists(guid'{1}')/items({2})/{3}/Label", currentSiteUrl, currentListId, currentItemId.ToString(), mmFieldName)
Now, we add the httpSend activity and set the properties as follows:
If all goes as planned, the value of the serviceResponse should look something like this:
{"d":{"Label":"Accounting"}}
The final step is to parse out the value of the Label property, which is done by adding a GetDynamicValueProperty<T> activity and setting the Type to “String”.
The magic is done by setting the PropertyName value to “d/Label”, which extracts the value of the Label property as a string.
When the new custom activity is added to a SharePoint Designer Workflow, a String variable can be assigned to the mmFieldValue parameter, which will contain the value of the Managed Metadata column.