How to upload a document to SharePoint from an InfoPath form

by S.Y.M. Wong-A-Ton

Learn how you can write code in the FormCode.cs file of an InfoPath form template to be able to extract and upload a document attached to the InfoPath form to a SharePoint document library.

In a previous article about uploading documents from InfoPath through a SharePoint workflow to a SharePoint Document Library, I wrote code to upload a document from InfoPath to SharePoint.

Since many visitors to BizSupportOnline were having difficulties finding this code and extracting the bits they needed, I’ve decided to isolate the code and write a separate article for it.

The solution described here makes use of an InfoPath form that has a File Attachment control named document and a Button control on it.

Furthermore, it makes use of the InfoPathAttachmentDecoder class described in How to encode and decode a file attachment programmatically by using Visual C# in InfoPath.

To upload a document that has been stored in an attachment field on an InfoPath form to a SharePoint document library named MyDocuments, you can add the following code behind the button control:

// 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();
}
}

Note: You must add a reference to the SharePoint DLL and a using statement for the Microsoft.SharePoint namespace and give the InfoPath form template Full Trust for the code to work.

If you want to upload a document to a SharePoint document library that is located on the same SharePoint site as an InfoPath browser form, you can use one of the two methods described in 2 Ways to retrieve the SharePoint site collection URL of an InfoPath browser form to find the site and get a reference to the SharePoint document library.

 Subscribe (RSS | Email)

Related Posts

Comments to this post were closed 30 days after it was published.

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. Usage of techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

Working with InfoPath