我使用以下代码将一个项目插入列表。
function createitem() {
var selectListBox = document.getElementById("txtFormName");
var selectedListTitle = selectListBox.value;
var selectedList = web.get_lists().getByTitle(selectedListTitle);
var listItemCreationInfo = new SP.ListItemCreationInformation();
var newItem = selectedList.addItem(listItemCreationInfo);
newItem.set_item('Title','abc');
newItem.update();
context.load(newItem);
context.executeQueryAsync(onItemCreationSuccess, onItemCreationFail);
}
工作正常。现在我想用
newItem.set_item('Title1','def');
newItem.set_item('Title2','xyz');
.
.
.
and so on
在循环中插入同一行中的多个项目。我怎样才能做到这一点。亲切的指导。
答案 0 :(得分:0)
由于SharePoint CSOM API支持Request Batching,以下示例演示了如何使用单个请求向服务器创建多个列表项:
function createListItem(context,listTitle,itemProperties)
{
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
for(var propName in itemProperties) {
listItem.set_item(propName, itemProperties[propName])
}
listItem.update();
return listItem;
}
//Usage
var contactItems = [];
var context = new SP.ClientContext.get_current();
var contactProperties = {'Title': 'Doe','FirstName': 'John'};
//1.Prepare multiple list items
for(var i = 0; i < 16; i++) {
var contactItem = createListItem(context,'Contacts',contactProperties);
contactItems.push(contactItem);
}
//2. Submit request to the server to create list items
context.executeQueryAsync(
function() {
console.log(contactItems.length + ' contacts have been created');
},
function(sender, args) {
console.log(args.get_message());
}
);
关键点:
SP.ClientContext.executeQueryAsync
方法