Skip to main content

Cloud

Creating a MOSS State Machine Workflow (Part 2 of 4: Creating InfoPath Forms)

“InfoPath is my friend. InfoPath is my friend. InfoPath is my friend.”
I had to chant that to myself a few times before I started to believe it. Coming from a custom development background, my initial inclination with a web form is to simply bang out a quick ASPX page. But I’m starting to appreciate what InfoPath brings to the table – though I still have to chant occasionally.
In my last post, I outlined a hypothetical business case for a state machine workflow. If you haven’t read that one yet, start there.
This post will walk through the steps of creating the four InfoPath 2007 forms for use in the workflow.
The first form I’m going to create is the association form that will be displayed when my workflow is associated with a list.
For simplicity, I’ve decided that the only thing I need from the association form is the identity of the user who will be responsible for approving proposals. (Note: it’s a good idea to use the contact selector when selecting users and groups. For simplicity’s sake in this example, I’m using only a text field. If you’re interested in using the contact selector in InfoPath, check out this blog post.)
Here are the steps I followed to create my association form:
  1. Create a new form in InfoPath. To do this, select Design a Form Template… from the wizard, then use the Blank form template.
  2. Set up the main datasource. To do this, click on the Data Source link in the Design Tasks task bar on the right side of the screen.
  3. You should be brought to a panel that looks something like this:

    InfoPath creates a default datasource called myFields for you.
  4. Double-click on the myFields group to rename it. I like to use Main.
  5. InfoPath also sets you up with one field – aptly named field1. Double-click on the field and rename it to Approver.
  6. Your datasource should now look like this:
  7. Now it’s time to lay out the form. Drag the Approver field from the data source onto the form design area. You’ll notice it automatically creates a text box bound to that field. Switch over to the Controls task pane and drag a button on to the form. Apply some pretty formatting if you desire, and your form should look something like this:
  8. Now we need to tell the form what to do when the button is clicked. Double-click on the button you dragged on to the form. Change the label to read Submit, then click the Rules button.

    Click Add to add a new rule, then Add Action. In the action dialog, select Submit using a data connection from the dropdown list.

    Next to the disabled data connection dropdown, click Add. On the next screen, choose to create a new connection to submit data.

    Configure your new connection to submit data to the hosting environment.

    Give your new data connection a name and click Finish. Your action dialog should now look something like this:

    Click OK two times to return to the definition of the rule. Click the Add Action button again and choose the Close the form option from the action dropdown. Uncheck the checkbox, as shown here:

    Click OK, and your definition of the rule that runs for the submit button should look like this:

    Click OK twice and you’re back out to the form design surface. Now when the button is clicked, InfoPath will send the form’s data as XML to the hosting environment (SharePoint, in this case).

  9. Just a few more housekeeping items and our form is nearly complete. First, we need to set the security level on the form. To do this, select Tools > Form Options from the menu in InfoPath. Click on the Security and Trust panel and configure your form to run with Domain security.

    (Side note: If you leave the “Automatically determine security level” checkbox checked, SharePoint will show that your form is using Domain security when you install the form to InfoPath Forms Services, but the form won’t work in your workflow. I’m not sure why this step is required, but it’s a deal-killer if you forget to do it.)
  10. Now it’s time to save and publish. First, save the form template to a local file. Then return to InfoPath and select File > Publish from the menu.
  11. In the Publishing Wizard, choose to publish the form to a network location:
  12. On the next screen, choose the path to which you want to publish. Use the same filename as you used when saving the form, but put it in a different folder. My suggestion is to use a folder called Publish (or something equally clever) and to put all of your published InfoPath forms for the current workflow in that location. When it comes time to deploy, it will be easy to get all of your form templates together.
  13. On the next step of the wizard, it’s very important to leave the text box for the alternate access location blank:

    InfoPath will show you this dire-sounding warning:

    This is where you have to be strong and click OK without entering a value.
  14. The last step of the publishing wizard should look like this. Click Publish to complete the process.
  15. There’s one final step I’m going to do that’s technically optional. When the workflow stores the association and initiation data from the association and initiation forms, respectively, it does so as XML in the AssociationData and InitiationData properties of the SPWorkflowActivationProperties class.

    To get at this data, you have two choices:

    a. Parse the XML in the property yourself, or
    b. Create a class that can be deserialized from the XML structure.

    For the sake of example, I’m going to show the second option here, as InfoPath has pretty nice features to support it.

    Go to File > Save as Source Files on the menu in InfoPath. You’ll need to choose a folder (a different one than your file is saved and published to) in which the source files will be created.

  16. Once the files have been saved as source, close InfoPath, then open up a Visual Studio 2005 Command Prompt and navigate to the directory in which you saved the source files. If you take a look at the files that are created there, you’ll see one called myschema.xsd. This is the file that InfoPath creates that specifies the schema of the data that the form will submit, and is the file from which we can generate a C# class that can be deserialized from XML at runtime.

    To generate the class, type the following at the command prompt:

    xsd myschema.xsd /c

    The /c operator tells the XSD utility to generate classes (or in our case, a single class) from the specified XSD file.

    The result should look like this:

    Notice that it created a C# code file. (Note: All you VB.NET people out there can do this using the /language switch on the XSD utility, but I’ll be writing my workflow in C#, so the default output is fine with me.)

    We’ll come back to this file in my next post, but for now, on to more InfoPath forms.

