C# Exporting SharePoint List Attachments from Access
<p>I have a task to export all attachments inside of a SharePoint 2007 List. The list has about 5,000 items. I would like to export these items in one large batch. With the research I have done so far, I have only found one solution that meets my requirements. Below is the link to the web page that I'm referring to. I'm getting an error on line 21 with MSACCESS.Doa.DBEngine(). I'm new to C#, so I'm not exactly sure why I'm getting this error. I have the Access referenced in the VS project. I'm using VS Enterprise. What do I need to do to fix this issue? Thank you for your help. </p>

<p>Source
<a href="http://viziblr.com/news/2011/11/5/batch-exporting-sharepoint-2010-list-item-attachments-using.html" rel="nofollow">http://viziblr.com/news/2011/11/5/batch-exporting-sharepoint-2010-list-item-attachments-using.html</a></p>

<p>C# Code</p>

<pre><code>    ---

    using System;
    using System.Linq;
    using MSACCESS = Microsoft.Office.Interop.Access;

    namespace ExportAccessAttachments3
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string fieldname_filename = "FileName";
                const string fieldname_filedata = "FileData";


                string outputfolder = @"D:\attachments";
                string dbfilename = @"D:\\AX6Reports.accdb";
                string tablename = "AX6Reports";
                var prefix_fieldnames = new[] { "Name", "Design" };
                string attachment_fieldname = "Attachments";

                var dbe = new MSACCESS.Dao.DBEngine();
                var db = dbe.OpenDatabase(dbfilename, false, false, "");
                var rstype = MSACCESS.Dao.RecordsetTypeEnum.dbOpenDynaset;
                var locktype = MSACCESS.Dao.LockTypeEnum.dbOptimistic;
                string selectclause = string.Format("SELECT * FROM {0}", tablename);
                var rs = db.OpenRecordset(selectclause, rstype, 0, locktype);
                rs.MoveFirst();
                int row_count = 0;

                while (!rs.EOF)
                {
                    var prefix_values = prefix_fieldnames.Select(s =&gt; rs.Fields[s].Value).ToArray();
                    var attachment_rs = (MSACCESS.Dao.Recordset2)rs.Fields[attachment_fieldname].Value;
                    int attachment_count = 0;
                    while (!attachment_rs.EOF)
                    {

                        var field_filename = attachment_rs.Fields[fieldname_filename].Value;

                        var field_attachment = (MSACCESS.Dao.Field2)attachment_rs.Fields[fieldname_filedata];
                        if (field_attachment != null)
                        {
                            if (field_attachment.Value != null)
                            {

                                string prefix = "";
                                if (prefix_fieldnames.Length &gt; 0)
                                {
                                    prefix = string.Format("{0}__", string.Join("__", prefix_values));
                                    prefix = prefix.Replace(" ", "_");
                                    prefix = prefix.Replace(":", "_");
                                    prefix = prefix.Replace("/", "_");
                                }

                                var dest_fname = System.IO.Path.Combine(outputfolder, prefix + field_filename);

                                if (System.IO.File.Exists(dest_fname))
                                {
                                    System.IO.File.Delete(dest_fname);
                                }

                                field_attachment.SaveToFile(dest_fname);
                            }
                        }

                        attachment_rs.MoveNext();
                        attachment_count++;
                    }
                    attachment_rs.Close();
                    Console.WriteLine(row_count);
                    row_count++;
                    rs.MoveNext();
                }

                rs.Close();
            }
        }
    }
</code></pre>

<p>Error</p>

<p>An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll</p>

<p>Additional information: Retrieving the COM class factory for component with CLSID {CD7791B9-43FD-42C5-AE42-8DD2811F0419} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).</p>