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
168 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

776 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