C#从Access导出SharePoint列表附件

时间:2016-01-18 21:20:23

标签: c# visual-studio ms-access sharepoint batch-processing

我有一项任务是导出SharePoint 2007列表中的所有附件。该清单有大约5,000件物品。我想在一个大批量中导出这些项目。通过我迄今为止所做的研究,我只找到了一个符合我要求的解决方案。以下是我所指的网页链接。我在第21行用MSACCESS.Doa.DBEngine()收到错误。我是C#的新手,所以我不确定为什么我会收到这个错误。我在VS项目中引用了Access。我正在使用VS Enterprise。我该怎么做才能解决这个问题?谢谢您的帮助。

来源 http://viziblr.com/news/2011/11/5/batch-exporting-sharepoint-2010-list-item-attachments-using.html

C#代码

    ---

    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 => 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 > 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();
            }
        }
    }

错误

mscorlib.dll中出现未处理的“System.Runtime.InteropServices.COMException”类型异常

附加信息:由于以下错误,检索具有CLSID {CD7791B9-43FD-42C5-AE42-8DD2811F0419}的组件的COM类工厂失败:80040154未注册类(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG))。

0 个答案:

没有答案