How to use a Visual Studio workflow to submit data from an InfoPath form to a SharePoint list
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.
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.
You could use one of the following 5 methods:
- Use the SharePoint Lists web service together with a CAML batch update to add an item to a SharePoint list.
- Use a SharePoint Designer workflow to get data from the InfoPath form and create a SharePoint list item.
- 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.
- 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.
- 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.
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:
- 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 MySharePointWorkflow (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 field in the InfoPath form
XPathNavigator nodeNav = ipFormNav.SelectSingleNode(
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
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;
- 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. The final feature.xml could look something like the following:
Description="This feature is a workflow that adds an item to the Fruits list."
<ElementManifest Location="workflow.xml" />
<Property Key="GloballyAvailable" Value="true" />
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:
Description="This workflow that adds an item to the Fruits list."
CodeBesideAssembly="MySharePointWorkflow, Version=188.8.131.52, Culture=neutral, PublicKeyToken=ddc6e14c3d8398bb"
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.
Open the install.bat file and follow the instructions. You only need to replace a few values in this file.
- 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 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:
- 5 Ways to submit an InfoPath form to a SharePoint list
- Automatically add a new item to a SharePoint list using InfoPath 2007 and a custom workflow
- How to submit the rows of a repeating table in InfoPath to a SharePoint list
- How to use the SharePoint object model to submit data from an InfoPath form to a SharePoint list
- SharePoint Designer 2013 workflow to loop through InfoPath repeating table and create list items
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.