Javascript 将分类字段设置为多个值

Javascript 将分类字段设置为多个值,javascript,sharepoint,taxonomy,office365,csom,Javascript,Sharepoint,Taxonomy,Office365,Csom,我正在创建一个仅基于javascript的sharepoint托管应用程序。我想更新列表中的一个多值分类字段,所以我编写了这个函数,但它不起作用。对javascript csom online的支持很少 var list = context.get_web().get_lists().getByTitle('Company'); var item = list.getItemById(2); var field = list.get_fields().getByInternalNameOrTi

我正在创建一个仅基于javascript的sharepoint托管应用程序。我想更新列表中的一个多值分类字段,所以我编写了这个函数,但它不起作用。对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');
});
我也使用了这个代码

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');
});

必须在值中包含一个伪wssid查找id前缀。下面是我用来在应用程序模型中从jsom设置多值术语字段的代码。这很有效

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());
});
}

必须在值中包含一个伪wssid查找id前缀。下面是我用来在应用程序模型中从jsom设置多值术语字段的代码。这很有效

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());
});
}

有没有办法先从项目中清除当前术语?您会传递空字符串,然后更新,然后传递术语并再次更新吗?有没有办法先从项目中清除当前术语?您会传递空字符串,然后更新,然后传递术语并再次更新吗?