Custom SharePoint workflow to extract attachment from InfoPath form and upload it to a SharePoint document library
Learn how you can use a SharePoint workflow on a SharePoint Form Library to extract data from an Attachment on an InfoPath form and use this data to add a file to a SharePoint Document Library.
You could use either an ItemAdded SharePoint event handler or a custom SharePoint workflow created in Visual Studio to extract and upload a document stored as a file attachment in an InfoPath form to a SharePoint document library.
This article discusses the second method.
This article assumes that you have created an InfoPath form template that has a File Attachment control named attachment, and published the InfoPath form template to a SharePoint Form Library.
This article also assumes that you have created a Document Library named Form Attachments in SharePoint and that you know how to work with and program in Microsoft Visual Studio.
You can create the custom SharePoint workflow in Visual Studio as follows:
- In Microsoft Visual Studio 2005, from the File menu, choose New, and then Project.
- On the New Project dialog box, click the SharePoint project type, select the Sequential Workflow Library template if you're using WSS or the SharePoint Server Sequential Workflow Library template if you're using MOSS, name the project ExtractAttachmentInfoPathForm (or whatever you like), and click OK.
- Add a Code activity to the Sequential Workflow as shown in figure 1.
Figure 1. Sequential workflow in Microsoft Visual Studio 2005.
Add the following namespaces to the Workflow1.cs file:
- In the Workflow Designer, right-click onWorkflowActivated1 and select Generate Handlers from the context menu.
Add the following C# code to the onWorkflowActivated1_Invoked event handler:
workflowId = this.workflowProperties.WorkflowId;
- In the Workflow Designer, right-click codeActivity1 and select Generate Handlers from the context menu.
Add the following C# code to the codeActivity1_ExecuteCode event handler:
// Retrieve the file associated with the item
// on which the workflow has been instantiated
SPFile file = workflowProperties.Item.File;
if (file == null)
// Get the binary data of the file
byte xmlFormData = null;
xmlFormData = file.OpenBinary();
// Load the data into an XPathDocument object
XPathDocument ipForm = null;
if (xmlFormData != null)
using (MemoryStream ms = new MemoryStream(xmlFormData))
ipForm = new XPathDocument(ms);
if (ipForm == null)
// Create an XPathNavigator object to navigate the XML
XPathNavigator ipFormNav = ipForm.CreateNavigator();
XmlNamespaceManager nsManager =
new XmlNamespaceManager(new NameTable());
foreach (KeyValuePair<string, string> ns
if (ns.Key == String.Empty)
// Retrieve the value of the attachment in the InfoPath form
XPathNavigator nodeNav = ipFormNav.SelectSingleNode(
string ipFieldValue = string.Empty;
if (nodeNav != null)
ipFieldValue = nodeNav.Value;
// Decode the InfoPath file attachment
InfoPathAttachmentDecoder dec =
string fileName = dec.Filename;
byte data = dec.DecodedAttachment;
// Add the file to a document library
using (SPWeb web = workflowProperties.Web)
SPFolder docLib = web.Folders["Form Attachments"];
Note: This code makes use of the InfoPathAttachmentDecoder class described in the encoding decoding article How to encode and decode a file attachment programmatically by using Visual C# in InfoPath.
- In the Solution Explorer, right-click the project, and select Properties.
- Click the Signing tab and sign the assembly with a strong name key file.
- Build the project and register the resulting DLL in the Global Assembly Cache (GAC).
- Open the feature.xml file and follow the instructions to add the appropriate XML snippet.
- Open the workflow.xml file and follow the instructions to add the appropriate XML snippet.
- Open the install.bat file and follow the instructions.
- Run the install.bat file to install and activate the feature for the workflow.
- In SharePoint, go to the Form Library on which you want the workflow to run, click Settings, and select Form Library Settings.
- On the Form Library Settings page, click Workflow settings.
- On the Add a Workflow page, select your workflow from the workflow template list box, enter a name for your SharePoint workflow, select the Start this workflow when a new item is created check box, and click OK.
Now whenever you fill out a new InfoPath form, attach a document or file to it, and submit it to the SharePoint form library, the SharePoint workflow on the InfoPath form is started, and the document or file you attached to the InfoPath form is extracted and uploaded to the Form Attachments SharePoint document library.
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. 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, S.Y.M. Wong-A-Ton.
Related InfoPath Articles:
- Top 10 questions about InfoPath attachments
- How to programmatically rename an InfoPath file attachment
- How to submit a file attached to an InfoPath form as an attachment to a SharePoint list item
- How to submit an InfoPath form to a SharePoint list and attach the form to the list item
- Get data from an Excel workbook attached to an InfoPath form