It's a short post that describes how to easily update Managed Metadata Field value using SharePoint REST API or PnPJS.
Although the Title of the post is about REST API and PnPJS I'll describe the process using PnPJS only as it is easier and absolutely identical to REST API. Usually, to update list item we use update for the list item like that:
The documentation for the update method can be found here But this method is not very convenient when we want to update Managed Metadata Field value. And especially if the field can contain multiple terms selected. Even for a single term the call will look like that:
where Label is not a term label but its ID. If you don't have terms localization it will be "1". But anyway, it's not really cool to construct such objects, right? That's why I would recommend to use another available method: validateUpdateListItem. In this method we need to provide array of objects with the next properties:
And for the Managed Metadata Field the FieldValue value can be constructed really easily: term_label + "|" + term_id + ";" And this format works both for single value and multiple values So, for example we have Categories field and we want to set the value to contain terms "HR" and "Benefits". We can do that like this:
If you want to update multiple fields, just add them to the array:
As a cocnlusion, for simple field types update method is easier, but for such fields as Managed Metadata validateUpdateListItem is better.
Comments