Working with Managed Metadata using SharePoint REST with PNP JS
REST services and SharePoint client side approach is became popular as SPFx was introduced. Combining solution with pnpjs you can create powerfull solutions. As the development basis went from Full-Trust to SharePoint-Addin and now to SPFx. API’s has drastically changed and it happened so fast that not all SharePoint things were implemented fully. This means while you are developing stuff using latest technologies, might be that not all areas in SharePoint are implemented yet. Sometimes you hit the wall and either do an long research over the internet or either give up and get back to old supported way.
One of the most anticipated request on uservoice.com at SharePoint category is “Add managed metadata term store operations to REST API”. I had to make an SPFx solution which I had to create an custom form taking it from list content type and use this form to insert or update item. I had to deal with managed metadata field and I felt into same problem. At the beginning I had to use JSOM approach to fetch Managed metadata for my field. This was working well on classic pages, but was big head ache in modern page, due JSOM libraries were not accessible by default, so I had to load them during run-time and it sucked a lot. It was painful and I do not like JSOM syntax…
So once I decided to try to implement using REST only. I did it, lets say, in with some workarounds I found in the internet, but it works and it looks better than JSOM approach.
So let’s start:


As we have taxonomy terms we can render them in by showing parent and even children terms:


Now we have terms so we need a way to save them when we save the item. This is quite tricky because REST implementation for updating Managed metadata field is really weird, so as for Lookup and User field.





Now we can create and update the items. So what about the rendering item data?





TL;DR
When dealing with FieldTaxonomyMulti:
1. Get Secret name of taxonomy field.
2. While getting item include TaxCatchAll value, to get actual label of the term value.
3. When saving use Secret name instead of internal name and also value should be string;
4. For rendering the label of the term use item value of taxonomy field and search for it in TaxCatchAll field.
When Dealing with FieldTaxonomy:
- While getting item include TaxCatchAll value
- When saving the item use internal name and create an object of selected term.
- For rendering the label of the term use item value of taxonomy field and search for it in TaxCatchAll field.





I hope this was clear enough. I the long version code examples are too difficult to understand refer to the TL;DR version.