我想在sharepoint文档库中批量插入项目。 如何使用SPWeb.ProcessBatchData()实现此目的。
答案 0 :(得分:0)
以下是从此链接获取的SPWeb.ProcessBatchData方法的一些示例代码: http://www.arboundy.com/code/sharepoint/bulk-add-new-items-to-a-sharepoint-list-using-processbatchdata/
private readonly string BatchXMLBanner =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:Batch OnError=\"Continue\">{0}</ows:Batch>";
private readonly string BatchXMLMethod =
"<Method ID=\"{0}\"><SetList>{1}</SetList><SetVar Name=\"ID\">New</SetVar><SetVar Name=\"Cmd\">Save</SetVar>{2}</Method>";
private readonly string BatchXMLSetVar =
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#{0}\">{1}</SetVar>";
Batch = buildBatch(dataTable, TargetList.ID);
BatchReturn = oWeb.ProcessBatchData(Batch);
private string buildBatch(DataTable dataTable, Guid guid)
{
StringBuilder XMLSetVarLines = new StringBuilder();
StringBuilder XMLSetMethods = new StringBuilder();
int methodID = 0;
foreach(DataRow row in dataTable.Rows)
{
foreach(DataColumn column in dataTable.Columns)
{
var fieldname = column.ColumnName;
var fieldvalue = row[fieldname].ToString().Replace("&", "&");
XMLSetVarLines.Append(String.Format(BatchXMLSetVar, fieldname, fieldvalue));
}
XMLSetMethods.Append(String.Format(BatchXMLMethod, methodID, guid, XMLSetVarLines));
methodID++;
XMLSetVarLines.Length = 0;
}
return (String.Format(BatchXMLBanner, XMLSetMethods));
}