2013-10-04 61 views
0

我想在SharePoint文檔庫中批量插入項目。 如何使用SPWeb.ProcessBatchData()實現此目的。在SharePoint文檔庫中批量插入項目

+0

請發表你的努力程度的代碼。 –

+0

現在我正在使用spweb.files.add()插入項目,藉助於此方法,我可以一次上傳1個文件,因此它影響性能。在谷歌上的某處,我閱讀了有關SPWeb.ProcessBatchData()的內容。用於批量文檔上傳,因此詢問有關SPWeb.ProcessBatchData()。方法 – Dude

回答

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)); 
}