0

我想在共享点文档库中批量插入项目。如何使用 SPWeb.ProcessBatchData() 来实现这一点。

4

1 回答 1

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("&", "&amp");
            XMLSetVarLines.Append(String.Format(BatchXMLSetVar, fieldname, fieldvalue));
        }

        XMLSetMethods.Append(String.Format(BatchXMLMethod, methodID, guid, XMLSetVarLines));

        methodID++;

        XMLSetVarLines.Length = 0;
    }
    return (String.Format(BatchXMLBanner, XMLSetMethods));
}
于 2013-10-05T13:31:39.813 回答