How to create an InfoPath initiation or association form for a SharePoint workflow created in Visual Studio

Learn which steps are required to create an InfoPath form that can be used as an initiation or association form in a SharePoint workflow that is created using Visual Studio.

ADVERTISEMENTS

Introduction

A SharePoint workflow association form is displayed when you associate a workflow to a SharePoint list or library. A workflow initiation form is displayed when a user manually starts a workflow, but before the workflow runs.

Both a SharePoint workflow association form and a workflow initiation form are used to supply additional information that a workflow requires before it can be started.

This article describes how you can create an InfoPath form that can be used as a SharePoint workflow initiation form to provide the workflow with information before it runs.

Scenario

Documents are stored in a SharePoint document library. You want to be able to select any document in this document library, start a workflow on the selected document, and enter the email address of a user to whom you want to send a notification email when you run the SharePoint workflow.

For this you'll create an InfoPath form template that can be used as an initiation or association form in a SharePoint workflow.

Note: If you're only interested in the functionality of this article, that is, to start a workflow, specify an email address and send an email, you can also use a SharePoint Designer workflow that allows users to send an email. There is no need to write code to get this functionality. This article is only meant to demonstrate the creation of workflow forms using InfoPath.

Steps to create an InfoPath initiation or association form

InfoPath forms that are used as SharePoint workflow forms run in the browser, so you must have InfoPath Forms Services installed and running on your SharePoint server if you want to use InfoPath forms as SharePoint workflow forms.

To use an InfoPath form as a SharePoint workflow form, you must:

  1. Create a browser-compatible form template in InfoPath.
  2. Set the Domain security level on the InfoPath form template.
  3. Submit the InfoPath form to a hosting environment.
  4. Publish the InfoPath form template to a network location and empty the public URL or full network path to the form template in the Publishing Wizard.
  5. Create a class from the schema definition of the InfoPath form to use for deserialization in the workflow.
  6. Use the Form ID of the InfoPath form template as the URN for the InfoPath form in the workflow.xml file of the Feature for the SharePoint workflow.

Create the InfoPath form template

To create an InfoPath form that can be used as an initiation or association form in a SharePoint Visual Studio workflow:

  1. In InfoPath, create a new Blank browser-compatible form template.
  2. Add a Text Box named email and Button control with the label Send Email to the form template.
  3. In the Data Source task pane, rename the myFields root node of the InfoPath form template to emailAssociation.
  4. On the Tools menu, click Form Options.
  5. On the Form Options dialog box, select the Security and Trust category, deselect Automatically determine security level (recommended), select Domain (the form can access content from the domain in which it is located), and click OK.
  6. On the Tools menu, click Data Connections.
  7. On the Data Connections dialog box, click Add, create a Submit data connection To the hosting environment, such as an ASP.NET page or a hosting application, and name the data connection Submit.
  8. Add a Rule with the following 2 actions to the Button control:

    Submit using a data connection: Submit

    and

    Close this form: No Prompt

  9. On the File menu, click Publish.
  10. On the Publishing Wizard, select To a network location, and click Next.
  11. On the Publishing Wizard, click Browse to browse to and select the location where to publish the InfoPath form template.
  12. On the Publishing Wizard, enter a name for the form template (IPAssocForm in this example), and click Next.
  13. On the Publishing Wizard, delete the public URL or network path listed in the text box, and click Next. Click OK to accept the warning that appears.
  14. On the Publishing Wizard, click Publish.
  15. On the Publishing Wizard, click Close.

Create the SharePoint workflow in Visual Studio

You must know how to create a Visual Studio workflow before proceeding.

