How to: Access Association and Initiation Form Data in a Workflow in SharePoint Server 2010 (ECM)

Applies to: SharePoint Server 2010

When a workflow instance starts, any association and initiation data gathered from the user is stored in an SPWorkflowActivationProperties object, which you can access through the WorkflowProperties property of the OnWorkflowActivated activity. This object contains standard information for every workflow, as well as custom data from the workflow definition XML or from workflow association and initiation forms. By accessing the SPWorkflowActivationProperties object variable you specify for the WorkflowProperties property, you can use the information passed to the workflow.

The SPWorkflowActivationProperties object contains a standard set of properties for every workflow instance in SharePoint Foundation, including HistoryListId, ItemId, TaskListId, and WorkflowId.

In addition, the object also contains strings, represented by the AssociationData and InitiationData properties, which store custom properties for the workflow association and initiation, respectively.

For Microsoft Office InfoPath forms, these properties return XML strings that conform to the schema of the form used to gather the data. To access these custom properties, you must write code that parses the XML string.

The workflow developer can decide what method to use to parse the XML string and identify the custom properties it contains. For this procedure, we will use a Visual Studio command line tool, xsd.exe, to generate a class based on the schema of the workflow form; then we will store incoming form data into an object of that type by deserializing the XML string returned by the InitiationData property of the SPWorkflowActivationProperties object.

For more information about setting activity properties, see the Windows Workflow Foundation SDK.

To access association or initiation form data in your workflow

  1. Extract the schema of your InfoPath association or initiation form.

    1. In InfoPath, open your saved and published workflow form.

    2. To save the form as source files, on the File menu, click Save as Source File. Browse to the location to which you want to save the form source files, and then click OK.

      InfoPath saves a collection of form source files, including the schema file, to the specified location. The form schema file is always named myschema.xsd.

  2. Use the command line tool, xsd.exe to generate a new class file, based on the form schema file (.xsd).

    1. Open a Visual Studio command prompt. Click Start, point to All Programs, point to Microsoft Visual Studio 2010, point to Visual Studio Tools, and then click Visual Studio 2010 Command Prompt.


      By default, Visual Studio 2010 installs the xsd.exe command line tool to the following location, where C: represents the drive on which you have installed Visual Studio 2010: C:\Program Files\Microsoft Visual Studio 10\SDK\v2.0\Bin

    2. Navigate to the location of the form schema (.xsd) file, and then run the following command: xsd myschema.xsd /c /o:output_directory

      The xsd.exe tool generates a new class file based on the form schema. The file is named the same as the schema file, myschema.cs. The class in the file is named the same as the root element of the schema, which was named the same as the form fields collection.


      Specifying a unique name for the form fields collection, rather than using the default name of myfields, helps to ensure that the class generated from the form schema file also has a unique name. This is especially important when you are programming a workflow that uses multiple forms.

  3. In Visual Studio, open your workflow project, and add the new class file to it.

  4. Add code to your workflow that serializes a new instance of the new class, by using the workflow association or initiation data.

    For example, the following code serializes a new object, of type InitForm, from the InitiationData property of a SPWorkflowActivationProperties object variable named workflowProps. This example assumes the developer has created a class, InitForms, whose schema matches the schema of the InfoPath form used to gather the initiation data.

    using System.Xml.Serialization;
    using System.Xml;
      XmlSerializer serializer = new 
       XmlTextReader reader = new XmlTextReader(new 
       InitForm initform = (InitForm) serializer.Deserialize(reader);
    Imports System.Xml.Serialization
    Imports System.Xml
      Dim serializer As New XmlSerializer(GetType(InitForm))
      Dim reader As New XmlTextReader(New System.IO.StringReader(workflowProps.InitiationData))
      Dim initform As InitForm = CType(serializer.Deserialize(reader), InitForm)
  5. Add code to your workflow that accesses the custom properties as properties of the class, based on the form schema.

    The following code builds on the previous example. The code accesses three custom properties of the InitForm object and assigns them to string variables.

      assignee = initform.assignee;
      instructions = initform.instructions;
      comments = initform.comments;
    assignee = initform.assignee
    instructions = initform.instructions
    comments = initform.comments

See Also


How to: Design InfoPath Workflow Forms (ECM)

How to: Design a Workflow Form to Use Association and Initiation Data in SharePoint Server 2010 (ECM)


InfoPath Forms for Workflows (ECM)

Workflow Association and Initialization Forms in SharePoint Server 2010 (ECM)