SharePoint 2013:如何使用 JavaScript CSOM 更新多值查找字段

use*_*226 5 javascript sharepoint-2013 csom

我有一个联系人列表,其中有一个名为 ContactType 的多值查找字段。CAML 查询的结果将显示列表项之一的 ContactType 的以下值:

1;#Applicant;#2;#Employee
Run Code Online (Sandbox Code Playgroud)

在针对多值查找字段执行 CSOM 查询后,我查看了 Fiddler,并注意到 SP.FieldLookupValue 对象具有两个具有值的属性:

$1E_1 : 1
$2e_1 : "Applicant"
Run Code Online (Sandbox Code Playgroud)

但是,当您保存值时,您只能设置lookupId,在本例中为1。没有像lookup.set_lookupValue()那样设置值的方法。

我正在尝试将 ContactType 的内容复制到联系人的新列表项中。不幸的是,我在更新 ContactType 字段时没有成功。这是我到目前为止所尝试过的:

var clientContext = new SP.ClientContext.get_current(); 
var oList = clientContext.get_web().get_lists().getByTitle('Contacts');
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);

var contactTypes = new Array();

$.each(contact.contactTypes, function (index, contactType) {
    var lookup = new SP.FieldLookupValue();
    lookup.set_lookupId(contactType.id);
    contactTypes.push(lookup);
});

// other set_item statements skipped for brevity
oListItem.set_item('ContactType', contactTypes);

oListItem.update();
Run Code Online (Sandbox Code Playgroud)

错误信息是:

Invalid lookup value. A lookup field contains invalid data.
Run Code Online (Sandbox Code Playgroud)

我还尝试了以下代码,但没有成功:

lookup.set_lookupId(contactType.id + ";#" + contactType.title);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,错误消息是:

The input string is not in the correct format.
Run Code Online (Sandbox Code Playgroud)

如果我更新单个查找,我没有问题,但问题在于保存查找数组。例如,以下代码可以正常工作:

var lookup = new SP.FieldLookupValue();
lookup.set_lookupId(1);
contactTypes.push(lookup);
oListItem.set_item('ContactType', lookup);
Run Code Online (Sandbox Code Playgroud)

但当尝试保存查找数组时,它不会起作用,如下所示

oListItem.set_item('ContactType', contactTypes);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

use*_*226 4

不要构建 SP.FieldLookupValue 数组,而是将多个联系人类型保存到字符串。

var clientContext = new SP.ClientContext.get_current(); //if the page and the list are in same site.If list is in different site then use relative url instead of get_current
var oList = clientContext.get_web().get_lists().getByTitle('Contacts');
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);

var contactTypes = null;

$.each(contact.contactTypes, function (index, contactType) {
    if (index != 0)
        contactTypes += ';#' + contactType.id + ';#' + contactType.title;
    else
        contactTypes =  contactType.id + ';#' + contactType.title;
});

// other set_item statements omitted for brevity
oListItem.set_item('ContactType', contactTypes);

oListItem.update();

clientContext.executeQueryAsync(
    // success return
    function () {
        var success = true;
    },
    // failure return
    function (sender, args) {
        window.alert('Request to create contact failed. ' + args.get_message() +
                '\n' + args.get_stackTrace());
    })
Run Code Online (Sandbox Code Playgroud)