Take the 2-minute tour ×
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
    
Are you running the code from one of the servers or are you running it remotely? –  NotVonKaiser Aug 2 '13 at 11:56

4 Answers 4

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
1  
As long as you're running the application from one of your SharePoint servers, you should be able to run the app. –  NotVonKaiser Aug 2 '13 at 11:52

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

If you run console app on one of SharePoint server, app should work, but I think you forget to change platform from x86 to x64. This is common mistake when developing console application for SharePoint. Check Visual Studio project settings -> Build -> Platform target -> set x64.

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.