close
文章出處

上一次我們開發了一個簡單的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與其他技術進行整合,構建一個完善的解決方案。


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    AutoPoster 發表在 痞客邦 留言(0) 人氣()