So now we’ve created the association form in 16 (!) easy steps; just three more forms to go <grin />. You’ll get substantially faster at this process once you’ve pounded through it a few times.

The next form will be the initiation form that will be displayed when the workflow is started. On this form, I simply need to have some comments and a button to submit the proposal for review.

To do this, I’m going to follow pretty much exactly the same process I went through above:

  1. Create a new form template in InfoPath
  2. Create your datasource. In this case, mine has one field called Comments.
  3. Design the form, binding the field in the datasource to the one on the form.
  4. Add a button to the form and set it up to post its data to the hosting environment and then close the form.
  5. Set the security on the form to Domain level.
  6. Save the form. I called mine InitForm.xsn.
  7. Publish the form.
  8. Save the form’s source files and generate the C# class.

(It looks easier in that list, doesn’t it?)

When you’re done, your form should look something like this:

Two more forms. The next one is the one that will be used by the manager to review the status of the proposal. The main feature of this form will be the decision of whether or not to approve the proposal.

Follow the same general steps:

  1. Create a new form template in InfoPath
  2. Create your datasource. In this case, mine has one field called Approved, which I configured to be a Boolean value.
  3. Design the form, binding the field in the datasource to the one on the form. You’ll note when you drag a Boolean value on to the form, it automatically chooses a checkbox to represent the value.
  4. Add a button to the form and set it up to post its data to the hosting environment and then close the form.
  5. Set the security on the form to Domain level.
  6. Save the form. I called mine ReviewForm.xsn.
  7. Publish the form.
  8. It’s not necessary to save the source files for this form: the association and initiation forms are special cases and make their data available to the workflow differently than other forms do. For this form, we will access the data from the form using the ExtendedProperties of the SPWorkflowTaskProperties.

My manager review form looks like this:

Just one more. We need a form that the salesperson uses to specify the outcome of the proposal. The possible values are:

  1. Accepted by client
  2. Rejected by client
  3. Needs changes

The result of this form determines what we do in the workflow.

So – you’re getting the hang of this, right? – we create a form in InfoPath:

  1. Create a new form template in InfoPath
  2. Create your datasource. In this case, mine has one field called NextStep, which I configured to be a text value.
  3. Design the form, binding the field in the datasource to the one on the form. When I dragged the NextStep field onto the form, it automatically created a TextBox for me. However, I want to display the available options in a dropdown list. You can easily change the bound control type by right-clicking on the TextBox and selecting the Change To option from the context menu.

    Double-click on the dropdown list to configure its list items. I set mine up manually so that the available values in the dropdown list will be Accepted, Rejected, and Changes.
  4. Add a button to the form and set it up to post its data to the hosting environment and then close the form.
  5. Set the security on the form to Domain level.
  6. Save the form. I called mine SubmittedForm.xsn.
  7. Publish the form.
  8. Again, since this is not the association or initiation form, I’m going to skip the step of generating the source files for this form.

My form looks like this:

That’s it for the InfoPath form creation. With the XSN files that are in your Publish folder and with the two C# files generated for the association and initiation forms, you’re ready to move on to the next step. My next post will cover creating the state machine workflow in Visual Studio.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Matthew Morse

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram