0

I'm using JSOM to update a managed metadata column (in SharePoint 2016) using term GUID like this:

...
var oListItem = collListItem.itemAt(i)
oListItem.set_item('SPSBaranID', theTermGUID)
oListItem.update()
...

This is working perfectly fine, but I need to add multiple terms to the same column. I have all GUIDs in a JavaScript array. I tried passing the array as the second parameter inside oListItem.set_item() but it didn't work. Is there a method to achieve this goal? Any advice and help would be appreciated.

1 Answer 1

0

This might help https://www.c-sharpcorner.com/article/update-taxonomymmd-field-via-jsom/

I've copied the code sample directly from that page.

function CreateItem(isSingleValue){  
 SP.SOD.executeOrDelayUntilScriptLoaded(function () {  
       'use strict';  
        var context = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);  
        var list = context.get_web().get_lists().getByTitle('CustomList');              
        var itemCreateInfo = new SP.ListItemCreationInformation();  
        var item = list.addItem(itemCreateInfo);  
        var field = list.get_fields().getByInternalNameOrTitle("MyMMDField");  
        var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);  
          
        if(isSingleValue == true){ // this you have to handle manually , use   
            var termValue = new SP.Taxonomy.TaxonomyFieldValue();  
            termValue.set_label("MyWikiTerm");  
            termValue.set_termGuid("fb58bc5e-5ce5-41fc-9a90-7431018aa935");  
            termValue.set_wssId(-1);  
            taxField.setFieldValueByValue(item, termValue);  
            item.set_item("Title", "Created New Item and Set single valued Taxonomy");  
        }  
        else { // this you have to handle manually  
            item.set_item("Title", "Created New Item and Multi valued Taxonomy");  
            var pairs = "-1;#MyWikiTerm|fb58bc5e-5ce5-41fc-9a90-7431018aa935;#-1;#MyWikiTerm2|1cee8427-41f1-4a2b-aff3-26c67685988e";  
            var termValueCollection = new SP.Taxonomy.TaxonomyFieldValueCollection(context,pairs,taxField);  
            taxField.setFieldValueByValueCollection(item, termValueCollection);  
        }             
        item.update();  
        context.load(item);  
        context.executeQueryAsync(  
            function () {  
                console.log('Item created sucessfully: ' + item.get_id());  
            },   
            function (sender, args) {  
              console.log("exception in addItem");  
            });  
    }, 'SP.Taxonomy.js');  
}  

See the part that uses taxField.setFieldValueByValueCollection(item, termValueCollection);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.