Solved

How to update List item Managed Metadata Column with JSOM

Posted on 2016-07-25
2
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This Experts Exchange lesson shows how to use VBA to loop through rows in Excel.  In order to sort, filter, and use database features, there needs to be a value in each column for every row. When data arrives with values missing, code to copy values…

759 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