"Design Once" and the new InfoPath 2007 Object Model

One of the main design principles for InfoPath 2007 and Forms Services 2007 is that you can create your form once, write code for it once, build and publish it once, and then have it work in both the browser and the client. We call this Design Once.

 

To make this possible for form templates with code, we created a new object model that works on both the client and the server, so that the exact same assembly can work in both places. This is similar to what the Compact .NET Framework provides for mobile devices, where you can write a WinForm app that works on both the desktop and a Windows Mobile smartphone. Using the InfoPath object model you can write code for your form that will run the same way both on client, when opened in InfoPath, and on the server, when opened in the browser.

 

Getting started with System.Xml

If you've written code for InfoPath 2003, then you're used to writing code against the MSXML-based object model. For example, you might read and write a field's value like this in C# with the old object model:

 

string xpath = "/my:myFields/my:field1";

IXMLDOMNode field1 = thisXDocument.DOM.selectSingleNode(xpath);

string oldValue = field1.text; // Read

field1.text = "New Value"; // Write

Here's how to do the same thing using the new object model:

string xpath = "/my:myFields/my:field1";

XPathNavigator field1 = MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);

string oldValue = field1.Value; // Read

field1.SetValue(“New Value”); // Write

 

If we break that down, we see the following mappings:

 

InfoPath 2003 OM InfoPath 2007 OM
thisXDocument.DOM this.MainDataSource
IXMLDOMNode XPathNavigator
Read field1.text Read field1.Value
Set field1.text Set using field1.SetValue()

 

Compatibility: Using the old OM in InfoPath 2007

InfoPath continues to open forms that used the old object model, so you don't have to upgrade them just to use InfoPath 2007. That said, if you want a form to work in the browser, you will need to upgrade to the new object model. If you upgrade the code, InfoPath will automatically update the project format and rebind all your events, but you will need to convert your custom functions to the new OM.

 

Compatibility: Using the new OM in InfoPath 2003

The new object model won't work in InfoPath 2003, so we also allow you to create forms that use the old object model in InfoPath 2007. You can choose which version of the object model you want to use in Form Options in the Programmability category. 

 

Enjoy!

Ned