How to: Work with Views Using the InfoPath 2003 Object Model

When working with an InfoPath form template, you can write code to access the form's views, and then perform a variety of actions on the data that the views contain. The InfoPath 2003-compatible object model supports access to a form's views through the use of the members of the ViewObject interface.

Overview of the ViewObject Interface

The ViewObject interface provides the following methods and properties, which form developers can use to interact with an InfoPath view.

Note

The methods and properties of the ViewObject interface are not available during the OnLoad event.

Name Description

DisableAutoUpdate method

Disables synchronization of the XML Document Object Model (DOM) and the view.

EnableAutoUpdate method

Enables synchronization of the XML DOM and the view.

ExecuteAction method

Executes an InfoPath editing action.

Export method

Exports the view as a file of the specified format.

ForceUpdate method

Synchronizes the XML DOM and the view.

GetContextNodes method

Returns a reference to the XMLNodesCollection interface, based on the specified XML node and view context or on the current selection in the view.

GetSelectedNodes method

Returns a reference to the XMLNodesCollection interface, based on the current selection in the view.

SelectNodes method

Selects a range of XML nodes in the view.

SelectText method

Selects the text contained in the specified XML node in the view.

SwitchView method

Switches an InfoPath form to the specified view

Name property

Returns a string value indicating the name of the current view.

Window property

Returns a reference to the WindowObject interface which accesses the Window associated with the view.

Note

The InfoPath 2003-compatible object model also provides the ViewInfosCollection interface, which can be used to get information about all of the views implemented in a form.

Using the ViewObject Interface

The ViewObject interface is accessed through the View property of the XDocument interface (which is accessed through the thisXDocument variable that is initialized in the _Startup method of the form code class). For example, the following code sample demonstrates how to use the Alert method of the UIObject interface to display a message box with the name of the current view that is associated with a form's underlying XML document.

thisXDocument.UI.Alert("Current view name: " + 
   thisXDocument.View.Name);
thisXDocument.UI.Alert("Current view name: " & _
   thisXDocument.View.Name)

All InfoPath forms contain at least one default view; however, InfoPath also supports the creation of multiple views of a form's underlying XML document. When you have multiple views in a form, the View object can be used to work with the view that is currently active. You can programmatically change the view that is currently active by using the SwitchView method of the View object, as the following code sample demonstrates.

thisXDocument.View.SwitchView("MySecondView");
thisXDocument.View.SwitchView("MySecondView")

The previous example for switching a view will work only after the form is opened. To set a default view during the OnLoad event, use the IsDefault property of the ViewInfoObject interface, as shown in the following example.

thisXDocument.ViewInfos["MyDefaultView"].IsDefault = true;
thisXDocument.ViewInfos("MyDefaultView").IsDefault = True