Quantcast
Channel: Content Master » SharePoint 2013
Viewing all articles
Browse latest Browse all 3

Custom Workflow Activity for Setting Managed Metadata Field Values

$
0
0

In this post I’ll show you how to build a custom workflow activity in Visual Studio that can update managed metadata field values in a SharePoint 2013 list or library. This is the final part of a three-part series on working with managed metadata fields in workflows:

  • Getting and Setting Managed Metadata Fields in SharePoint 2013 Workflows. In this post, I introduce the scenario, explain why you can’t use built-in list actions to work with managed metadata fields, and provide a conceptual overview of the custom workflow activities approach.
  • Custom Workflow Activity for Getting Managed Metadata Field Values. In this post, I’ll walk you through how to build a custom workflow activity in Visual Studio that gets managed metadata field values from a list item.
  • Custom Workflow Activity for Setting Managed Metadata Field Values (this post). In this post, I’ll walk you through how to build a workflow activity that sets managed metadata field values on a list item.

I’ve said it before, but it’s worth repeating – you can use these custom workflow activities in any SharePoint Designer list workflows, including on Office 365 sites – custom workflow activities in SharePoint 2013 are entirely declarative, so deploying to SharePoint Online is not a problem.

This series of posts tackles the scenario where you want to extract a managed metadata field value from an item in one list (previous post), and then apply that value to a managed metadata field in another list (this post). The main constraint is that the managed metadata fields in the source list and the destination list must both use the same term set.

Arguments and Variables

If you’ve been following the series of posts you’ll be familiar with the scenario and the concepts, so let’s assume we’ve created a brand new custom workflow activity named Set MMS Field Value in Visual Studio and jump straight into defining arguments and variables. First the arguments:
I want to be able to use this activity update a managed metadata field on any SharePoint list or library, so the first piece of information we need is an identifier for the target list or library (selectedList). Next, we need to know which list item to update. List items are commonly identified using either a GUID identifier (listItemGuid) or an integer identifier (listItemIdIn) – I’ve defined arguments for both so the workflow designer can use either approach to identify the target list item. Next, we need to know the name of the managed metadata field in the target list item (mmsFieldName). Finally, we need the two property values that uniquely identify our managed metadata term (termGuid and termLabelInteger).Now the variables:
We’ll use listItemId to store the integer identifier for the target list item. The emptyGuid variable is just an empty GUID that we’ll use for comparison purposes, and the remaining variables (metadataDV, propertiesDV and fieldValueDV) are DynamicValue properties that we’ll use to progressively build the correct JSON structure to update a managed metadata field.

Activity Design

The workflow activity consists of seven child activities that correspond to three main tasks:
  1. Get the integer identifier of the target list item. (If the workflow designer has provided an integer identifier, use it directly. Alternatively, if the workflow designer has provided a GUID identifier, use the GUID to look up the integer identifier.)
  2. Build up the JSON payload we must provide in order to update the specified managed metadata field.
  3. Update the specified managed metadata field on the specified list item.
Let’s take a closer look at these three high-level tasks.
Task 1 – Get an integer identifier for the target list item
Our first task is to get an integer identifier for the target list item. Remember that we’re giving the workflow designer two options: he or she can provide either a GUID identifier or an integer identifier to specify the target list item. To cater for both scenarios, we use an If activity. If the list item GUID is equal to an empty GUID, we can assume the workflow designer has used an integer identifier to specify the target list item. In this case, we use an Assign activity to set the the listItemId variable to the value of the listItemIdIn argument. If not, we use a LookupSPListItemId activity to look up the integer identifier using the specified GUID and then set the listItemId variable accordingly.
Task 2 – Build a JSON payload for the target managed metadata field

Our next task is to build a JSON payload for the target managed metadata field. The payload must take the following format, where <Field name> is the name of the target managed metadata field, <Term label integer> is the term label integer of the managed metadata term, and <Term GUID> is the term GUID of the managed metadata term:
{“<Field name>”:{
   “__metadata”:{“type”:”SP.Taxonomy.TaxonomyFieldValue”},
   “Label”:”<Term label integer>”,
   “TermGuid”:”<Term GUID>”,
   “WssId”:-1 }}
To take advantage of the built-in child activities in Visual Studio, we need to create our JSON payload using DynamicValue structures. Because of the nested nature of this payload, we need to build the structure progressively from the inside out. First we use a BuildDynamicValue activity to build the contents of the innermost braces (the value of the __metadata property):
Next, we use a BuildDynamicValue activity to build the value of the middle set of braces (the value of the <Field name> property):
Notice how we set the __metadata key to the metadataDV value we created in the previous step, thereby creating a nested DynamicValue instance.
Finally, we use a CreateDynamicValue activity to build the value of the outer set of braces:
Note: We use a CreateDynamicValue activity rather than a BuildDynamicValue activity in this task because it allows us to set the dictionary key (PropertyName) to a variable value (mmsFieldName in this case). The BuildDynamicValue activity only allows you to type static string text for the dictionary key (Path). That wouldn’t work in this scenario as we don’t know the name of the target managed metadata field at compile time.
Task 3 – Update the target list item
Now that we’ve identified our target list item and build our JSON payload, all that remains is to perform the update operation on the list item. We can use the built-in UpdateListItem activity to do this:

The Actions File

The next stage is to build the actions (.actions4) file for the workflow activity, to specify how our activity should behave when we add it in SharePoint Designer. My actions file looks like this:
I won’t go into more detail on the structure of the actions file right now, as there’s nothing out of the ordinary in it and I don’t want to get too repetitive. When you deploy the activity and use it in SharePoint Designer, it looks like this:
In this case, I’m using my Get MMS Field Value activity to get the term GUID and the term label integer from a managed metadata field named Customer in the current list item. I’m then using the Set MMS Field Value activity to set the value of the Client field on a list item in the Clients list to the same term. Because the source Customer field and the destination Client field both use the same term set, the workflow is able to copy the value across as desired.

 

This was originally posted on Jason Lee’s blog - http://www.jrjlee.com/2015/01/custom-workflow-activity-for-setting.html

 

Share This

The post Custom Workflow Activity for Setting Managed Metadata Field Values appeared first on Content Master.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images