Update Taxonomy Or Managed Metadata Field Via SharePoint JSOM

In this article, you will learn how to update the taxonomy or managed metadata field via SharePoint JSOM.

In this article, I am sharing code snippets to create List items and update items with Taxonomy or Managed Metadata field on a custom list. I have seen some threads on how to update the multi-value taxonomy fields also. The below code demonstrates both single/multi-valued, taxonomy column update snippets via JSOM.
 
Create List Item  
  1. function CreateItem(isSingleValue){  
  2.      SP.SOD.executeOrDelayUntilScriptLoaded(function () {  
  3.            'use strict';  
  4.             var context = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);  
  5.             var list = context.get_web().get_lists().getByTitle('CustomList');              
  6.             var itemCreateInfo = new SP.ListItemCreationInformation();  
  7.             var item = list.addItem(itemCreateInfo);  
  8.             var field = list.get_fields().getByInternalNameOrTitle("MyMMDField");  
  9.             var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);  
  10.               
  11.             if(isSingleValue == true){ // this you have to handle manually , use   
  12.                 var termValue = new SP.Taxonomy.TaxonomyFieldValue();  
  13.                 termValue.set_label("MyWikiTerm");  
  14.                 termValue.set_termGuid("fb58bc5e-5ce5-41fc-9a90-7431018aa935");  
  15.                 termValue.set_wssId(-1);  
  16.                 taxField.setFieldValueByValue(item, termValue);  
  17.                 item.set_item("Title""Created New Item and Set single valued Taxonomy");  
  18.             }  
  19.             else { // this you have to handle manually  
  20.                 item.set_item("Title""Created New Item and Multi valued Taxonomy");  
  21.                 var pairs = "-1;#MyWikiTerm|fb58bc5e-5ce5-41fc-9a90-7431018aa935;#-1;#MyWikiTerm2|1cee8427-41f1-4a2b-aff3-26c67685988e";  
  22.                 var termValueCollection = new SP.Taxonomy.TaxonomyFieldValueCollection(context,pairs,taxField);  
  23.                 taxField.setFieldValueByValueCollection(item, termValueCollection);  
  24.             }             
  25.             item.update();  
  26.             context.load(item);  
  27.             context.executeQueryAsync(  
  28.                 function () {  
  29.                     console.log('Item created sucessfully: ' + item.get_id());  
  30.                 },   
  31.                 function (sender, args) {  
  32.                   console.log("exception in addItem");  
  33.                 });  
  34.         }, 'SP.Taxonomy.js');  
  35. }  
Let us quickly test this via Chrome Developer Console.
 
Go to page and view site in the Classic experience. 
 
Press F12. Go to Developer Console and paste the above code in the console.
 
Press Enter.
 
We should get an undefined logged-in console. Don't worry. Ignore this message.
 
Update Taxonomy Or Managed Metadata Field Via SharePoint JSOM
 
Now let us try this out, call this function in chrome console developer like below
 
Update Taxonomy Or Managed Metadata Field Via SharePoint JSOM
 
If you get the above message, an item will be created in the list and the Taxonomy field would have been set. Please refer to the below screenshot for reference.
 
Update Taxonomy Or Managed Metadata Field Via SharePoint JSOM
 
The same way, you can try multi-value also by passing the parameter as false in the CreateItem method.
 
Update Item  
  1. function UpdateItem(ItemId,isSingleValue){  
  2.      SP.SOD.executeOrDelayUntilScriptLoaded(function () {  
  3.            'use strict';  
  4.             var context = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);  
  5.             var list = context.get_web().get_lists().getByTitle('CustomList');              
  6.             var item = list.getItemById(ItemId);  
  7.             var field = list.get_fields().getByInternalNameOrTitle("MyMMDField");  
  8.             var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);  
  9.               
  10.             if(isSingleValue == true){ // this you have to handle manually , use   
  11.                 var termValue = new SP.Taxonomy.TaxonomyFieldValue();  
  12.                 termValue.set_label("MyWikiTerm");  
  13.                 termValue.set_termGuid("fb58bc5e-5ce5-41fc-9a90-7431018aa935");  
  14.                 termValue.set_wssId(-1);  
  15.                 taxField.setFieldValueByValue(item, termValue);  
  16.                 item.set_item("Title""Updated item and Set single valued Taxonomy");  
  17.             }  
  18.             else { // this you have to handle manually  
  19.                 item.set_item("Title""Updated Item and Multi valued Taxonomy");  
  20.                 var pairs = "-1;#MyWikiTerm|fb58bc5e-5ce5-41fc-9a90-7431018aa935;#-1;#MyWikiTerm2|1cee8427-41f1-4a2b-aff3-26c67685988e";  
  21.                 var termValueCollection = new SP.Taxonomy.TaxonomyFieldValueCollection(context,pairs,taxField);  
  22.                 taxField.setFieldValueByValueCollection(item, termValueCollection);  
  23.             }             
  24.             item.update();  
  25.             context.load(item);  
  26.             context.executeQueryAsync(  
  27.                 function () {  
  28.                     console.log('Item updated sucessfully: ' + item.get_id());  
  29.                 },   
  30.                 function (sender, args) {  
  31.                   console.log("exception in updating item");  
  32.                 });  
  33.         }, 'SP.Taxonomy.js');  
  34. }  
Use the same Chrome Developer Console technique to quickly test this function. Type ''UpdateItem(8,false)". Please note that we are updating the same item created using CreateItem method.
 
Calling update method
 
Update Taxonomy Or Managed Metadata Field Via SharePoint JSOM
 
Output
 
Update Taxonomy Or Managed Metadata Field Via SharePoint JSOM
 
I hope this helps ... happy coding..!!!!