How to use a Visual Studio workflow to submit data from an InfoPath form to a SharePoint list

Applies to: InfoPath 2007

This article explains how you can use a custom Visual Studio sequential workflow on a Form Library to extract data from an InfoPath form that was submitted to the form library and use this data to add a new item to a custom SharePoint list.

ADVERTISEMENTS

Problem

You want to submit the data that is entered into an InfoPath form to a SharePoint list, but there is no out-of-the-box way to submit an InfoPath form to a SharePoint list.

Solution

You could use one of the following 5 methods:

  1. Use the SharePoint Lists web service together with a CAML batch update to add an item to a SharePoint list.
  2. Use a SharePoint Designer workflow to get data from the InfoPath form and create a SharePoint list item.
  3. When using an InfoPath browser form, call directly into the SharePoint object model from within the InfoPath form to add an item to the SharePoint list.
  4. Use a SharePoint event handler feature to make calls directly into the SharePoint object model to add an item to the SharePoint list when an InfoPath form is added to a Form Library on which the event handler has been activated.
  5. Use a custom Visual Studio workflow to add an item to the SharePoint list when an InfoPath form is added to a Form Library on which the workflow has been set to run.

This article discusses method 5.

Discussion

This article assumes that you have created an InfoPath form template that has a text field named field1, and published the InfoPath form template to a SharePoint Form Library.

This article also assumes that you have created a Custom List named Fruits in SharePoint and that you know how to work with and program in Microsoft Visual Studio.

You can create the custom Visual Studio workflow 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 MySharePointWorkflow (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;

  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)
    return;

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

    if (ipForm == null)
    return;

    // Create an XPathNavigator object to navigate the XML
    XPathNavigator ipFormNav = ipForm.CreateNavigator();

    ipFormNav.MoveToFollowing(XPathNodeType.Element);
    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);
    }
    else
    {
    nsManager.AddNamespace(ns.Key, ns.Value);
    }
    }

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

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

    // Add an item to a list and use the value of the field
    // as the title for the new item
    if (!String.IsNullOrEmpty(ipFieldValue))
    {
    using (SPSite site = new SPSite(workflowProperties.SiteId))
    {
    using (SPWeb web = site.OpenWeb())
    {
    SPList list = web.GetList("/Lists/Fruits");

    if (list != null)
    {
    SPListItem item = list.Items.Add();
    item["Title"] = ipFieldValue;
    item.Update();
    }
    web.Close();
    }
    site.Close();
    }
    }
    }

  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. The final feature.xml could look something like the following:

    <Feature Id="68CBE2C1-3F21-42e8-B6F8-6C92AF22F69E"
    Title="MySharePointWorkflow"
    Description="This feature is a workflow that adds an item to the Fruits list."
    Version="12.0.0.0"
    Scope="Site"
    xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
    <ElementManifest Location="workflow.xml" />
    </ElementManifests>
    <Properties>
    <Property Key="GloballyAvailable" Value="true" />
    </Properties>
    </Feature>

  13. Open the workflow.xml file and follow the instructions to add the appropriate XML snippet. The final workflow.xml could look something like the following:

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Workflow
    Name="MySharePointWorkflow"
    Description="This workflow that adds an item to the Fruits list."
    Id="055B6B63-FA5A-4795-AE90-616C83B4C32C"
    CodeBesideClass="MySharePointWorkflow.Workflow1"
    CodeBesideAssembly="MySharePointWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddc6e14c3d8398bb"
    >
    <Categories/>
    <MetaData />
    </Workflow>
    </Elements>

    Note: You can find the value for the PublicKeyToken on the Properties dialog box of the DLL you deployed to the GAC, which is located at C:\Windows\assembly.

  14. Open the install.bat file and follow the instructions. You only need to replace a few values in this file.

  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 MySharePointWorkflow (or whatever you called your workflow) from the workflow template list box, enter a name for your 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 and submit it to the form library, the workflow on the form library is started, and a new item is added to the Fruits list.

 


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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services