Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

3 Answers

I think your list names have gone wrong. I can see that you are getting myListCollection from the list object myList.

 SPListItemCollection myListCollection = myList.Items;

But then the next statement,

BulkUpdate(labList.GetDataTable(), "dbo.TEST_Table");

This is using a list called lablist. Where did that come from? Or am I missing something?

share|improve this answer
Oops, I changed my names to generic titles. I must have missed that one. It's somewhat confidential, and the names are highly reflective of the project. I fixed it in the original post – withoutIf Mar 6 '12 at 15:15

OK, from bottom to top REM out the contents of your delegate. When does the error go away?

Do you have contents in your list? Can you try another list? In your code, can you try instantiating a new datatable, and copying the list content into there, and then loading that datatable's contents in the bulkupdate?

share|improve this answer

I've been away from this for a while, but my solution was this - I was running a console app with SharePoint client dlls in MOSS. It's just not supported.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.