How to: Work with Views
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 object model provided by the Microsoft.Office.InfoPath namespace supports access to a form's views through the use of the members of the View class.
Overview of the View Class
The View class provides the following methods and properties, which form developers can use to interact with an InfoPath view.
Note
The methods and properties of the View class are not available during the Loading event.
Name | Description |
---|---|
DisableAutoUpdate method |
Disables automatic synchronization between a form's underlying XML document and the associated view. |
EnableAutoUpdate method |
Enables automatic synchronization between a form's underlying XML document and the associated view. |
ExecuteAction method |
Executes an editing command against a form's underlying XML document, based on the data currently selected in the view. |
ExecuteAction method |
Executes an editing command against a form's underlying XML document, based on the specified field or group. |
Export method |
Exports the view to a file of the specified format. |
ForceUpdate method |
Forces synchronization between a form's underlying XML document and the associated view. |
GetContextNodes method |
Gets a reference to an XPathNodeIterator object for iterating over the returned XML nodes starting from the specified node. |
GetContextNodes method |
Gets a reference to an XPathNodeIterator object for iterating over the returned XML nodes in the current selection within the control bound to the specified field or group. |
GetSelectedNodes method |
Gets a reference to an XPathNodeIterator object for iterating over all XML nodes in the current selection of items in a view. |
SelectNodes method |
Selects a range of nodes in a view based on the specified starting XML node. |
SelectNodes method |
Selects a range of nodes in a view based on the specified starting XML node and ending XML node. |
SelectNodes method |
Selects a range of nodes in a view based on the specified starting XML node, the ending XML node, and the specified control. |
SelectText method |
Selects the text contained in an editable control that is bound to the node specified by the XPathNavigator object passed to this method. |
SelectText method |
Selects the text contained in an editable control that is bound to the node specified by the XPathNavigator object passed to this method, and the specified control. |
ShowMailItem method |
Creates an e-mail message containing the current view. |
ViewInfo property |
Gets a reference to a ViewInfo object associated with the view. |
Window property |
Gets a reference to a Window object associated with the view. |
Note
The InfoPath object model also provides the ViewInfoCollection and ViewInfo classes, which can be used to get information about all of the views implemented in a form.
Using the View Class
The View class is accessed through the CurrentView property of the XmlForm class, which is accessed using the this (C#) or Me (Visual Basic) keyword. For example, the following code sample demonstrates how to display a message box with the name of the view that is currently active.
MessageBox.Show("Current view name: " +
this.CurrentView.Name);
MessageBox.Show("Current view name: " & _
Me.CurrentView.Name)
All InfoPath form templates 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, the ViewInfoCollection can be used to work with all of the views implemented in the form template. To access the ViewInfoCollection of a form template, use the ViewInfos property of the XmlForm class. You can programmatically change the view that is currently active by using the SwitchView method of the ViewInfoCollection, as the following code sample demonstrates.
this.ViewInfos.SwitchView("MySecondView");
Me.ViewInfos.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 Initial property of the ViewInfoCollection class as shown in the following example. Note, however, that this value will only take effect after the form is saved and re-opened.
this.ViewInfos.Initial = this.ViewInfos["MyInitialView"];
Me.ViewInfos.Initial = Me.ViewInfos["MyInitialView"];