Solved

How to update List item Managed Metadata Column with JSOM

Posted on 2016-07-25
2
393 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

636 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