Custom SharePoint workflow to extract attachment from InfoPath form and upload it to a SharePoint document library

Applies to: InfoPath 2007

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 want to extract and upload a document that is stored as an InfoPath attachment to a SharePoint document library when the InfoPath form is submitted to or saved in a Form 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:

  1. In Microsoft Visual Studio 2005, from the File menu, choose New, and then Project.
  2. 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.
  3. Add a Code activity to the Sequential Workflow as shown in figure 1.
    Sequential workflow in Microsoft Visual Studio 2005
    Figure 1. Sequential workflow in Microsoft Visual Studio 2005.
  4. Add the following namespaces to the Workflow1.cs file:

    using System.Collections.Generic;
    using System.Xml.XPath;
    using System.IO;
    using System.Text;

  5. In the Workflow Designer, right-click onWorkflowActivated1 and select Generate Handlers from the context menu.
  6. Add the following C# code to the onWorkflowActivated1_Invoked event handler:

    workflowId = this.workflowProperties.WorkflowId;

  7. In the Workflow Designer, right-click codeActivity1 and select Generate Handlers from the context menu.
  8. 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
    in ipFormNav.GetNamespacesInScope(XmlNamespaceScope.All))
    if (ns.Key == String.Empty)
    nsManager.AddNamespace("def", ns.Value);
    nsManager.AddNamespace(ns.Key, ns.Value);

    // Retrieve the value of the attachment in the InfoPath form
    XPathNavigator nodeNav = ipFormNav.SelectSingleNode(
    "//my:attachment", nsManager);

    string ipFieldValue = string.Empty;
    if (nodeNav != null)
    ipFieldValue = nodeNav.Value;

    // Decode the InfoPath file attachment
    InfoPathAttachmentDecoder dec =
    new InfoPathAttachmentDecoder(ipFieldValue);
    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"];
    docLib.Files.Add(fileName, data);

    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.

  9. In the Solution Explorer, right-click the project, and select Properties.
  10. Click the Signing tab and sign the assembly with a strong name key file.
  11. Build the project and register the resulting DLL in the Global Assembly Cache (GAC).
  12. Open the feature.xml file and follow the instructions to add the appropriate XML snippet.
  13. Open the workflow.xml file and follow the instructions to add the appropriate XML snippet.
  14. Open the install.bat file and follow the instructions.
  15. Run the install.bat file to install and activate the feature for the workflow.
  16. In SharePoint, go to the Form Library on which you want the workflow to run, click Settings, and select Form Library Settings.
  17. On the Form Library Settings page, click Workflow settings.
  18. 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.


Related InfoPath Articles:


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.