VSTA Point
Programming against the InfoPath 2007 Managed Code Object Model in VSTA
October 30, 2009
asp:feature
LANGUAGES:ALL
ASP.NETVERSIONS: ALL
VSTA Point
Programming against the InfoPath 2007 Managed Code ObjectModel in VSTA
By Cole Shiflett
Microsoft Office InfoPath 2007 and InfoPath Forms Serverenable developers to design powerful browser-based forms and deploy them tousers 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 thatenables developers to create professional end-user forms with embedded businesslogic that can be deployed into the InfoPath Forms Server environment hostedwithin MOSS 2007. This article focuses on the VSTA programming environmentwithin InfoPath, and simple data access using .NET XPath objects.
A full explanation of InfoPath, Forms Server, and XPathand the differences between client and Web forms is beyond the scope of thisarticle, but there are several excellent Internet sources available (see Figure1).
Microsoft InfoPath 2007 Site | |
Microsoft Office Forms Server 2007 Site | http://office.microsoft.com/en-us/formsserver/FX100490391033.aspx |
Microsoft InfoPath 2007 How-To | http://office.microsoft.com/en-us/infopath/FX100647031033.aspx |
Microsoft InfoPath 2007 Solution Center | |
MSDN Reference on XPath Assembly | http://msdn.microsoft.com/en-us/library/system.xml.xpath(VS.80).aspx |
Figure 1: Checkthese sites for more on InfoPath, Forms Server, and XPath.
Working with InfoPath offers some distinct differencesover traditional ASP.NET development. The managed code environment is availablevia Visual Studio add-ins: Visual Studio Tools for Applications (VSTA) andVisual 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 toMicrosoft 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 beexplicitly selected during the installation process to be available in the designerenvironment (see Figure 3). Many prefer using VSTA because it s an integratedpart of the InfoPath designer client.
Figure 2: InfoPath 2007 templateusing VSTO extension in Visual Studio 2008.
Figure 3: Microsoft Office InfoPath2007 Install: Visual Studio Tools for Applications option.
Although the vast toolbox to which developers areaccustomed in Visual Studio is not available in the VSTA environment, there area 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 MicrosoftWord than Visual Studio. InfoPath form templates are based on XML; manipulationof form data in code is achieved by using XPath through the System.Xml.XPathassembly.
InfoPath provides a limited event model when designingbrowser-based forms. Form templates are derived from InfoPath data sources,which can be configured within the form itself, or derived from a Web serviceor XML file. These data sources are necessary in order to bind the controlsthat are added to the form. Such binding is done only at design time. InfoPathfields do allow for standard data types such as string, integer, date, andboolean.
After fields have been added to the data source anddragged on to the form palette, code can be written to access the data thatwill be entered by the users. Before accessing the coding environment, it isimportant to review a few default settings, such as choosing the preferredprogramming language and making the form browser-compatible. Setting thelanguage 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 projectthat contains a file structure (just like a standard ASP.NET project).
Figure 4: Selecting the Programming languagein the Form Options Menu.
Ensure that the form is browser-compatible once deployedto Forms Services by selecting the Compatibility option (see Figure 5). TheForm Options menu contains dozens of settings, so it s important to explore allof these settings in order to provide maximum control over the form beforedeploying to the Forms Server environment. Once these options are set, accessthe IDE by selecting the Programming option under the Tools menu. The event methodstubs at the form and field level will be created automatically if invokedthrough the Programming menu option (or in the right-click context menu).
Figure 5: Ensuring InfoPath 2007form is set up for browser compatibility.
To access a field value from the form, the XPathNavigatorclass can be used. First, create a new XPathNavigator object and set it toreference the form s data source. Next, create a similar object, but make areference to the field(s) to be accessed. The example shown in Figure 6 usesthe Changed event of the Country field (invoked through the right-click contextmenu) 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 demonstratingdata access using .NET XPathNavigator assembly.
Once the XPathNavigator reference is created to the form smain data source, that reference is passed to the field level XPathNavigatorobjects. At the field level, a method such as SelectSingleNode can be usedwhereby the field s XPath expression is passed as a parameter in order toobtain the values stored in the field. Retrieving that XPath expression stringis easy; simply go to the Data Source view in the designer and right-click onany field, then select Copy XPath from the context menu and paste that in tothe code. A field value can be set by calling the SetValue method ofXPathNavigator and passing in the new value. Aside from XPath, the methodsavailable should be relatively intuitive to experienced .NET developers,because at the core it is just getting and setting data. There are manyadditional classes and methods available for controlling the form and its data,but it is important to understand the simple examples above in order to providethe foundation for accessing and manipulating field values and the associatedevents in which they can be triggered.
This simple code works fine in both the client and Webforms; however, as forms are deployed to the Forms Server environment and it isrendered as an ASPX page within the MOSS environment, conditions may beencountered requiring additional InfoPath form configuration. For example, toensure that the browser form performs a postback for an event (such as thechanging of a value in a dropdown field), go to the Properties window of afield and select the Browser forms tab (see Figure 7). In this section thepostback behavior can be adjusted, such as selecting Always to ensure thebrowser form executes the event as desired.
Figure 7: Controlling browser formpostback behavior.
This article focused on setting up the VSTA codingenvironment in Microsoft Office InfoPath 2007 and how to use a few .NET XPathobjects to manipulate data entered into InfoPath forms. Information ondeployment of these forms in to SharePoint using Forms Server is a topic of itsown and may be addressed in future articles. Until then, there are severalvaluable resources on the Web to get you started, including those provided withthis article.
Cole Shiflett is aSolutions Architect at a large financial services company in Atlanta, GA.
Read more about:
MicrosoftAbout the Author
You May Also Like