下面将以create为例(edit同样适用)
实例化list item对象
var clientContext = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('TestList');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
给不同类型的列设置值
//单行文本类型
oListItem.set_item('Title', 'My New Item!');
//选项类型(单选)
oListItem.set_item('PetkaChoiceDrop', 'Enter Choice #1');
//选项类型(多选)
var petkaChoiceMultiArray = new Array("Enter Choice #1","Enter Choice #2");
oListItem.set_item('PetkaChoiceMulti', petkaChoiceMultiArray);
//Lookup (单选)
var PetkaLookupSingle = new SP.FieldLookupValue();
PetkaLookupSingle.set_lookupId(2);
oListItem.set_item('PetkaLookup', PetkaLookupSingle);
//Lookup (多选)
var lookupsIds = [1,2];
var lookups = [];
for (var ii in lookupsIds) {
var lookupValue = new SP.FieldLookupValue();
lookupValue.set_lookupId(lookupsIds[ii]);
lookups.push(lookupValue);
}
oListItem.set_item('PetkaLookupMulti', lookups);
//Yes或No :Yes=1 / No=0
oListItem.set_item('PetkaYesNo', 1);
// 单用户
var singleUser = SP.FieldUserValue.fromUser('Peter Dotsenko');
oListItem.set_item('PetkaPersonSingle', singleUser);
//多用户
var petkaUserMultiArray = new Array("peterd@domain.com","Peter Dotsenko","domain\\peterd");
var lookups = [];
for (var ii in petkaUserMultiArray) {
var lookupValue = SP.FieldUserValue.fromUser(petkaUserMultiArray[ii]);
lookups.push(lookupValue);
}
oListItem.set_item('PetkaPersonMulti', lookups);
//超链接或图片字段
var hyperLink = new SP.FieldUrlValue();
hyperLink.set_url("http://cnn.com");
hyperLink.set_description("CNN");
oListItem.set_item('PetkaHyperLink', hyperLink);
//币种字段
oListItem.set_item('PetkaCurrency', '100');
//日期字段
oListItem.set_item('PetkaDateTime', '3/14/2014');
//多行文本字段
oListItem.set_item('PetkaMultiText', '<p><strong>Hello!</strong></p>');
设置完成后添加到list中
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
//单独定义执行成功或失败的function
function onQuerySucceeded()
{
SP.UI.Notify.addNotification('Item created: ' + oListItem.get_id());
}
function onQueryFailed(sender, args)
{
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}