Update Managed Metadata Field Using SharePoint Designer 2013 Workflow

In this article, I am going to explain how we can update a managed metadata field using SharePoint 2013 Designer Workflow. This solution will work for both, SharePoint 2013 On-Premise and SharePoint Online (Office 365). I tested the below solution in Office 365 environment.

Let’s get started.

Requirement

There is a document library with a single content type created called Client Document. This content type has the following fields.

  • Name
  • Client (Managed Metadata)
  • Client Status (Choice)
  • Advisor (People Picker)
  • Document Type (Choice)
  • Document Date (Date/Time)

In the document library, there are approximately 200 clients (folders), each having its own top-level folder; e.g.

  • Document Library

    • Client 1 (Folder)
    • Client 2 (Folder)
    • Client 3 (Folder)

Within each client (folder), there is a set of sub folders created, such as - Correspondence, Plans, Review, and Advice. Each folder will contain documents. The requirement is that the Client field (managed metadata) in content type to be set for that folder and all sub items to this particular term name,  that is available in the SharePoint Term Store Management.

In other words, the requirement is to set the managed metadata field with the name of the root/parent folder in all sub-folders files and this name needs to be taken from Term Store Managed Metadata.

Issue

When you try to update a managed metadata field through the “Update List Item” action a Bad Request error occurs.

Solution

We know that whenever we add a taxonomy field in SharePoint, two filed are added internally,

  • The first field is the visible field which we can edit
  • Hidden field which holds the value of its visible filed

Let’s say you add a taxonomy field called “Client”, SharePoint will automatically add a note field called “Client_0”.

Therefore, the solution is to update the hidden filed with a well-formatted text value, so that its associated taxonomy field also gets updated.

So, all we need to do now is to get the internal name of our hidden field and set its value with a REST call using the format (for single value managed metadata field),

Term1Name|Term1Guid

Below is the View of Document Library, showing the folder (Client 1) and managed metadata column name.

SharePoint

Below is the image showing site columns created in a content type at site collection level,

SharePoint

Below is the image showing the managed metadata terms in Term Store Management,

SharePoint

NOTE

The term name is same as the folder name created in the document library.

Below are the steps to update the managed metadata filed using SharePoint Designer.

Step 1 – Get the Hidden field name

Use the below REST API call to get the hidden field internal name

https://site/_api/web/lists/getbytitle('listtitle')/fields

Use the “Static Name” of the filed as the internal name. (It will be in the form of a GUID)

Step 2 – Get the Term Name and Term Guid

In the Term Store Management Tool for your site (Site Settings > Site Administration > Term Store Management), find the term you want to use to update the taxonomy field and get its name and GUID.

SharePoint

Step 3 – Get the List Item Type

Use REST API to get the list item type.

https://site/_api/web/lists/getbytitle('listtitle')

Then, get the value for the “d:ListItemEntityTypeFullName” element.

In my case, it was - SP.Data.ClientDocsItem

Step 4 – Create Headers Dictionary in SharePoint Designer

Create a Dictionary in SharePoint Designer 2013 Workflow with the following values.

Accept: application/json;odata=verbose
Content-Type: application/json;odata=verbose
Content-Length: <length of post body>
X-HTTP-Method: MERGE
If-Match: *

SharePoint

Step 5 – Create Metadata Dictionary

Create another Dictionary to store the metadata. This dictionary will only contain one entry for the list item type.

type: SP.Data.ClientDocsItem

SharePoint  

Step 6 – Create Data Dictionary

We need one more dictionary to create the request data.

There will be two entries in this dictionary:-

  • key “__metadata” (note the double underscore before the “metadata” string) with its value set to the previously created metadata dictionary.
  • key set to the internal name of our hidden field and its value having the format “TermName|TermGuid”. 
__metadata: <metadata dictionary>
h06e3d351bb84094abb4330d3c9eace1: Client 1|ad8bf1c5-efba-46b5-b392-ebff656f98a3

SharePoint

Step 7 – Call REST API Web Service

Finally, call the REST API web service to update the managed metadata field. To do this we need to insert the “Call HTTP Web Service” action in our workflow.

https://site/_api/web/lists/getbytitle('listtitle')/items(x)

SharePoint

Set the action verb to POST.

SharePoint

Step 8

Set the request headers to headers dictionary and the request content to data dictionary. For setting these values, go to the properties of the “Call HTTP web service” action.

SharePoint

The complete Workflow steps and its output is shown below,

SharePoint

Output

SharePoint

Additional Notes

We can also update the managed metadata field in Visual Studio using SharePoint Client Object Model (I did by creating a Console Application in VS2015 and writing Managed CSOM). The point to remember is we need to use TaxonomyFieldValue API, use its WssId property, and set its value to 1.

SharePoint

Summary

In this article, we saw how we could update the managed metadata field in SharePoint Designer 2013 Workflow.