Setup Namespaces for a Secondary Data Source - Matt Faus
in

InfoPath Dev

Matt Faus

Setup Namespaces for a Secondary Data Source

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:

  1. Open the Data Source task pane, and then choose the secondary data source from the Data Source drop-down list.
  2. Double-click on the node that you need the namespace for, and then click to the Details tab.
  3. 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:

  1. Open a text editor, such as Microsoft Notepad and open the XML document that will be added as a secondary data source.
  2. Find the root node of the XML structure. This is usually the first node that begins with a < instead of <?.
  3. 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:

  1. Open the Microsoft Script Editor by choosing Programming | Microsoft Script Editor from the Tools menu.
  2. 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>

  1. 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"' );

  1. 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");

Comments

 

skaaal said:

You may be interested in extracting programmatically the namespace URI : just follow the link above.

July 4, 2007 2:45 AM
 

warrtalon said:

Matt, great job.  I didn't have do anything with code, but I was able to quickly use your guide to determine the proper namespace for a SharePoint secondary data connection (tasks list) for use in the CopyTable qRule.  Saving my template as source files and viewing the Tasks.xsd showed right away that the answer was "dfs."

Kudos.

June 24, 2009 12:02 PM

About Matt Faus

Matt holds a BS in Computer Science and Software Engineering from the University of Texas at Dallas. During his studies, he helped Qdabra Software develop DBXL and InfoPath solutions. During that time he lived in Dallas, Seattle, and Valencia, Spain. He now works at Microsoft, developing for Hotmail.
Copyright © 2003-2010 Qdabra Software. All rights reserved.
View our Terms of Use.