Reference to undeclared namespace prefix: 'my' error in InfoPath

Learn how to solve the InfoPath error: Reference to undeclared namespace prefix: 'my'.

ADVERTISEMENTS

Problem

You have an InfoPath form that uses the NewFromSolution method to dynamically create an InfoPath form that is based on another InfoPath form template. The form has a field named field1, which you want to populate (=fill) with data.

When you use the following JScript code in the event handler for a button that dynamically creates a form:

var oXDoc = Application.XDocuments.NewFromSolution("C:\\BaseFormTemplate.xsn");
var field1 = oXDoc.DOM.selectSingleNode("//my:field1");
field1.text = "Text to go in field1";

you get the following error:

A run-time error has occurred.
Do you want to debug?

The following error occurred:

Reference to undeclared namespace prefix: 'my'

File:script.js
Line:25

Solution

You can solve this problem on one of two ways:

  1. Use the setProperty method of the XDocument.DOM object.
  2. Use the local-name() function in the XPath expression that retrieves the field.

Discussion

To use the first method, you must know the full namespace that the 'my' namespace prefix is referring to. To find out what this namespace is:

  1. Fill out a form based on the form template that you want to dynamically create.
  2. Save the form locally.
  3. Open the XML of the form in Notepad.
  4. In the XML of the form, search for the namespace that the 'my' prefix is referring to.

Important:
The error message could refer to any namespace prefix that has been defined in the form template. If the error message refers to a namespace prefix other than 'my', you must search for the namespace that the error message is referring to, and not the 'my' namespace.

In the form that I used, the 'my' prefix referred to the following namespace: http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-12-03T23:56:18.

You can then use the namespace that you retrieved from the XML of the form as a parameter for the setProperty method to declare the namespace prefix as shown in the following code:

var oXDoc = Application.XDocuments.NewFromSolution("C:\\BaseFormTemplate.xsn");
oXDoc.DOM.setProperty("SelectionNamespaces",
"xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-12-03T23:56:18\"");
var field1 = oXDoc.DOM.selectSingleNode("//my:field1");
field1.text = "Text to go in field1";

The second method is a 'quick and dirty' way of finding an element in an XML document without having to worry about namespaces, since the local-name() function searches on an element's name and ignores all namespaces. The danger of using this method is that if your InfoPath form contains elements that have the same name but different namespaces, you won't be able to distinguish between these elements. However, if you are absolutely sure that the elements' names in your InfoPath form are unique, you can choose to use this method.

The following code shows how you can use the local-name() function.

var oXDoc = Application.XDocuments.NewFromSolution("C:\\BaseFormTemplate.xsn");
var field1 = oXDoc.DOM.selectSingleNode("//*[local-name()='field1']");
field1.text = "Text to go in field1";

 
 Subscribe for updates via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services