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的实例:
/// 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事件里初始化:
{
this.app = application as Microsoft.Office.Interop.Excel.Application;
this.addIn = addInInst as Microsoft.Office.Core.COMAddIn;
}
4、在OnStartupComplete事件中设置一个按钮,关联事件处理逻辑:
{
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的路径语法:
{
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。
{
//保存文件
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、按钮事件处理逻辑:
{
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与其他技术进行整合,构建一个完善的解决方案。
VSTO学习笔记(四)从SharePoint 2010中下载文件相关推荐
- HTML5(李炎恢)学习笔记四 ------------- HTML5元素(中)
一.表格元素 HTML5中表格元素的用法.表格的主要用途是以网格的形式显示二维数 1.表格元素总汇 表格的基本构成最少需要三个元素:<table>.<tr>.<td> ...
- 【VS开发】VSTO 学习笔记(十)Office 2010 Ribbon开发
微软的Office系列办公套件从Office 2007开始首次引入了Ribbon导航菜单模式,其将一系列相关的功能集成在一个个Ribbon中,便于集中管理.操作.这种Ribbon是高度可定制的,用户可 ...
- Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(显示数据 二)...
在Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(显示数据一)中,解释了如何把Crystal Report整合到Sharepoint ...
- ROS中的roslaunch命令和launch文件(ROS入门学习笔记四)
ROS中的基本对象和概念学习笔记(ROS入门学习笔记一) ROS中创建工作区和包(ROS入门学习笔记二) ROS功能包中CMakeLists.txt的说明(ROS入门学习笔记三) 1.roslaunc ...
- VSTO学习笔记(二)Excel对象模型
原文:VSTO学习笔记(二)Excel对象模型 上一次主要学习了VSTO的发展历史及其历代版本的新特性,概述了VSTO对开发人员的帮助和效率提升.从这次开始,将从VSTO 4.0开始,逐一探讨VSTO ...
- Sharepoin学习笔记—架构系列—Sharepoint服务(Services)与服务应用程序框架(Service Application Framework) 1
Sharepoin学习笔记-架构系列-Sharepoint服务(Services)与服务应用程序框架(Service Application Framework) 1 Sharepoint服务是Sha ...
- VSTO 学习笔记(十二)自定义公式与Ribbon
原文:VSTO 学习笔记(十二)自定义公式与Ribbon 这几天工作中在开发一个Excel插件,包含自定义公式,根据条件从数据库中查询结果.这次我们来做一个简单的测试,达到类似的目的. 即在Excel ...
- JavaScript-WebGL2学习笔记四-蒙板
stencil test(蒙板) demo的显示效果 这个例子由四个源文件构成 webgl.html <html> <head><!--Title: JavaScript ...
- SharePoint 2010中的客户端AJAX应用——ASP.NET AJAX模板
WCF Data Services是SharePoint 2010中一个极具吸引力的新特性.然而,因为它的强大,直接对其进行编程仍然会有点痛苦.幸运的是,一个新的相关技术 -- ASP.Net AJA ...
最新文章
- [BZOJ 2734] 集合选数
- Java四种线程池的使用
- 极速pdf编辑器的水印如何去掉_如何修改PDF?有没有详细的PDF编辑器操作方法?...
- SpringCloud微服务注册调用入门-路由网关
- 设计模式-观察者模式 发布/订阅模式
- 全志t3linux驱动_全志A20GPIO驱动分析|Android驱动及系统开发交流区|研发交流|雨滴科技技术论坛 - Powered by Discuz!...
- 那年学过的web后端笔记
- php扫描目录字典,Python如何实现敏感目录扫描 Python实现敏感目录扫描代码示例...
- 启动后显示不了数据_90后都买不起房?统计数据显示:90后成了城市租房主力!...
- 生成器 Generators
- SSM第一篇 最简单的SSM框架搭建过程--SSM简单整合
- 台式机dp接口_常见视频接口图示及说明
- sort排序不区分大小写C语言,使用sort进行不区分大小写的排序(list,key = str.lower)...
- MAVEN工具篇——maven打包跳过测试
- 熔断漏洞和幽灵漏洞_崩溃和幽灵:发现新的英特尔CPU漏洞
- 解决 windows 下浏览器无法上网,QQ/微信正常上网
- 【Linux】如何查找命令及历史记录history
- Python查找算法之狄克斯特拉算法
- 如何连接局域网_手机画面如何投屏到电视、盒子、投影仪?
- html表格边框怎么消除,如何去除table的粗边框