【翻译】如何从InfoPath向SharePoint上载文档

原文: http://www.bizsupportonline.net/blog/2010/01/upload-document-sharepoint-infopath-form/
 
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资源。

你可能感兴趣的:(职场,SharePoint,休闲,infopath)