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

简介: 原文:VSTO学习笔记(四)从SharePoint 2010中下载文件上一次我们开发了一个简单的64位COM加载项,虽然功能很简单,但是包括了开发一个64位COM加载项的大部分过程。本次我们来给COM加载项添加一些功能:从SharePoint 2010的文档库中下载一个Excel文档到本地。
原文: 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的实例:

 

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

 

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

img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
         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的路径语法:

img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
         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

img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
         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、按钮事件处理逻辑:

img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
         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与其他技术进行整合,构建一个完善的解决方案。

目录
相关文章
|
14天前
|
Web App开发 Windows
【Windows】 chrome 如何下载网站在线预览PDF文件,保存到本地
【Windows】 chrome 如何下载网站在线预览PDF文件,保存到本地
93 0
|
8月前
|
开发者 C++
Visual Studio常用快捷键(附带免费PDF)
Visual Studio常用快捷键(附带免费PDF)
101 0
|
数据可视化 Windows
Computer:Microsoft Office Visio2021的简介、安装、使用方法图文教程之详细攻略
Computer:Microsoft Office Visio2021的简介、安装、使用方法图文教程之详细攻略
Computer:Microsoft Office Visio2021的简介、安装、使用方法图文教程之详细攻略
|
前端开发 Python
Office 文件转 PDF 之服务实战
小编之前写了一篇关于 Office 文件转 PDF 的实战文章,详见Python 小技之 Office 文件转 PDF但是在平时的工作中,咱们需要通过接口的形式来调用具体的转换逻辑,同时开可以将文件转换写成服务的形式,将服务开启后传入参数或者地址即可直接调用逻辑转换,今天的文章主要讲解如何将文件转换写成服务; 一起拭目以待吧!!!
263 0
Office 文件转 PDF 之服务实战
Confluence 6 Office 和 PDF 文件
插入一个文件到页面中是能够让你将有用的文件,电子表格,幻灯片或者其他可用的文件在你小组中进行分享的好方法。 针对所有的文件类型,你可以选择以链接方式插入一个文件。缩略图将会对文档的内容进行预览同时可以对大小进行调整。
888 0
|
Java Linux Windows
java实现office文件预览
喜欢的朋友可以关注下,粉丝也缺。        不知觉就过了这个久了,继上篇java实现文件上传下载后,今天给大家分享一篇java实现的对office文件预览功能。
3606 0
|
存储 数据格式 XML
|
网络安全 数据安全/隐私保护 Windows