How to: Work with Views
Applies to: InfoPath 2010 | InfoPath Forms Services | Office 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio Tools for Microsoft Office
In this article
Overview of the View Class
Using the View Class
Selecting Controls in a View
When to use SelectText and SelectNodes
Using the SelectText and SelectNodes Methods
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.
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(ActionType) method |
Executes an editing command against a form's underlying XML document, based on the data currently selected in the view. |
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. |
Gets a reference to an XPathNodeIterator object for iterating over the returned XML nodes starting from the specified node. |
|
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(XPathNavigator) method |
Selects a range of nodes in a view based on the specified starting XML node. |
Selects a range of nodes in a view based on the specified starting XML node and ending XML node. |
|
Selects a range of nodes in a view based on the specified starting XML node, the ending XML node, and the specified control. |
|
SelectText(XPathNavigator) method |
Selects the text contained in an editable control that is bound to the node specified by the XPathNavigator object passed to this 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. To access the name of the view, you need to access the ViewInfo object associated with the view. The following example demonstrates how to display a message box with the name of the view that is currently active.
MessageBox.Show("Current view name: " +
this.CurrentView.ViewInfo.Name);
MessageBox.Show("Current view name: " & _
Me.CurrentView.ViewInfo.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 Loading 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"];
Selecting Controls in a View
InfoPath provides two methods of the View class, both of which are overloaded, to programmatically select a control in the current view: the SelectText() and SelectNodes() methods. The SelectText(XPathNavigator) method is used for data entry controls, such as a Text Box, while the SelectNodes method is used for structural controls, such as an Optional Section. To select a particular control in the view, you need to provide the node and, optionally, the control's ViewContext ID. The ViewContext ID is needed when you have multiple controls bound to the same node in the data source. InfoPath provides the ViewContext ID information when you design the form.
A control's ViewContext ID is displayed on the Advanced tab of the control's properties dialog box, which is accessed by right-clicking the control, clicking ControlName Properties, and then clicking the Advanced tab. The ViewContext ID of the control is listed in the Code section of the Advanced tab.
When to use SelectText and SelectNodes
You can programmatically select the following data entry controls by using the SelectText(XPathNavigator) method:
Text Box
Rich Text Box
Date Picker
You can programmatically select the following structural controls by using the SelectNodes(XPathNavigator) method:
Optional Section
Choice Section
Repeating Section (items)
Repeating Table (rows)
Repeating Recursive Section (items)
Bulleted, Numbered, and Plain List
Horizontal Repeating Table
You cannot programmatically select, or set focus to, the following controls:
Drop-Down List Box
List Box
Check Box
Option Button
Button
Picture (linked or included)
Ink Picture
Hyperlink
Expression Box
Vertical Label
ActiveX Section
Horizontal Region
Using the SelectText and SelectNodes Methods
In the following example, the SelectText(XPathNavigator) overload of the SelectText method, which provides one xmlNode parameter, is used to select a Text Box that is bound to "my:field1".
// Create XPathNavigator and select field.
XPathNavigator textNode =
CreateNavigator().SelectSingleNode(
"/my:myFields/my:field1", NamespaceManager);
// Select text in specified field.
CurrentView.SelectText(textNode);
If you have multiple controls bound to "my:field1", you must use the SelectText(XPathNavigator, String) overload of the SelectText method, which provides an additional viewContext parameter to select a specific control. The following example assumes that there are two Text Box controls bound to "my:field1", with the first control having a ViewContext ID of "CTRL1" and the second control having a ViewContext ID of "CTRL8". The second control is selected.
// Create XPathNavigator and select field.
XPathNavigator textNode =
CreateNavigator().SelectSingleNode(
"/my:myFields/my:field1", NamespaceManager);
// Select text in specified field.
CurrentView.SelectText(textNode, "CTRL8");
In the following example, the SelectNodes(XPathNavigator) overload of the SelectNodes method, which provides only one startNode parameter, is used to select the first row in a repeating table bound to the repeating group "my:employee".
// Create XPathNavigator and specify XPath for nodes.
XPathNavigator repeatingTableRow1 =
CreateNavigator().SelectSingleNode(
"/my:myFields/my:employees/my:employee[1]", NamespaceManager);
// Select nodes in specified XPathNavigator.
CurrentView.SelectNodes(repeatingTableRow1);
You can also select multiple rows in a repeating table. In the following example, the first three rows of a repeating table bound to the repeating group "my:employee" are selected using the SelectNodes(XPathNavigator, XPathNavigator, String) overload of the SelectNodes method, which provides startNode and endNode parameters:
// Create XPathNavigators to specify range of nodes.
XPathNavigator repeatingTableRow1 =
CreateNavigator().SelectSingleNode(
"/my:myFields/my:employees/my:employee[1]", NamespaceManager);
XPathNavigator repeatingTableRow3 =
CreateNavigator().SelectSingleNode(
"/my:myFields/my:employees/my:myemployee[3]", NamespaceManager);
// Select range of nodes in specified XPathNavigators.
CurrentView.SelectNodes(repeatingTableRow1, repeatingTableRow3);