infopath上传attachment

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://zhonglei.blog.51cto.com/1159374/268587
 
by S.Y.M. Wong-A-Ton
 
本文指导我们如何使用代码从InfoPath表单中将附件抽取并且上载到SharePoint中。
 
如下代码,可以把InfoPath中名为 Document的附件控件中的文件,上载到SharePoint中名为 MyDocuments的文档库中。
 
具体操作是在表单上添加一个按钮,然后在按钮上添加VSTA代码,添加下面的代码。
 
在整个程序中,使用了 InfoPathAttachmentDecoder 类,这个类的编写方法请参照微软的官方文章 http://support.microsoft.com/kb/892730
 
// Retrieve the value of the attachment in the InfoPath form
XPathNavigator ipFormNav = MainDataSource.CreateNavigator();
XPathNavigator nodeNav = ipFormNav.SelectSingleNode(
"//my:document", NamespaceManager);

string attachmentValue = string.Empty;
if (nodeNav != null && !String.IsNullOrEmpty(nodeNav.Value))
{
attachmentValue = nodeNav.Value;

// Decode the InfoPath file attachment
InfoPathAttachmentDecoder dec =
new InfoPathAttachmentDecoder(attachmentValue);
string fileName = dec.Filename;
byte[] data = dec.DecodedAttachment;

// Add the file to a document library
using (SPSite site = new SPSite("http://ServerName"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPFolder docLib = web.Folders["MyDocuments"];
docLib.Files.Add(fileName, data);
web.AllowUnsafeUpdates = false;
web.Close();
}
site.Close();
}
}
 
需要注意的是,你需要在VSTA工程中引用SharePoint dll并且添加对Microsoft.SharePoint名字空间的引用。
然后给InfoPath表单完全的安全信任,以便代码可以访问SharePoint资源。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值