8

I am trying to write the value of a managed metadata column across several documents in a document library at once using JSOM, but for some reason the way I'm trying only writes the value to the first item in my set that I write to.

For example, say I have two columns, a text field and a managed metadata field. I can loop over every document and write to both fields. The text field will get written to every document correctly, but the managed metadata value only gets written to the first document for which I call item.update(); The value of the managed metadata field is correct, and it does definitely work properly for the first item at least.

I have tried both of the following patterns for the value:

'Label Text|0f21b3b6-7701-4da1-991a-05355ac64393'
'1033#;Label Text|0f21b3b6-7701-4da1-991a-05355ac64393'

I've pared down a simple test case code sample demonstrating my example above:

var fieldValue = '1033#;Supply Chain|0f21b3b6-7701-4da1-991a-05355ac64393';
var fieldName = 'test_x0020_managed_x0020_metadata';
var listName = 'Documents';

var context = SP.ClientContext.get_current();
var query = new SP.CamlQuery();
query.set_viewXml('<View><Query></Query></View>');
var items = context.get_web().get_lists().getByTitle(listName).getItems(query);
context.load(items);
context.executeQueryAsync(function() {
    var enumerator = items.getEnumerator();
    while(enumerator.moveNext()) {
        var item = enumerator.get_current();
        item.set_item('other_fields', 'get_written_to');
        item.set_item(fieldName, fieldValue);
        item.update();
    }
    context.executeQueryAsync(
        function(sender, args) { console.log(arguments); }, 
        function(sender, args) { console.log(arguments); }
    );
}, function(sender, args) { console.log(arguments); });

I've tried to use SP.Taxonomy.TaxonomyFieldValue as well, but I get the same results. Is it just not possible to write managed metadata columns across multiple documents in one executeQueryAsync call?

  • I should also note: the executeQueryAsync call does succeed and no errors get thrown.. but in the web service response, you can see it failed to write the managed metadata value to all but the first document – Dennis George Aug 26 '14 at 16:22
9

Depending whether taxonomy field is single-valued or or multi-valued use the corresponding method:

How to set Managed Metadata field value via JSOM

The following example demonstrates how to update taxonomy field value for a several list items:

var listName = '<list title>';
var fieldName = '<field name>'; 
var termId = '<term guid>';
var termLabel = '<term label>';


var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listName); 
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
context.load(items);

var field = list.get_fields().getByInternalNameOrTitle(fieldName);
var txField = context.castTo(field, SP.Taxonomy.TaxonomyField);
context.load(txField);

context.executeQueryAsync(function() {

    if (txField.get_allowMultipleValues()) { /* Is multiple taxonomy field? */

         //1. Prepare TaxonomyFieldValueCollection object
         var terms = new Array();             
         terms.push("-1;#" + termLabel + "|" + termId);
         var termValueString = terms.join(";#");
         var termValues = new SP.Taxonomy.TaxonomyFieldValueCollection(context, termValueString, txField);

          //2. Update multi-valued taxonomy field
         var e = items.getEnumerator();
         while(e.moveNext()) {
            var item = e.get_current();
            txField.setFieldValueByValueCollection(item, termValues);
            item.update();
         }
    }
    else {

         //1. Prepare TaxonomyFieldValue
         var termValue = new SP.Taxonomy.TaxonomyFieldValue();
         termValue.set_label(termLabel);
         termValue.set_termGuid(termId);
         termValue.set_wssId(-1);

         //2. Update single-valued taxonomy field
         var e = items.getEnumerator();
         while(e.moveNext()) {
            var item = e.get_current();
            txField.setFieldValueByValue(item, termValue);
            item.update();
         }
    }

    context.executeQueryAsync(
       function(sender, args) { 
           console.log('List items have been updated'); 
       }, 
       onError
    );

  }, 
  onError
);

function onError(){
   console.log(args.get_message());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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