2015-09-17 2 views
3

У меня есть список контактов, в котором есть поле для многозначного поиска, называемое ContactType. Результат запроса CAML покажет следующее значение ContactType для одного из элементов списка:SharePoint 2013: Как обновить многозначное поле поиска с использованием JavaScript CSOM

1;#Applicant;#2;#Employee 

Я посмотрел на скрипача после выполнения запроса CSOM от поля поиска в многозначной и заметил, что СП объект .FieldLookupValue имеет два свойства со значениями:

$1E_1 : 1 
$2e_1 : "Applicant" 

Однако при сохранении значения можно установить только на 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(); 

Сообщение об ошибке:

Invalid lookup value. A lookup field contains invalid data. 

Я также экспериментировал со следующим кодом без успеха:

lookup.set_lookupId(contactType.id + ";#" + contactType.title); 

В этом случае сообщение об ошибке:

The input string is not in the correct format. 

Если я обновляю один поиск, у меня нет проблем, но проблема заключается в сохранении массива поисков. Например, следующий код работает отлично:

var lookup = new SP.FieldLookupValue(); 
lookup.set_lookupId(1); 
contactTypes.push(lookup); 
oListItem.set_item('ContactType', lookup); 

но не играть в мяч при попытке сохранить массив выборок, как в

oListItem.set_item('ContactType', contactTypes); 

Любые идеи?

ответ

2

Не создавайте массив 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()); 
    }) 
Смежные вопросы