1

As i have understand it by googling a litle, that its not posible to use REST for Taxonomy fields ? I was not able to update my list doing it by http REST . But it should be posible with JSOM.

How can I save a managed metadata value in my SP.List? I have a list with one MMD column which I want to update.

My code looks like:

  $scope.saveProsessConnection = function (propertyName) {
        var taxPickerIndex = $("#taxPickerOpenSingle").val();
        console.log(taxPickerIndex);
        var result = $.parseJSON(taxPickerIndex);
        console.log(result);
        var listTitle = "GNTiles";
        var taxFieldName = "Prosess";
        var termGUID = result[0].Id;
        var termLabel = result[0].Name;
        console.log(termGUID);
        console.log(result);

        var context = new SP.ClientContext(appWebUrl);
        var list = context.get_web().get_lists().getByTitle(listTitle);
        var item = list.getItemById(tileId);
        //console.log('Item Id: '+item.id);

        var termValue = "-1" + ";#" + termLabel + "|" + termGUID;
        item.set_item(taxFieldName, termValue);
        item.update();


        context.load(item);
        context.executeQueryAsync(onSuccess, onFailure);


        function onSuccess() {
            //retriveListItem(); 
            console.log("Success!!")
        }

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

And my data looks like :

[{"Id":"e0a1f57a-6746-4ecc-b119-47f2ff80a1ea","Name":"L3-2 Forbedringsprosess","PathOfTerm":"L3 Lede og forbedre;L3-2 Forbedringsprosess","Children":[],"Level":1,"RawTerm":null}] 

The error iam getting is:

Request failed. Unexpected response data from server. null

2 Answers 2

1

Below snippet can be used to update MMD field

var termValue = "-1;#" + termLabel + "|" + termGUID;
item.set_item("Process", termValue);
item.update();
7
  • Thanks for the response, i did almost like you showed in your comment "Amal". But still iam getting error: Jul 4, 2016 at 13:14
  • Can you confirm the Id and Value is proper? Jul 4, 2016 at 13:15
  • yes Id and value is proper.... Jul 4, 2016 at 13:56
  • Here is how my data looks like: [{"Id":"e0a1f57a-6746-4ecc-b119-47f2ff80a1ea","Name":"L3-2 Forbedringsprosess","PathOfTerm":"L3 Lede og forbedre;L3-2 Forbedringsprosess","Children":[],"Level":1,"RawTerm":null}] Jul 5, 2016 at 7:46
  • You can se how data looks like in comment above, i just get an error on context.executeQueryAsync : Request failed. Unexpected response data from server. null Jul 5, 2016 at 7:48
0

Here is how i resolved my case with updating a 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());
        });

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.