Solved

How to update List item Managed Metadata Column with JSOM

Posted on 2016-07-25
  • MS SharePoint
  • JavaScript
  • Office 365
  • Windows Server 2012
  • MS Development-Other
  • +1
2
129 Views
Last Modified: 2016-07-26
I have simple list with one title column and one Metadata Column, and iam trying to update my list item but getting an error:

I found this article on google, and i have tried to follow it by implementing JSOM to update my List item. Error iam getting is when context.executeQueryAsync:
Error: Invalid field or parameter url.

 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(appWebUrl);
        var factory = new SP.ProxyWebRequestExecutorFactory(appWebUrl);
        context.set_webRequestExecutorFactory(factory);
        var appContextSite = new SP.AppContextSite(context, spHostUrl);
        var list = appContextSite.get_web().get_lists().getByTitle(listTitle);
        var item = list.getItemById(tileId);
        var field = list.get_fields().getByInternalNameOrTitle(taxFieldName);
        var txField = context.castTo(field, SP.Taxonomy.TaxonomyField);

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

        context.executeQueryAsync(
           function () {
               var termSetId = txField.get_termSetId().toString();
               var termId;

               getTermIdForTerm(function success(id) {
                   //termId = id;
                   console.log("Kommer hit...<");
                   var termValue = new SP.Taxonomy.TaxonomyFieldValue();
                   termValue.set_label(term);
                   termValue.set_termGuid(termId);
                   termValue.set_wssId(-1);
                   txField.setFieldValueByValue(item, termValue);

                   item.update();
                   context.executeQueryAsync(
                      function () {
                          console.log('field updated');
                      }, function (sender, args) {
                          console.log(args.get_message() + '\n' + args.get_stackTrace());
                      });
               }, function (sender, args) {
                   console.log(args.get_message() + '\n' + args.get_stackTrace());
               }, context, term, termSetId);

           }, function error(err) {
               console.log(err.get_message());
           });
    }

    function getTermIdForTerm(success, error, clientContext, term, termSetId) {
        var termId = "";
        console.log("hmm");
        var tSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
        var ts = tSession.getDefaultSiteCollectionTermStore();
        var tset = ts.getTermSet(termSetId);
        var lmi = new SP.Taxonomy.LabelMatchInformation(clientContext);

        lmi.set_lcid(1033);
        lmi.set_trimUnavailable(true);
        lmi.set_termLabel(term);

        var termMatches = tset.getTerms(lmi);

        clientContext.load(tSession);
        clientContext.load(ts);
        clientContext.load(tset);
        clientContext.load(termMatches);

        context.executeQueryAsync(
          function () {

              if (termMatches && termMatches.get_count() > 0)
                  termId = termMatches.get_item(0).get_id().toString();
              success(termId);

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


    }

Open in new window

0
Comment
Question by:Adnan
  • 2
2 Comments
 

Accepted Solution

by:
Adnan earned 0 total points
ID: 41729627
Here is how i resolved it, this how you can update an existing List item with Managed Metadata Column:
        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());
            });

        }

Open in new window

0
 

Author Closing Comment

by:Adnan
ID: 41729629
Resolved
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
how to add IIS SMTP to handle application/Scanner relays into office 365.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now