SP客户端对象模型 - 仅使用一个ExecuteQuery将多个新项添加到列表中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SP客户端对象模型 - 仅使用一个ExecuteQuery将多个新项添加到列表中相关的知识,希望对你有一定的参考价值。
我一直在阅读在排队多个项目以便在SP列表中处理之后只执行一个查询。
我有以下代码,其中有一个按钮,将一堆记录加载到Excel电子表格中的空SP列表中。如果我在每个item.update之后执行executequery就行了......但是我认为应该有更好的方法..我可以排队数组中的项目并一次发送所有项目。我看到了如何使用exisitng列表项来做到这一点的示例......但我还没有看到使用新项目的方法。我能够删除项目...将它们全部缓存并执行一次执行查询以删除所有项目......这显然比每个项目的eq快1000倍。所以希望通过添加新项目来做同样的事情。谢谢你的任何想法。
er
private void buttonAddIndividualApplicants_Click(object sender, EventArgs e)
{
logThis("Start adding all individual applicants...");
//Set up SCOM
ClientContext context = new ClientContext(textBoxSPSite.Text);
List list = context.Web.Lists.GetByTitle(textBoxSPList.Text);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = list.AddItem(itemCreateInfo);
//ListItemCollection items = new[];
//Set up Excel
var package = new ExcelPackage(new FileInfo(GlobalVars.ssFileName));
ExcelWorksheet workSheet = package.Workbook.Worksheets[GlobalVars.ssApplicantsTab];
//Start iterating through ss
for (int i = workSheet.Dimension.Start.Row + 1;
i <= workSheet.Dimension.End.Row;
i++)
{
logThis("Row:" + i);
string van = workSheet.Cells[i, 1].Value.ToString();
string appID = workSheet.Cells[i, 2].Value.ToString();
string name = workSheet.Cells[i, 3].Value.ToString();
string email = workSheet.Cells[i, 4].Value.ToString();
logThis(van + "-" + appID + "-" + name + "-" + email + " queued for processing.");
//Push an item to the stack:
oListItem["AppID"] = appID;
oListItem["ApplicantName"] = name;
oListItem["VAN"] = van;
oListItem["ApplicantEmailAddress"] = email;
oListItem.Update();
//context.ExecuteQuery(); ***with ExecuteQuery here it works
}
//After all items pushed onto stack...call ExQuery to apply
logThis("Starting ExecuteQuery to process queued list items...");
context.ExecuteQuery(); //Here it gives me only last name in spreadsheet
logThis("FINISHED ADDING INDIVIDUAL APPLICANTS");
}
答案
明白了。我把ListItem放在了错误的地方。需要它在我的for循环中而不是在它之外。工作代码如下:
private void buttonAddIndividualApplicants_Click(object sender, EventArgs e)
{
logThis("Start adding all individual applicants...");
//Set up SCOM
ClientContext context = new ClientContext(textBoxSPSite.Text);
List list = context.Web.Lists.GetByTitle(textBoxSPList.Text);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
//Set up Excel
var package = new ExcelPackage(new FileInfo(GlobalVars.ssFileName));
ExcelWorksheet workSheet = package.Workbook.Worksheets[GlobalVars.ssApplicantsTab];
//Start iterating through ss
for (int i = workSheet.Dimension.Start.Row + 1;
i <= workSheet.Dimension.End.Row;
i++)
{
ListItem oListItem = list.AddItem(itemCreateInfo); //**MOVED ListItem into for loop fixed it
logThis("Row:" + i);
string van = workSheet.Cells[i, 1].Value.ToString();
string appID = workSheet.Cells[i, 2].Value.ToString();
string name = workSheet.Cells[i, 3].Value.ToString();
string email = workSheet.Cells[i, 4].Value.ToString();
logThis(van + "-" + appID + "-" + name + "-" + email + " queued for processing.");
//Push an item to the stack:
oListItem["AppID"] = appID;
oListItem["ApplicantName"] = name;
oListItem["VAN"] = van;
oListItem["ApplicantEmailAddress"] = email;
oListItem.Update();
//context.ExecuteQuery();
}
//After all items pushed onto stack...call ExQuery to apply
logThis("Starting ExecuteQuery to process queued list items...");
context.ExecuteQuery();
logThis("FINISHED ADDING INDIVIDUAL APPLICANTS");
}
以上是关于SP客户端对象模型 - 仅使用一个ExecuteQuery将多个新项添加到列表中的主要内容,如果未能解决你的问题,请参考以下文章
为 apollo 客户端创建反应组件,带有对象参数的简单模型
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office的JavaScript对象模型