How to copy DataTable to SharePoint SPList?

I have the source list data in a data table sourceListand you want to copy this data to your root list.

How can i do this?

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)
    {

    }
}
+3
source share
1 answer

The best approach for the above case is to use the ProcessBatchData strong> method of the SPWeb object . This will help you update the list items in the list in batch mode.

  • You need to create XML tags that will contain the details for inserting data into the list.
  • , , , . , 1000 , 500 .
  • XML , StringBuilder.
  • Link1 Link2 Link3 ProcessBatchData​​strong >

OM.

`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()

}

`

+2

Source: https://habr.com/ru/post/1712557/


All Articles