Solved

How to update List item Managed Metadata Column with JSOM

Posted on 2016-07-25
  • Microsoft SharePoint
  • JavaScript
  • Office 365
  • Windows Server 2012
  • Microsoft Development
  • +1
2
211 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Microsoft Office Picture Manager has a Picture Shortcuts pane that shows a list with the Recently Browsed folders. While creating my video Micro Tutorial here at Experts Exchange showing How to Install Microsoft Office Picture Manager in Office 2013…
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…

807 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