To create the Visual Studio workflow that makes use of the InfoPath form as an initiation form:

  1. Create a SharePoint Sequential Workflow that has a SendEmail activity as shown in Figure 1.

    The SharePoint Sequential Workflow with a SendEmail activity shown in the workflow designer in Visual Studio.
    Figure 1. The SharePoint Sequential Workflow with a SendEmail activity shown in the workflow designer in Visual Studio.

  2. Use the XML Schema Definition Tool to generate a class for the InfoPath form template, and add the class to the Visual Studio workflow project.
  3. Add a private member variable named emailAddress to the class:

    private string emailAddress = string.Empty;

  4. Generate an event handler for the OnWorkflowActivated activity and add the following code to the event handler:

    // Set the ID for the workflow
    workflowId = workflowProperties.WorkflowId;

    // Deserialize InfoPath initiation form data
    string initData = workflowProperties.InitiationData;

    XmlSerializer xser = new XmlSerializer(typeof(emailAssociation));
    XmlTextReader reader = new XmlTextReader(
      new System.IO.StringReader(initData));
    emailAssociation ipInitForm =
      (emailAssociation)xser.Deserialize(reader);

    // Store the email address in a variable
    emailAddress = ipInitForm.email;

  5. Generate an event handler for the SendEmail activity and add the following code to the event handler:

    if (!String.IsNullOrEmpty(emailAddress))
    {
      sendEmail1.To = emailAddress;
      sendEmail1.Subject = "Document review required";
      sendEmail1.Body = "Please review this document: "
        + workflowProperties.Item.Url;
    }

    where sendEmail1 is the name of the SendEmail activity.

    Note: You must fill in the To and From properties of the SendEmail activity with valid (default) email addresses, and also set the CorrelationToken property to workflowToken, otherwise the activity (and workflow) will fail to run.

  6. Copy the InfoPath form template from the location to where you published it and paste the form template in the same location as where the feature.xml and workflow.xml files are located (alternatively, you can open InfoPath and re-publish the InfoPath form template to the location of the workflow deployment files), and deploy the workflow.

    Note: You'll have to add an ElementFile element to the ElementManifests node in the feature.xml file. This ElementFile must point to the XSN of the InfoPath initiation form.

    <ElementFile Location="IPAssocForm.xsn" />

    In the code snippet above (taken from the feature.xml file), IPAssocForm.xsn is the InfoPath initiation form template that can also be used as an InfoPath association form template.

    You'll also have to add a Property element to the Properties node in the feature.xml file to register the InfoPath form.

    <Property Key="RegisterForms" Value="*.xsn" />

    In addition, you'll have to enter the Form ID of the InfoPath form template in the Instantiation_FormURN element of the MetaData node in the workflow.xml file.

    <MetaData>
      <Instantiation_FormURN>
        urn:schemas-microsoft-com:office:infopath:IPAssocForm:-myXSD-2009-01-13T21-00-55
      </Instantiation_FormURN>
    </MetaData>

    And if you also want to use the InfoPath form to enter an email address when you attach the workflow to a SharePoint library, you must add an Association_FormURN element with the same Form ID as the Instantiation_FormURN element to the MetaData node.

    To find the Form ID of the InfoPath form template:

    1. Open the InfoPath form template in Design mode.
    2. On the File menu, click Properties.
    3. On the Form Template Properties dialog box, copy the URN displayed in the ID text box.

Test the SharePoint workflow

Before you can test the SharePoint workflow, you must attach the workflow to a document library in SharePoint and allow the workflow to be manually started.

Once you've attached the workflow to a document library, you can click on a document in that document library, and select Workflows from the drop-down menu to manually start a workflow.

Accessing the workflows that can be manually started on a document in a SharePoint library
Figure 2. Accessing the workflows that can be manually started on a document in a SharePoint library.

On the Workflows page, you can then start a new instance of the workflow to run on the document. When you click on the name of your workflow, you will be presented with the InfoPath initiation form.

The InfoPath initiation form to fill in and pass an email address to the SharePoint workflow
Figure 3. The InfoPath initiation form to fill in and pass an email address to the SharePoint workflow.

When you click the Send Email button, the SharePoint workflow will start to run and use the email address you supplied to send an email to the recipient.

 
 Subscribe for updates via RSS or Email

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. 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, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services