VSTO学习笔记(四)从SharePoint 2010中下载文件

时间:2021-02-28 08:28:02

原文:VSTO学习笔记(四)从SharePoint 2010中下载文件

上一次我们开发了一个简单的64位COM加载项,虽然功能很简单,但是包括了开发一个64位COM加载项的大部分过程。本次我们来给COM加载项添加一些功能:从SharePoint 2010的文档库中下载一个Excel文档到本地。

示例代码下载

本系列所有示例代码均在 Visual Studio 2010 Ultimate RC + Office 2010 Professional Plus Beta x64 上测试通过。

1、首先创建一个Shared AddIn项目(具体细节请参阅上一篇文章):

VSTO学习笔记(四)从SharePoint 2010中下载文件

2、添加引用:

Microsoft.SharePoint

System.Windows.Forms

System.Drawing

System.DirectoryServices

3、在Connect类中创建Application和COMAddIn的实例:

VSTO学习笔记(四)从SharePoint 2010中下载文件代码
    /// <summary>
    ///   The object for implementing an Add-in.
    /// </summary>
    /// <seealso class='IDTExtensibility2' />
    [GuidAttribute("6D3788F4-9529-429E-BA5D-09695F85687A"), ProgId("SimpleExcelServicesDemo.Connect")]
    public class Connect : Object, Extensibility.IDTExtensibility2
    {
        private Microsoft.Office.Interop.Excel.Application app;
        private Microsoft.Office.Core.COMAddIn addIn;

3、在OnConnection事件里初始化:

VSTO学习笔记(四)从SharePoint 2010中下载文件代码
        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
        {
            this.app = application as Microsoft.Office.Interop.Excel.Application;
            this.addIn = addInInst as Microsoft.Office.Core.COMAddIn;
        }

4、在OnStartupComplete事件中设置一个按钮,关联事件处理逻辑:

VSTO学习笔记(四)从SharePoint 2010中下载文件代码
);
                simpleButton下载数据.Caption = "下载数据";
                simpleButton下载数据.Style = MsoButtonStyle.msoButtonCaption;
            }

            // Make sure the button is visible
            simpleButton下载数据.Visible = true;
            simpleButton下载数据.Click += new _CommandBarButtonEvents_ClickEventHandler(btnDownload_Click);

            standardBar下载数据 = null;
            commandBars = null;
        }

5、做一个域用户验证,当用户输入了合法的与用户名和密码后,才允许下载。这里添加了一个WindowsForm到项目中:

VSTO学习笔记(四)从SharePoint 2010中下载文件

6、域用户验证逻辑,我本机是一台域控制器*S.COM,使用的静态IP: 192.168.1.100,【LDAP://192.168.1.100/DC=*S,DC=com】是LDAP的路径语法:

VSTO学习笔记(四)从SharePoint 2010中下载文件代码
        private bool fn数据验证()
        {
            if (this.txt用户名.Text.Trim() == string.Empty)
            {
                MessageBox.Show("用户名不能为空!");
                this.txt用户名.Focus();
                return true;
            }

            if (this.txt密码.Text.Trim() == string.Empty)
            {
                MessageBox.Show("密码不能为空!");
                this.txt密码.Focus();
                return true;
            }

            if (this.fn域用户验证(@"LDAP://192.168.1.100/DC=*S,DC=com", this.txt用户名.Text.Trim(), this.txt密码.Text.Trim()))
            {
                MessageBox.Show("您输入的用户名或密码错误,请重新输入!");
                this.txt密码.Clear();
                this.txt密码.Focus();
                return true;
            }
            return false;
        }

        private bool fn域用户验证(string path, string username, string pwd)
        {
            try
            {
                DirectoryEntry entry = new DirectoryEntry(path, username, pwd);
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + username + ")";
                SearchResult result = search.FindOne();

                if (null == result)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return true;
            }
        }

7、使用Windows Server 2008 R2的AD管理器创建一个域用户:test

VSTO学习笔记(四)从SharePoint 2010中下载文件

VSTO学习笔记(四)从SharePoint 2010中下载文件

8、在Connect中编写下载文件逻辑:

SharePoint 2010 网站是:http://*spc/sites/doc,我们要下载的就是其Document库中的Excel Services Test.xlsx。

VSTO学习笔记(四)从SharePoint 2010中下载文件代码
, content.Length);
                fs.Flush();
                fs.Close();
            }
        }

9、按钮事件处理逻辑:

VSTO学习笔记(四)从SharePoint 2010中下载文件代码
        public void btnDownload_Click(CommandBarButton sender, ref bool cancelDefault)
        {
            FrmLogin login = new FrmLogin();
            if (login.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.fn下载文件();
            }
        }

10、编译一下,安装生成的setup.exe:

VSTO学习笔记(四)从SharePoint 2010中下载文件

11、打开Excel,点击【下载数据】:

VSTO学习笔记(四)从SharePoint 2010中下载文件

12、输入域用户名、密码后,点击【登录】,即把SharePoint中的文件下载到了本地,默认在C盘:

VSTO学习笔记(四)从SharePoint 2010中下载文件

VSTO学习笔记(四)从SharePoint 2010中下载文件

小结:

本次只是添加了一些功能,和SharePoint 2010进行了交互,下载了一个文档,其中用到了域用户的验证。后续篇章会继续将VSTO与其他技术进行整合,构建一个完善的解决方案。