14.4. Form Programming

There are three ways you can add intelligence to an InfoPath 2007 form. You can attach conditional rules directly to data elements, you can write script that gets embedded into the form, and you can write managed code that is called from a separate "code behind" assembly. In addition to these choices, there are two ways that you can write managed code. You can install Visual Studio Tools for Applications (VSTA) and launch the development environment from within InfoPath, or you can install Visual Studio Tools for Office (VSTO) and launch InfoPath from within the Visual Studio 2005 IDE. The project structure is the same in both cases, but with VSTO you have more control over the client application. For instance, you cannot write code that modifies the InfoPath 2007 user interface from within a VSTA project. For that, you have to use VSTO.

To start adding code to a form, you must first select the appropriate options in the form itself. Open the form options dialog by choosing Form Options from the Tools menu in InfoPath 2007, and then select the Programming category from the list on the left side. In the Programming Language section, you can choose the form template code language from a dropdown list. Selecting JScript or VBScript causes InfoPath to launch the Microsoft Script Editor when you add an event handler to your form. Selecting C# or Visual Basic launches VSTA. If VSTA is not installed, then these options do not appear in the list.

14.4.1. Using Script

If your custom logic does not rely on managed code, you can write it using JScript or VBScript. When you add an event handler to a form that is setup for scripting, you write the code in the Microsoft Script Editor. Figure 14-12 illustrates this with a simple example that overrides the default value of a data field when the form is loaded.

Figure 14.12. Figure 14-12

Improvements in InfoPath 2007

One thing that has really improved in InfoPath 2007 is the prescriptive guidance that is offered when you are writing script. In InfoPath 2003, you had to continually consult the data source to determine the proper XPath expression to use when accessing the form data model from script. InfoPath 2007 makes it easier by injecting the associated XPath expression into the comment for the event handler. Keep in mind that neither the comment nor the function name is updated if you change the underlying object model, although the link to the generated function remains in place even if the name of the original data element changes.


14.4.2. Using Visual Studio Tools for Applications

Visual Studio Tools for Applications provides a familiar development environment for developing smart forms. If you've developed managed code for InfoPath 2003 forms, you may recall that the solution architecture was somewhat convoluted because of the way Visual Studio communicated with the InfoPath 2003 client. The development picture has improved dramatically with the release of InfoPath 2007 and Visual Studio 2005. First, all of the code is now fully event driven, meaning that there is no special startup code you have to write. Instead, you can simply select the event you want to handle and InfoPath will generate the wrapper code for you, creating a handler on the fly.

Before you can start writing VSTA code, you must install the VSTA IDE on your development machine, as follows:

  1. If you haven't done so already, open the Control Panel and select Add/Remove Programs.

  2. Find the Microsoft Office InfoPath 2007 in the list, and click Change.

  3. Select the VSTA option under .NET Programmability Support to install the IDE.

  4. Next, open InfoPath and set up your programming preferences.

  5. Select Options from the Tools menu, and then click the Design tab, as shown in Figure 14-13.

  6. In the Programming Defaults section, select the programming languages you prefer to use when designing forms that run either in InfoPath alone, or in InfoPath and InfoPath Forms Services. Here, you can also set the default location for creating projects.

Once you get all this set up, you can start writing managed code for your forms. To add code for an event, select Tools Programming from the main menu and then select the event you want to code against. This will open the VSTA IDE and generate the proper declaration for adding your code.

Figure 14-14 shows the VSTA IDE for customizing the Asset Tracking form. Notice that the project contains the appropriate references, and the assembly is signed using a generated public key. While developing your code, you will enjoy the usual benefits of IntelliSense and integrated debugging as well as tight integration with the InfoPath client.

Figure 14.13. Figure 14-13

Figure 14.14. Figure 14-14

When you are finished building your code, the generated assembly is added to the form template, as shown in Figure 14-15.

Figure 14.15. Figure 14-15

14.4.3. Using Visual Studio Tools for Office

Visual Studio Tools for Office provides the most comprehensive development experience available for building smart electronic forms. When you install VSTO, you get a number of project templates for creating solutions at the application level (called "add-ins") and at the document level (generally called "templates"). Use an application- level add-in when you want to customize the authoring experience within the application. Use a document- level template when you want to create a custom document with specialized behavior.

Start by creating a new InfoPath Form Template project. You will find this project template in the Office node of the project type tree in the New Project dialog under your language of choice. Give the project a name and click OK. Figure 14-16 shows the starting layout of a new form template project for customizing the sample Asset Tracking form. The first thing you'll notice is that the InfoPath design surface is hosted within the Visual Studio IDE. This means that you can design your form from within Visual Studio without having to switch back and forth between it and the InfoPath client. You also get the Design Tasks task pane with all the familiar links. Even the logic inspector is hosted within the Visual Studio environment.

Although many of the InfoPath commands are merged into similar locations in the Visual Studio IDE, the form-level event handlers are placed on the Insert menu instead of the Tools menu.

One drawback of the current VSTO implementation is that the property pane is not fully integrated with the InfoPath objects. For example, if you select a data element in the Data Source pane, the Visual Studio Properties pane is not updated to reflect the properties of the selected element. To get to the properties, you have to use the context menu and select the properties... command, which opens a modal dialog for editing the data element.

Figure 14.16. Figure 14-16

14.4.4. The Logic Inspector

When developing code for a form, it is often difficult to keep track of all the places where that code will be invoked within the form. This is mainly because of the fact that custom code can be attached to many form elements, some of which are dependent on other elements. To simplify this process, InfoPath 2007 includes a tool called the logic inspector that shows all of the code dependencies for the form in a single dialog. Figure 14-17 shows the logic inspector for the sample Meeting Agenda form, which automatically adjusts the meeting end time whenever the meeting start time is changed.

Using this tool, you can quickly get an overall picture of the code dependencies that exist in a form. This is particularly useful when working with legacy forms that were developed by others. Note that the logic inspector shows all of the logical dependencies that exist for the form, including logic that has been developed in managed code and attached to various elements. Although the managed code is not decompiled, it can be helpful to know which elements depend on which methods.

Figure 14.17. Figure 14-17

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.62