Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm creating a sharepoint hosted app which is based on javascript only. I wanted to update a multivalue taxonomy field in a list so I wrote this function which didn't work. there is a very little support for javascript csom online.

var list = context.get_web().get_lists().getByTitle('Company');
var item = list.getItemById(2);

var field = list.get_fields().getByInternalNameOrTitle("Departments");
var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);

var terms = new SP.Taxonomy.TaxonomyFieldValueCollection(context,
'Unit 1|5bf47d1f-d890-49d1-a844-85628ca508fd;#Unit 4|334ad23d-d2d8-4acb-ab09-38d2bacb97d4',
taxField);

taxField.setFieldValueByValueCollection(item, terms);

item.update();
context.load(taxField);
context.executeQueryAsync(
function() {
    console.log('field updated');
});

I also used this code

var list = context.get_web().get_lists().getByTitle('Company');
var item = list.getItemById(2);

item.set_item('Departments', 'Unit 1|5bf47d1f-d890-49d1-a844-85628ca508fd;Unit 4|334ad23d-d2d8-4acb-ab09-38d2bacb97d4');
item.update();
context.executeQueryAsync(
function() {
    console.log('field updated');
});
share|improve this question
    
Have a look at this from Msdn msdn.microsoft.com/en-us/library/ee567833.aspx. It specifies valid input formats for at taxonomy field value, and too me yours seem to be reversed (GUID and label) –  Robert Lindgren Jun 13 '13 at 5:23
add comment

1 Answer

up vote 0 down vote accepted

You must include a fake wssid (lookup id) prefix to the value. Below is a code I use to set a multiple value term field from jsom in the app model. This works.

function SetManagedMetaDataField() {
appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
context = new SP.ClientContext(appweburl);
factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostweburl);

var list = appContextSite.get_web().get_lists().getByTitle('Documents');
var item = list.getItemById(5);

var field = list.get_fields().getByInternalNameOrTitle("Cars");
var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);

var terms = new SP.Taxonomy.TaxonomyFieldValueCollection(context,
'-1;#ATS|9f3e8e20-593b-471d-a145-81ff8664fd96;#-1;#CTS|8b18f6df-22be-4548-92b4-8f240d8fbfe5',
taxField);

taxField.setFieldValueByValueCollection(item, terms);

item.update();
context.load(taxField);
context.executeQueryAsync(
function () {
    alert('field updated');
}, function (sender,args) {
    alert(args.get_message() + '\n' + args.get_stackTrace());
});
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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