Getting Started
Before you can use VSTO to put code behind InfoPath forms, you must ensure that the following things are installed on your development machine:
- Visual Studio 2005
- InfoPath 2003 Service Pack 1 or later
- The Microsoft Office InfoPath 2003 Toolkit for Visual Studio 2005
The toolkit must be installed last because it has an explicit dependency on both Visual Studio and InfoPath.InfoPath Service Pack 1 contains the primary interop assemblies (PIAs) for InfoPath so that managed code can automate the InfoPath object model. Service Pack 1 also added the OnSave event, improved support for some offline scenarios, and digital signature support. You will read more about the details later in this chapter.
Creating a New Project
After you have the toolkit installed, open Visual Studio and choose New Project from the File Menu. Open up the Visual C# node in the tree view, choose Office, and then choose the InfoPath Form Template project as shown in Figure 12-1.
Figure 12-1. Creating an InfoPath project in Visual Studio.
[View full size image]

The location is the location on the development machine, not the final location from which the published form will be accessed by your users. |
Figure 12-2. Creating the project based on a blank or existing form.

Figure 12-3. Visual Studio window and the InfoPath designer window run as two separate windows.
[View full size image]

- Open InfoPath (opens the InfoPath designer window)
- Publish Form (the equivalent of choosing Publish from InfoPath's File menu)
- Preview > Default (previews the form you are designing in an InfoPath windowthe equivalent of creating a new form from the template you have designed)
- Preview > With Data File (previews the form you are designing with a custom XML file passed in as the initial data the form is editing)
- Preview > With User Role (previews the form with a custom role defined using InfoPath's User Roles command in the InfoPath Tools menu)
These commands are available in the Visual Studio Project menu, the Tools menu, and in the context menu that displays when you right-click the InfoPath Project node in the Visual Studio Solution Explorer window.The InfoPath Project Properties dialog, also accessible from the project's right-click menu, has two settings that prove useful when previewing your form as shown in Figure 12-4.
Figure 12-4. Setting InfoPath-specific project properties.
[View full size image]
