Updating A Managed Metadata (MMS) Column Dynamically In A SharePoint List Using MS Flow

When updating an MMS Column in a SharePoint list, the value of the MMS column is expected to be in a certain format. This article explains the right way of updating these columns from MS Flow by identifying the Terms Guid.

INTRODUCTION

 
Unlike SharePoint standard columns, the Managed Metadata (MMS) column expects the value to be in a certain format when we update such a column using code, REST APIs, or through MS Flow. Below is the expected format for setting the value to an MMS column.
 
TERM-LABEL|TERM_GUID
 
An example would be "Lenovo|xxxx-xxxx-xxxx-xxxx" (separated by a pipe symbol).
 

WHY NOT USE THE DEFAULT UPDATE ITEM ACTION?

 
The defaut action supports updating an MMS column and works perfectly. But, we have to set a static (or a chosen) value while designing the flow itself. Consider the below example
 
Updating An Managed Metadata (MMS) Column Dynamically In A SharePoint List Using MS Flow
 
Consider we have an MMS Column called 'Make', which has three terms - Dell, HP, and Lenovo. The value for this column is set while designing the Flow and it will be the same for all the runs. There could be business cases where we have to set a different value for these columns based on other conditions or based on the other column values. Considering the previous example, if we want to set the "Make" value as Lenovo when the title contains Laptop, we cannot just enter text into this column; it will fail. And, we cannot keep adding nested conditions with multiple Update Item actions. The solution won't be scalable when we have 100s of terms for an MMS column.
 

THE SOLUTION

 
When we select a value for the MMS column ("Make" in the example above), the default Update Item action internally queries for the GUID of this term and sets the field's value as 'Dell|xxxx-xxxx-xxxx-xxxx'. Now, if we want to dynamically assign values to an MMS column, we can use the REST API to call the ValidateUpdateListItem endpoint and set the MMS column value in the expected format
 
Given below is a sample POST call to the REST API.
  1. POST  
  2. /sites/Internal/_api/web/lists/getByTitle('Products')/items(id)/ValidateUpdateListItem()  
  3.   
  4. PAYLOAD  
  5. {  
  6.     "formValues":   [  
  7.     {"FieldName":"Make","FieldValue":"Lenovo|<<guid-of-the-term>>"},  
  8.     ],  
  9.     "bNewDocumentUpdate":false,  
  10.     "checkInComment":null  

HOW TO GET THE TERM'S GUID

 
We can use the 'Send an HTTP Request to SharePoint' action to call the below REST API which gives us all the details about the required managed metadata term.
  1. POST  
  2. /sites/Internal/_vti_bin/TaxonomyInternalService.json/GetSuggestions  
  3.   
  4. PAYLOAD  
  5. {"start":"[NEW VALUE]",  
  6. "lcid":1033,  
  7. "sspList":"[GUID OF SSPLIST]",  
  8. "termSetList":"[GUID OF TERM SET LIST]",  
  9. "anchorId":"00000000-0000-0000-0000-000000000000",  
  10. "isSpanTermStores":false,  
  11. "isSpanTermSets":false,  
  12. "isIncludeUnavailable":false,  
  13. "isIncludeDeprecated":false,  
  14. "isAddTerms":false,  
  15. "isIncludePathData":false,  
  16. "excludeKeyword":false,  
  17. "excludedTermset":"00000000-0000-0000-0000-000000000000"
The payload for this request expects certain value, and we need to pass the following three dynamic values which are specific for an MMS column within a SharePoint list. Rest of the values for the properties like lcid and others can be set to default static value. We have to provide the values for the following three parameters.
  • start  - The term label we are searching for (ex: Lenovo)
  • sspList - The GUID of the MMS Service Application (previously known as SSP) of the Term Set
  • termSetList - The GUID of the source term set for the MMS column.

FINDING THE GUIDs OF SSP and TERM SET

 
We can query for the fields of the SharePoint list to find these values. Below is the REST API call with a sample response.
  1. GET  
  2. _api/web/lists/getByTitle('Products')/fields?$filter=InternalName eq 'Make'  
  3.   
  4. SAMPLE RESPONSE PAYLOAD  
  5. {  
  6.   "value": [  
  7.     {  
  8.       "SspId""9d7dab30-f5f1-4e32-b16e-7d4066af29e8",  
  9.       "TermSetId""f0ca54da-dc64-4120-bf1a-ccd1604aa75c",  
  10.     }  
  11.   ]  
  12. }  
  13. //Removed all other properties in the response for readability 
The response contains 2 properties SspId and TermSetId which can be used for calling the GetSuggestions endpoint to get the Terms GUID.
 
By calling these SharePoint REST APIs, we can dynamically find the GUID of a term and set its value using the ValidateUpdateListItem endpoint.
 

MS FLOW STEPS

 
Here is how the MS Flow steps should look like.
 
STEP 1
 
Query for the filed to find the MMS Columns metadata using 'Send an HTTP Request to SharePoint' action.
 
Updating An Managed Metadata (MMS) Column Dynamically In A SharePoint List Using MS Flow
 
STEP 2
 
Initialize two variables and assign the sspId and termSetList values from the previous step's response.
 
Updating An Managed Metadata (MMS) Column Dynamically In A SharePoint List Using MS Flow
 
sspId value : first(body('GetMMSMeta')?['value'])?['SspId']
termSetId : first(body('GetMMSMeta')?['value'])?['TermSetId']
 
STEP 3
 
A 'Send an HTTP Request to SharePoint' action calls the GetSuggestions endpoint with the required payload.
 
Updating An Managed Metadata (MMS) Column Dynamically In A SharePoint List Using MS Flow
 
We set the 2 variables for sspId and termSetId previously declared. The newMMSValue will be another variable which will have the term label that we want to set. The value for this variable can be set based on any logical condition or based on the other values of the list columns.
 
STEP 4
 
Format the new value for the MMS field using the previous response.
 
Updating An Managed Metadata (MMS) Column Dynamically In A SharePoint List Using MS Flow
 
The value for this variable should be : first(first(body('Suggestions')?['d']?['Groups'])?['Suggestions'])?['DefaultLabel']|first(first(body('Suggestions')?['d']?['Groups'])?['Suggestions'])?['Id']
 
Here, we are creating a string value with Term Label suffixed with the Term GUID separated by a pipe(|) symbol.
 
STEP 5
 
Set the list items value using 'Send an HTTP Request to SharePoint' action.
 
Updating An Managed Metadata (MMS) Column Dynamically In A SharePoint List Using MS Flow
 
 

CONCLUSION

 
While the default 'Update Item' action of MS Flow supports updating the MMS Column's value with a static value, it is ideal to set it dynamically using this approach. This makes sure our Flow doesn't break when moving the flow to a different environment and supports business cases which require setting the MMS field values dynamically.