View.GetContextNodes method

Office 2013 and later

Gets a reference to an XMLNodesCollection collection that is populated with XML Document Object Model (DOM) nodes based on the current context.

Namespace:  Microsoft.Office.Interop.InfoPath.SemiTrust
Assembly:  Microsoft.Office.Interop.InfoPath.SemiTrust (in Microsoft.Office.Interop.InfoPath.SemiTrust.dll)

XMLNodesCollection GetContextNodes(
	Object varNode,
	Object varViewContext
)

Parameters

varNode
Type: System.Object

An XML DOM node.

varViewContext
Type: System.Object

The ID of the control that is used for the context.

Return value

Type: Microsoft.Office.Interop.InfoPath.SemiTrust.XMLNodesCollection
A reference to the XMLNodesCollection collection.

The collection returned by the GetContextNodes method consists of the sequence of XML DOM nodes that are mapped from the view, corresponding to the current XSL Transformation (XSLT) node, starting at the current selection and walking up through the view ancestors to the BODY tag.

If no parameters are used, the context nodes are based on the current selection. If parameters are used, then the context nodes returned are those that would be returned based on the selection that would be obtained from calling the SelectNodes method.

NoteNote

The GetContextNodes method will not return nodes based on the current selection if used in the OnClick event of a button in the view, since the focus is lost from the control that is intended to be in context. To avoid this behavior, use the GetContextNodes method from a custom task pane, menu or toolbar.

Important noteImportant

This member can be accessed only by forms running in the same domain as the currently open form, or by forms that have been granted cross-domain permissions.

In the following example, the GetContextNodes method of the ViewObject object is used to return a collection of XML DOM nodes based on the current context. The code then loops through the collection of XML DOM nodes looking for a particular node. When it is found, the text of field1 is updated. This example requires a structure of a repeating section named group1 containing a field named field1.

XMLNodesCollection contextNodes = thisXDocument.View.GetContextNodes(Type.Missing, Type.Missing);
// Scan the list of context nodes for an field1 node and if one is found
// update its text.
 foreach (IXMLDOMNode contextNode in contextNodes)
 {
  if (contextNode.nodeName == "my:group1")
  {
   contextNode.selectSingleNode("my:field1").text = "found node";
   break;
  }
 }
Show: