How to upload a file to SharePoint from an InfoPath form

Learn how you can write code in the FormCode.cs file of an InfoPath form template to be able to extract and from within InfoPath upload a file or 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 from within InfoPath upload a file 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 InfoPath attachment field on 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;

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 from within InfoPath 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.

If you want to upload an image to a SharePoint picture library, you do not have to write code, but can use the SharePoint Imaging web service to upload an image from InfoPath to a picture library.

Also See

If you are using InfoPath 2010 with SharePoint 2010 and want to avoid giving the InfoPath form template Full Trust and having to digitally sign it, you can make use of recipe 98 of InfoPath 2010 Cookbook 3 (for C# code) or InfoPath 2010 Cookbook 4 (for VB code) to upload files from a file attachment control or an image stored in a picture control to a document or picture library in SharePoint 2010.


Related Posts


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. The 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.