How to set the value of an InfoPath form field through a SharePoint workflow
Learn how you can write code in a custom Visual Studio workflow for SharePoint to set the value of a field that is stored within an InfoPath form.
You have an InfoPath form stored in a SharePoint Form Library. You want to run a workflow on this InfoPath form and let the workflow set the value of a field that is stored within the InfoPath form.
Use the SPFile object of the SPListItem object that the workflow is running on to access the file that represents the XML of the InfoPath form. Load the XML into an XmlDocument object for read/write access. Set the value of the field by modifying the contents of the XmlDocument object and then replace the original XML of the SPFile object with the modified XML of the XmlDocument object.
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.
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
XmlDocument ipForm = null;
if (xmlFormData != null)
using (MemoryStream ms = new MemoryStream(xmlFormData))
ipForm = new XmlDocument();
if (ipForm == null)
// Create an XPathNavigator object to navigate the XML
XPathNavigator ipFormNav = ipForm.CreateNavigator();
// Retrieve the namespaces used in the form
XmlNamespaceManager nsManager =
new XmlNamespaceManager(new NameTable());
foreach (KeyValuePair<string, string> ns
if (ns.Key == String.Empty)
// Change the value of the InfoPath form field
nsManager).InnerText = "Set by the workflow";
// Convert the XML document to bytes
xmlFormData = Encoding.UTF8.GetBytes(ipForm.OuterXml);
// Save the bytes of the XML document as the contents
// of the SPFile object that represents the InfoPath form
// Save the changes made to the SPFile object
- Deploy the workflow to SharePoint.
- 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, and click OK.
Now whenever you manually start the workflow on an InfoPath form in the SharePoint Form Library, the value of the form field will be updated by the workflow.
Related InfoPath Articles:
- How to retrieve InfoPath form data in a SharePoint workflow
- How to use a Visual Studio workflow to submit data from an InfoPath form to a SharePoint list
- Create a SharePoint Designer workflow that sends an email with a link to an InfoPath browser form
- Create a SharePoint Designer workflow for InfoPath forms
- Run a SharePoint 3-state workflow on an InfoPath form
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.