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:

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.

The location is the location on the development machine, not the final location from which the published form will be accessed by your users.

After you click OK, the Project Wizard asks whether you want to create a new form or open a form that you have already created with InfoPath as shown in Figure 12-2.

Figure 12-2. Creating the project based on a blank or existing form.

To create a new, blank form, keep the default selection of Create new form template and click Finish. To create a new form template based on an XML data file, XML schema, database connection, or Web service, create the form template in InfoPath first without using Visual Studio, and then open it as an existing form template. After you have a form template created in InfoPath, you can select the Open existing form template option and browse to the form template (.xsn) or form template definition (.xsf) file.

JScript (.js) or VBScript (.vbs) files associated with an existing form template will also be imported but the script itself will be nonfunctional. Other scripts, such as scripts used for custom task pane extensibility for example, are not affected. If you select a form template that already has managed code, the associated code is not included in the import; rather, you should open the Visual Studio project associated with the form template.

After you have created your project, you can use Visual Studio as your code editor at the same time as you design the form using the InfoPath designer window, as shown in Figure 12-3. While developing for InfoPath, you will frequently switch between the InfoPath designer window and the Visual Studio window. In addition, when you press F5 in your project, InfoPath starts up another InfoPath window called a "preview" window to preview what your form would look like at runtime. This makes for three top-level windows you might be juggling at any given time. When you close the InfoPath preview window, Visual Studio stops debugging the project. If you accidentally close the InfoPath designer window, you can reopen it by choosing Open InfoPath from Visual Studio's Project menu.

Figure 12-3. Visual Studio window and the InfoPath designer window run as two separate windows.

Take a look at the Solution Explorer window on the right side of Visual Studio. Behind the scenes, a form template consists of many files working together. By default, when working with just InfoPath, the XML template, XML schemas, XSL views, and so on are hidden unless you choose Extract Form Files from the File menu and then explore that folder in Windows Explorer. When designing the code behind a form in Visual Studio, you can see and edit all files in the InfoPath project directly. However, the InfoPath designer "locks" all the forms files. If you want to manually edit the files in Visual Studio, first close the InfoPath designer window to unlock the files.

Visual Studio adds the following new commands to facilitate the development of code behind an InfoPath form:

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.

The first text box, called Preview Data File Path, takes a path to a custom XML file. Without a custom XML file, InfoPath previews with the default XML file used when filling out a new form. A custom XML file is useful for simulating what a user would experience if an existing form, saved from a previous editing session, were reopened. You can also use the Preview > With Data File command to achieve the same result.

The second text box, called Preview User Role, sets a "preview role." You can create forms that have different views depending on the "role" of the user filling out the form; an administrator might have a different view than an accountant. These roles are defined using InfoPath's User Roles command in the InfoPath Tools menu. You can also use the Preview > With User Role command to achieve the same result.

Категории