An often overlooked fact about accessing a secondary data source via code behind an InfoPath form is the need to setup the correct namespaces. Tutorials presented throughout the Internet often presume that the process for setting up namespaces is already known and do not mention it when they give code snippets with different namespaces.
Some very common errors that are usually due to namespace (or other XPath) issues are:
- Object Required
- Reference to undeclared namespace prefix: 'dfs'.
- 'null' or null is not an object

Figure 1. Two very common errors.
There are two common ways to obtain information about the namespaces that a data connection uses. Regardless of the data connection, the namespaces can be discovered through the InfoPath Data Source task pane. For XML documents, however, you may open the original XML file and look at the attributes of the root node to discover what namespaces need to be added.
Obtaining the namespace via the Data Source task pane:
- Open the Data Source task pane, and then choose the secondary data source from the Data Source drop-down list.
- Double-click on the node that you need the namespace for, and then click to the Details tab.
- The namespace is given in the first field, named Namespace.

Figure 2. The namespace for an XML document created by InfoPath.
Obtaining the namespaces of an XML document:
- Open a text editor, such as Microsoft Notepad and open the XML document that will be added as a secondary data source.
- Find the root node of the XML structure. This is usually the first node that begins with a < instead of <?.
- Find all of the xmlns attributes of this element. The names of the namespaces are directly after the colon and the value of each namespace is in quotation marks after the equals sign.

Figure 3. Finding the namespace using Notepad.
Once you know the namespace, a reference to it must be established in the code behind the form. At the very top of the code that InfoPath automatically generates there is a section where the namespaces for the Main DOM are referenced and this is where the references for the secondary data sources should go.
Add references to the secondary namespaces:
- Open the Microsoft Script Editor by choosing Programming | Microsoft Script Editor from the Tools menu.
- At the end of the introductory comments there is a section of code like the following:
//<namespacesDefinition> XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-01-17T04:02:20"'); //</namespacesDefinition> |
- This is the code that references the namespaces of the Main DOM. To add code that references the namespaces of the secondary data sources follow this template (replace red text with pertinent information):
XDocument.GetDOM( "Name of your secondary data source" ).setProperty( "SelectionNamespaces" , 'xmlns:ns1="Text obtained from Figure 2" xmlns:ns2="Another namespace"' ); |
- After updating the above template with your own values add the line of code directly beneath the line with references for the Main DOM.
Now that the additional namespaces have been added to the code, you can reference nodes from secondary data sources with a statement similar to this:
XDocument.GetDOM("Name of your secondary data source").selectSingleNode( "/ns1:myFields/ns1:field1"); |