我有一个包含分类字段的列表。我想要一个函数来更新该列表中的项目(我有 id)并将该字段更改为其值之一(始终相同)。我已经搜索并使用了我过去使用的函数,这是我现在正在使用的代码:
function SetSingleTaxonomyField(siteUrl, listName, fieldInternalName, itemId, term) {
return new Promise(function (resolve, reject) {
var context = new SP.ClientContext(siteUrl);
var list = context.get_web().get_lists().getByTitle(listName);
var listItem = list.getItemById(itemId);
context.load(listItem);
var categoryField = list.get_fields().getByInternalNameOrTitle(fieldInternalName);
var taxonomyValue = set_taxonomyField(term);
var taxField = context.castTo(categoryField, SP.Taxonomy.TaxonomyField);
taxField.setFieldValueByValue(listItem, taxonomyValue);
listItem.update();
context.load(listItem);
context.executeQueryAsync(function () {
resolve();
//console.log('Field successfully updated.');
}, function (sender, args) {
reject('An error occurred:' + args.get_message());
//console.log('An error occurred:' + args.get_message());
});
});
}
上面的行:
var taxonomyValue = set_taxonomyField(term);
调用这个函数:
function set_taxonomyField(term) {
var taxonomyValue = new SP.Taxonomy.TaxonomyFieldValue();
if (term !== undefined) {
taxonomyValue.set_label(term.Title);
taxonomyValue.set_termGuid(term.Id);
taxonomyValue.set_wssId(-1);
}
return taxonomyValue;
}
异常触发于
var taxField = context.castTo(categoryField, SP.Taxonomy.TaxonomyField);
这是我得到的错误:
Possible Unhandled Promise Rejection: Error: Sys.ArgumentException: Value does not fall within the expected range.
Parameter name: type
at Function.Error.create (ScriptResource.axd?d=N...fc8ae3:5)
at Function.Error.argument (ScriptResource....0&t=72fc8ae3:5)
at SP.ClientContext.castTo (sp.runtime.js:2)"
我还有其他类似的功能,但这是我唯一“硬编码”的功能(术语名称、字段内部名称和术语 ID)。但是使用相同的术语检查其他功能,这些值感觉正确。
我不知道如何解决这个问题,在此先感谢。