How to retrieve InfoPath form data in a SharePoint workflow

by S.Y.M. Wong-A-Ton

Learn how you can retrieve data that is stored within an InfoPath form either in a SharePoint Designer workflow or in a Visual Studio workflow that runs on that InfoPath form in a SharePoint form library.

Using and creating custom SharePoint workflows

While there are several ways to use and create workflows that run in SharePoint, you have two options when it comes to creating custom workflows for SharePoint:

  1. Create a SharePoint Designer workflow
  2. Create a Visual Studio workflow

Both types of workflows can run on InfoPath forms stored in SharePoint form libraries.

Retrieving InfoPath form data in a SharePoint Designer workflow

When you use SharePoint Designer to create custom workflows that run on InfoPath forms, your only option to retrieve form data from within the workflow is through promoted InfoPath form fields.

This means that when you publish an InfoPath form template to SharePoint, you have to promote the fields, that is, make them available as columns of the form library, so that the data that is stored in those fields are displayed in the columns when you save an InfoPath form in the form library.

A disadvantage of using this method is that InfoPath does not allow you to promote all types of fields. For example, when you promote a field from a repeating table, only the field in the first row of the repeating table will be displayed in the SharePoint form library column.

For an example of using this technique, see Automatically add a new item to a SharePoint list using InfoPath 2007 and a custom workflow

Update Feb 22, 2009:
For an example of using a SharePoint Designer workflow to set the value of a field in an InfoPath form, see Set the value in an InfoPath form from a SharePoint Designer workflow.

Retrieving InfoPath form data in a Visual Studio workflow

When you use Visual Studio to create custom workflows that run on InfoPath forms, you have total flexibility when retrieving form data from within the workflow.

Ordinarily you use

workflowProperties.Item

to retrieve the item within a SharePoint list or library that the workflow is running on. To access an InfoPath form that a workflow is running on, you simply have to use the File property of the item that the workflow is running on:

SPFile file = workflowProperties.Item.File;

Once you have an SPFile object, you can retrieve the binary data associated with this object:

byte[] xmlFormData = file.OpenBinary();

If you want to then access data that is stored within the InfoPath form, you can load the binary data into an XPathDocument object

XPathDocument ipForm = null;
using (MemoryStream ms = new MemoryStream(xmlFormData))
{
  ipForm = new XPathDocument(ms);
  ms.Close();
}

and then use XPath expressions to get the data out of the XPathDocument object

XPathNavigator ipFormNav = ipForm.CreateNavigator();
XPathNavigator nodeNav = ipFormNav.SelectSingleNode(
  "//my:field1", nsManager);

where nsManager is an XMLNamespaceManager object holding the XML namespaces for the InfoPath form.

For an example of using this technique, see How to use a Visual Studio workflow to submit data from an InfoPath form to a SharePoint list.

 Subscribe (RSS | Email)

Related Posts

Comments to this post were closed 30 days after it was published.

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. Usage of 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.

Working with InfoPath