I need to export a SharePoint list to an external SQL database. My initial idea is to open the list as a datatable and bulk update. I created a console app I can run anytime I need to sync the data. Something like this:
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite site = new SPSite("http://MySite"))
{
using (SPWeb web = site.OpenWeb())
{
SPList myList = web.GetListFromUrl("/default/Lists/TestList/AllItems.aspx");
SPListItemCollection myListCollection = myList.Items;
BulkUpdate(myListCollection.GetDataTable(), "dbo.TEST_Table");
}
}
});
private void BulkUpdate(DataTable table, string destName)
{
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlBulkCopy copy = new SqlBulkCopy(conn))
{
copy.ColumnMappings.Add(0, 0);
copy.DestinationTableName = destName;
copy.WriteToServer(table);
}
}
}
Unfortunately, it isn't doing anything. I'm getting a COM error: "failed due to the following error: 80040154."
I'm not sure where it's coming from. If I remove the delegate to look into the code, it just tells me it's an invalid site, since it's being accessed externally.
Any thoughts?