ASP.NET VERSIONS: ALL
Programming against the InfoPath 2007 Managed Code Object Model in VSTA
By Cole Shiflett
Microsoft Office InfoPath 2007 and InfoPath Forms Server enable developers to design powerful browser-based forms and deploy them to users across the enterprise. InfoPath provides an intuitive designer interface, as well as a managed code environment powered by Visual Studio 2008 and the .NET Framework. This combination delivers a rich development environment that enables developers to create professional end-user forms with embedded business logic that can be deployed into the InfoPath Forms Server environment hosted within MOSS 2007. This article focuses on the VSTA programming environment within InfoPath, and simple data access using .NET XPath objects.
A full explanation of InfoPath, Forms Server, and XPath and the differences between client and Web forms is beyond the scope of this article, but there are several excellent Internet sources available (see Figure 1).
Microsoft InfoPath 2007 Site
Microsoft Office Forms Server 2007 Site
Microsoft InfoPath 2007 How-To
Microsoft InfoPath 2007 Solution Center
MSDN Reference on XPath Assembly
Figure 1: Check these sites for more on InfoPath, Forms Server, and XPath.
Working with InfoPath offers some distinct differences over traditional ASP.NET development. The managed code environment is available via Visual Studio add-ins: Visual Studio Tools for Applications (VSTA) and Visual Studio Tools for Office (VSTO). Both are customized IDEs based on the .NET Framework, both support the VB.NET and C# languages. VSTO is built in to Microsoft Visual Studio 2008 and provided as an extension of the.NET 3.5 Framework (see Figure 2). VSTA, however, is built in to InfoPath itself, and must be explicitly selected during the installation process to be available in the designer environment (see Figure 3). Many prefer using VSTA because it s an integrated part of the InfoPath designer client.
Figure 2: InfoPath 2007 template using VSTO extension in Visual Studio 2008.
Figure 3: Microsoft Office InfoPath 2007 Install: Visual Studio Tools for Applications option.
Although the vast toolbox to which developers are accustomed in Visual Studio is not available in the VSTA environment, there are a standard set of controls available to design an InfoPath form. In fact, designing the user interface of a form in InfoPath may feel more like Microsoft Word than Visual Studio. InfoPath form templates are based on XML; manipulation of form data in code is achieved by using XPath through the System.Xml.XPath assembly.
InfoPath provides a limited event model when designing browser-based forms. Form templates are derived from InfoPath data sources, which can be configured within the form itself, or derived from a Web service or XML file. These data sources are necessary in order to bind the controls that are added to the form. Such binding is done only at design time. InfoPath fields do allow for standard data types such as string, integer, date, and boolean.
After fields have been added to the data source and dragged on to the form palette, code can be written to access the data that will be entered by the users. Before accessing the coding environment, it is important to review a few default settings, such as choosing the preferred programming language and making the form browser-compatible. Setting the language can be done in the Form Options menu under Programming (see Figure 4). This section also allows for the modification of the path of the VSTA project that contains a file structure (just like a standard ASP.NET project).
Figure 4: Selecting the Programming language in the Form Options Menu.
Ensure that the form is browser-compatible once deployed to Forms Services by selecting the Compatibility option (see Figure 5). The Form Options menu contains dozens of settings, so it s important to explore all of these settings in order to provide maximum control over the form before deploying to the Forms Server environment. Once these options are set, access the IDE by selecting the Programming option under the Tools menu. The event method stubs at the form and field level will be created automatically if invoked through the Programming menu option (or in the right-click context menu).
Figure 5: Ensuring InfoPath 2007 form is set up for browser compatibility.
To access a field value from the form, the XPathNavigator class can be used. First, create a new XPathNavigator object and set it to reference the form s data source. Next, create a similar object, but make a reference to the field(s) to be accessed. The example shown in Figure 6 uses the Changed event of the Country field (invoked through the right-click context menu) to obtain the value of the field, then write that value to another field (fields can have either a Changed or a Validated event handler).
Figure 6: Code sample demonstrating data access using .NET XPathNavigator assembly.
Once the XPathNavigator reference is created to the form s main data source, that reference is passed to the field level XPathNavigator objects. At the field level, a method such as SelectSingleNode can be used whereby the field s XPath expression is passed as a parameter in order to obtain the values stored in the field. Retrieving that XPath expression string is easy; simply go to the Data Source view in the designer and right-click on any field, then select Copy XPath from the context menu and paste that in to the code. A field value can be set by calling the SetValue method of XPathNavigator and passing in the new value. Aside from XPath, the methods available should be relatively intuitive to experienced .NET developers, because at the core it is just getting and setting data. There are many additional classes and methods available for controlling the form and its data, but it is important to understand the simple examples above in order to provide the foundation for accessing and manipulating field values and the associated events in which they can be triggered.
This simple code works fine in both the client and Web forms; however, as forms are deployed to the Forms Server environment and it is rendered as an ASPX page within the MOSS environment, conditions may be encountered requiring additional InfoPath form configuration. For example, to ensure that the browser form performs a postback for an event (such as the changing of a value in a dropdown field), go to the Properties window of a field and select the Browser forms tab (see Figure 7). In this section the postback behavior can be adjusted, such as selecting Always to ensure the browser form executes the event as desired.
Figure 7: Controlling browser form postback behavior.
This article focused on setting up the VSTA coding environment in Microsoft Office InfoPath 2007 and how to use a few .NET XPath objects to manipulate data entered into InfoPath forms. Information on deployment of these forms in to SharePoint using Forms Server is a topic of its own and may be addressed in future articles. Until then, there are several valuable resources on the Web to get you started, including those provided with this article.
Cole Shiflett is a Solutions Architect at a large financial services company in Atlanta, GA.