This chapter explores several ways to use Visual Studio 2010 with InfoPath. Visual Studio 2010 can be leveraged to create workflows for an InfoPath form as well as to create application-level add-ins.
The creation of Visual Studio workflows and the customization of Office products are broad subjects within themselves, and plenty of resources discuss these topics in great detail. The scenarios in this chapter provide a primer for using Visual Studio 2010 with InfoPath.
You may access the form contents within a workflow by creating an object of the form. To create an object, you need to define a class that defines the structure of your form. Luckily, utilities are available to automatically generate this class for you.
First you need the source files of your InfoPath form. There are two ways to accomplish this. One way is to export the source files from InfoPath, and the other is to just extract them from your form template file.
To export the source files of your form, follow these steps:
Figure 16.1. Clicking Export Source Files exports your form’s source files to a folder.
When creating a new folder to save the source files, it is a good idea to include Source Files in the name so you know what it contains (for example, Registration Form Source Files).
To extract the source files right from the form template, follow these steps:
The file that you are most interested in is the myschema.xsd file. This contains the XML definition of your form. Using the Visual Studio xsd.exe utility, you can easily generate a class file using myschema.xsd. To do this, follow these steps:
Figure 16.2. Using the xsd utility generates the class file.
Figure 16.3. Adding the form .cs file to your project allows you to leverage the form class.
Figure 16.4. Placing the form classes into the workflow namespace allows you to access the objects within the workflow code.
Now the class is part of the workflow project. The next steps and code pertain to the workflow class:
GetFormValues
method, as shown in Listing 16.2.This example assumes that you did not modify the main data source name from myFields to something else, although it is a good idea to rename the main data source to the form name or similar.
Listing 16.1. Using Statements
using System.Xml;
using System.Xml.Serialization;
Listing 16.2. GetFormValues
private void GetFormValues()
{
XmlSerializer serializer = new XmlSerializer(typeof(myFields));
XmlTextReader reader = new XmlTextReader(new System.IO.StringReader
(workflowProperties.InitiationData));
myFields registrationForm = (myFields)serializer.Deserialize(reader);
}
GetFormValues()
method from the onWorkflowActivated_Invoked
method or anywhere else needed in your workflow code.You may now access the form as an object and set class variables to the values the user entered, as shown in Figure 16.5.
Figure 16.5. Serializing the object allows you to access the entered values.
You may use Visual Studio 2010 to create an InfoPath 2010 add-in that can be used within InfoPath. The add-in is an application-level add-in that customizes the InfoPath Designer experience. Two common uses of an InfoPath add-in are to customize the ribbon and generate a custom task pane.
To create an InfoPath 2010 add-in, follow these steps:
Figure 16.6. Selecting the project template generates an InfoPath 2010 add-in project.
Figure 16.7. Clicking COM Add-Ins shows the available and installed application add-ins.
To create a new task pane, follow the steps for creating a new InfoPath 2010 add-in. Then perform these steps:
Listing 16.3. Private Declarations
private CustomTaskPaneControl customTaskPaneControl;
private Microsoft.Office.Tools.CustomTaskPane customTaskPane;
Listing 16.4. Custom Task Pane Instantiation
customTaskPaneControl = new CustomTaskPaneControl();
customTaskPane = this.CustomTaskPanes.Add(customTaskPaneControl, "Custom
Task Pane");
customTaskPane.Visible = true;
Figure 16.8. Building a custom task pane add-in displays the user control in InfoPath.
3.145.16.23