简体   繁体   English

如何将DataTable复制到SharePoint SPList?

[英]How to copy a DataTable to a SharePoint SPList?

I have my source list data in the sourceList data table and want to copy that data to its root list. 我在我的源列表数据sourceList数据表,并希望将数据复制到它的根目录。

How can I do that? 我怎样才能做到这一点?

private void MoveToTopTaskList(DataTable sourceList, SPSite DestinationSiteCollection)
{
    SPWeb Destinationsite = DestinationSiteCollection.OpenWeb();
    SPList DestinationList = Destinationsite.Lists[TASKS];
    SPListItem DestinationListItem = DestinationList.Items.Add();

    foreach (DataRow row in sourceList.Rows)
    {

    }
}

Best Approach for the above case is to Use the ProcessBatchData Method of the SPWeb Object. 上述情况的最佳方法是使用SPWeb对象的ProcessBatchData方法。 This will help you to update List items in to the List in Batch. 这将帮助您将列表项更新到批处理列表中。

  1. You need to build an XML tags that will have details for inserting the data to the list. 您需要构建一个XML标记,其中包含将数据插入列表的详细信息。
  2. If you have large number of records to be inserted to the list consider spliting it in to smaller batchs. 如果要将大量记录插入列表,请考虑将其拆分为较小的批次。 Say if you have 1000 records do it in two 500 sets. 假如你有1000条记录,那就是两套500套。
  3. While building the XML make sure you use StringBuilder class to append the string. 构建XML时,请确保使用StringBuilder类附加字符串。
  4. Refer these Links Link1 Link2 Link3 for more information on ProcessBatchData 有关ProcessBatchData的更多信息,请参阅这些链接Link1 Link2 Link3

In case if you want to do it using the OM. 如果你想使用OM这样做。 Then follow code 然后按照代码

`SPWeb Destinationsite = DestinationSiteCollection.OpenWeb();
SPList DestinationList = Destinationsite.Lists[TASKS];    
SPListItem DestinationListItem = DestinationList.Items.Add();
  foreach (DataRow row in sourceList.Rows)
{
    DestinationListItem = DestinationList.Items.Add();
    DestinationListItem["Field1"]=row["Col"].ToString();
    DestinationListItem["Fieldn"]=row["Coln"].ToString();
    DestinationListItem.Update()

}

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM