How to set the value of an InfoPath form field through a SharePoint workflow

Applies to: InfoPath 2007

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:

  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;
    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
    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
    in ipFormNav.GetNamespacesInScope(XmlNamespaceScope.All))
      if (ns.Key == String.Empty)
        nsManager.AddNamespace("def", ns.Value);
        nsManager.AddNamespace(ns.Key, ns.Value);

    // 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
  9. Deploy the workflow to SharePoint.
  10. In SharePoint, go to the Form Library on which you want the workflow to run, click Settings, and select Form Library Settings.
  11. On the Form Library Settings page, click Workflow settings.
  12. 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:


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.