1

How can i update List item with Managed Metadata column using REST or JSOM? Any good sample or suggestion will be appriciated....

0

To update managed metadata column, you need to get the associate termSet for the column and also get the term Id from that termset.

To update Managed Metadata column using REST check this

Below is the code to add the item with managed metadata column using JSOM. Read Comments in code for more description.

jQuery(function($) {  
      var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";  
      $.getScript(scriptbase + "SP.Runtime.js",  
     function () {  
       $.getScript(scriptbase + "SP.js", function(){  
            $.getScript(scriptbase + "SP.Taxonomy.js", ExecuteTaxonomyFunctions);  
       });  
     }  
   );  
 });  
 function ExecuteTaxonomyFunctions()  
 {  
      var context = SP.ClientContext.get_current();  
      var list = context.get_web().get_lists().getByTitle("Resources");  

      var field = list.get_fields().getByInternalNameOrTitle("ResourceType");  
      var txField = context.castTo(field, SP.Taxonomy.TaxonomyField);  
      context.load(field);  
      context.load(txField);  
      //Get the response from server to get the termsetId  
      context.executeQueryAsync(function(){  
        //Get the term set ID  
           var termSetId = txField.get_termSetId().toString();  
           var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
           var termStore = taxSession.getDefaultSiteCollectionTermStore();  
           var termSet = termStore.getTermSet(termSetId);  

           // Get the matching terms based on names  
           var lmi = new SP.Taxonomy.LabelMatchInformation(context);  
           lmi.set_lcid(1033);  
           lmi.set_trimUnavailable(true);  
           lmi.set_termLabel("UI Developer");  

           var termMatches = termSet.getTerms(lmi);  
           context.load(termMatches);  
           //Get the matching term and then add to list item  
           context.executeQueryAsync(function () {  
           if (termMatches && termMatches.get_count() > 0)  
           {  
               // Get the first matching term. As I know it has only one  
               // You need to iterate and get the correct term if you have same name terms  
               var term = termMatches.get_item(0);  

              // Add Item to list   
               var itemCreateInfo = new SP.ListItemCreationInformation();  
               var oListItem = list.addItem(itemCreateInfo);  
               oListItem.set_item('Title', 'Akash Karda');  
               var termFieldValue = new SP.Taxonomy.TaxonomyFieldValue();  
               termFieldValue.set_label(term.get_name());  
               termFieldValue.set_termGuid(term.get_id().toString());  
               termFieldValue.set_wssId(-1);  
               txField.setFieldValueByValue(oListItem, termFieldValue);  
               oListItem.update();       

               context.executeQueryAsync(function () {  
                 alert('Item Added');  
                }, function (sender, args) {  
                  alert(args.get_message() + '\n' + args.get_stackTrace());  
                });  

           }  
         }, function (sender, args) {  
            alert(args.get_message() + '\n' + args.get_stackTrace());  
         });  
      },function(sender,args){  
           alert(args.get_message());  
      });  
 }  

Reference : http://akashkarda.blogspot.com/2014/06/adding-item-to-managed-metadata-column.html

0

Here is how i resolved my case with updating an existing List Item with Managed Metadata Column using JSOM:

        function execRequest() {
        var taxPickerIndex = $("#taxPickerOpenSingle").val();
        var result = $.parseJSON(taxPickerIndex);
        var listTitle = "GNTiles";
        var taxFieldName = "Process";
        var termId = result[0].Id;
        var term = result[0].Name;
        console.log("Term ID: " + termId);
        console.log(result);

        var context = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);
        var list = context.get_web().get_lists().getByTitle(listTitle);
        var item = list.getItemById(tileId);

        var field = list.get_fields().getByInternalNameOrTitle("Prosess");
        var txField = context.castTo(field, SP.Taxonomy.TaxonomyField);

        context.load(field);
        context.load(txField);

        context.executeQueryAsync(function () {
            //Get the term set ID  
           // var termSetId = txField.get_termSetId().toString();
            var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
            var termStore = taxSession.getDefaultSiteCollectionTermStore();
            var termSet = termStore.getTermSet(termSetId);

            // Get the matching terms based on names  
            var lmi = new SP.Taxonomy.LabelMatchInformation(context);
            lmi.set_lcid(1033);
            lmi.set_trimUnavailable(true);
            lmi.set_termLabel("UI Developer");

            var termMatches = termSet.getTerms(lmi);
            context.load(termMatches);
            context.executeQueryAsync(function () {  
                var termFieldValue = new SP.Taxonomy.TaxonomyFieldValue();
                termFieldValue.set_label(term);
                termFieldValue.set_termGuid(termId);
                termFieldValue.set_wssId(-1);
                txField.setFieldValueByValue(item, termFieldValue);
                item.update();

                context.executeQueryAsync(function () {
                    console.log('Item Added');
                }, function (sender, args) {
                    console.log(args.get_message() + '\n' + args.get_stackTrace());
                });
             }, function (sender, args) {  
                console.log(args.get_message() + '\n' + args.get_stackTrace());  
             });

        }, function (sender, args) {
            console.log(args.get_message());
        });

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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