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项目(具体细节请参阅上一篇文章):

 

2、添加引用:

Microsoft.SharePoint

System.Windows.Forms

System.Drawing

System.DirectoryServices

 

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

 

代码
    /// <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事件里初始化:

 

代码
        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事件中设置一个按钮,关联事件处理逻辑: 

代码
        public void OnStartupComplete(ref System.Array custom)
        {
            CommandBars commandBars;
            CommandBar standardBar下载数据;
            CommandBarButton simpleButton下载数据;
            commandBars 
= this.app.CommandBars;

            
// Get the standard CommandBar from Word
            standardBar下载数据 = commandBars["Standard"];

            
try
            {
                
// try to reuse the button is hasn't already been deleted
                simpleButton下载数据 = (CommandBarButton)standardBar下载数据.Controls["下载数据"];
            }
            
catch (System.Exception)
            {
                
// If it's not there add a new button
                simpleButton下载数据 = (CommandBarButton)standardBar下载数据.Controls.Add(1);
                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到项目中:

 

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

代码
        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=BROOKS,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

 

 

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

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

代码
        private void fn下载文件()
        {
            
//保存文件            
            using (SPSite site = new SPSite("http://brookspc/sites/doc"))
            {
                SPWeb web 
= site.OpenWeb();
                
string __fileName = "http://brookspc/sites/doc/Documents/Excel Services Test.xlsx";
                SPFile file 
= web.GetFile(__fileName);
                
string __localFilePath = @"C:\ExcelServices.xlsx";
                
//将文件下载到本地
                byte[] content = file.OpenBinary();
                
if (File.Exists(__localFilePath))
                {
                    File.Delete(__localFilePath);
                }
                FileStream fs 
= new FileStream(__localFilePath, FileMode.Create);
                fs.Write(content, 
0, content.Length);
                fs.Flush();
                fs.Close();
            }
        }

 

 

 9、按钮事件处理逻辑:

代码
        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:

 

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

 

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

 

 

小结:

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

posted @ 2010-03-08 23:46  徐州瑞步科技  阅读(3525)  评论(8编辑  收藏  举报