Microsoft KB Archive/896451

= How to dynamically load data in an InfoPath form =

Article ID: 896451

Article Last Modified on 3/27/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2007
 * Microsoft Office InfoPath 2003
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

-



SUMMARY
This article describes three ways to dynamically load data in a Microsoft Office InfoPath form. You can use the OnLoad event, automate the InfoPath ExternalApplication object, or create an ASP page. Code samples are provided.



INTRODUCTION
This article describes how to dynamically load data in an Office InfoPath form.



MORE INFORMATION
An InfoPath form is an XML file. The file contains the data from the form and XML processing instructions that tell Microsoft Windows Explorer or Microsoft Internet Explorer to which application the XML data belongs.

You can dynamically load data into InfoPath by using one of the following three methods:  Use the OnLoad event.

In the OnLoad event, you can call an ASP page or another external data source to retrieve the data and to copy it into the form. Automate the InfoPathExternalApplication object.

This method creates a new form that is based on the data from an external source.

Note The Initialize and script ActiveX controls not marked as safe setting in Internet Explorer must be set to Prompt or Enable for this method to work.

The data from the external source must also be valid InfoPath XML. Create an ASP page.

The ASP page returns XML data in the correct format for InfoPath to open as a form file. The XML data contains a reference to the InfoPath Form template where the data should be displayed. InfoPath can display the data without a problem.

Create a new virtual directory

 * 1) Start Microsoft Internet Information Services Manager.
 * 2) Expand the Web Sites directory in the tree view.
 * 3) Right-click the Web site where you want to create the new virtual directory, point to New, and then click Virtual Directory.
 * 4) Follow the steps in the Virtual Directory Creation Wizard to create a new virtual directory.

Note You must grant Write permission to the virtual directory.

InfoPath 2007
 Start Infopath. In the left pane, click Design a Form Template. Under Design a new, click to select the Form Template option, click to select the Blank option, and then click OK. Under Design Tasks, click Controls.</li> Click Text Box two times to add two Text Box controls to the form.</li> Save the form.</li> On the File menu, click Publish.</li> In the Publishing Wizard, click Next. Click To a network location, and then click Next.</li> In the Form template path and file name box, type the URL of the virtual directory that you created.</li> In the Form template name box, type twofield.xsn.

Note Make sure that the URL resembles the following:

http:// / /twofield.xsn

</li> Click Next, click Publish, and then click Close.</li> Exit InfoPath.</li></ol>

InfoPath 2003
<ol> Start Infopath. In the left pane, click Design a Form.</li> Under Design a Form, click New Blank Form.</li> Under Design Tasks, click Controls.</li> Click Text Box two times to add two Text Box controls to the form.</li> On the File menu, click Publish.</li> In the Publishing Wizard, click Next, click To a Web server, and then click Next.</li> In the Web URL and file name box, type the URL of the virtual directory that you created.</li> <li>In the Form name box, type twofield.xsn.

Note Make sure that the URL resembles the following:

http:// / /twofield.xsn

</li> <li>Click Next, click Finish, and then click Close.</li> <li>Exit InfoPath.</li></ol>

Create the Default.asp page
<ol> <li>Click Start, click Run, type notepad, and then click OK.</li> <li> Paste the following code in Notepad. <%@ Language=VBScript %>

<%

dim serverName, hrefPath, fileNamePos, serverPath

serverName = Request.ServerVariables(&quot;SERVER_NAME&quot;)

hrefPath = Request.ServerVariables(&quot;PATH_INFO&quot;)

fileNamePos = InStr( 1, hrefPath, &quot;/Default.asp&quot;, 1 )

hrefPath = Left(hrefPath, fileNamePos )

serverPath = &quot;http://&quot; + serverName + hrefPath

%>

<HTML>

<SCRIPT LANGUAGE=VBScript>

Sub ButtonClick

Dim oInfoPathApp

set oInfoPathApp = CreateObject(&quot;InfoPath.ExternalApplication&quot;)

oInfoPathApp.New &quot;<%=serverPath%>GetData.asp?dataRequest=2&quot;

set oInfoPathApp = Nothing

window.close

End Sub

</SCRIPT>

</HEAD>

<BODY onload=&quot;ButtonClick&quot;>

</BODY>

</HTML> </li> <li>Save the file as Default.asp. Save the file in the virtual directory that you created in the &quot;Create a new virtual directory&quot; section.</li></ol>

Create an ASP page to generate a form dynamically
<ol> <li>Locate and then double-click the Twofield.xsn form that you just published.

Note A new blank form is opened for you to fill out.

</li> <li>Click File, and then click Save As.</li> <li>Save the file as Form1.xml.</li> <li>Click Start, click Run, type notepad, and then click OK.</li> <li> Paste the following code in Notepad. <%@ Language=VBScript %> <% ' Define the XML namespace for the form. Dim strNamespace strNamespace = &quot;http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-04-21T15:25:26&quot; 'Calculate the path of this server directory. dim serverName, hrefPath, fileNamePos, serverPath serverName = Request.ServerVariables(&quot;SERVER_NAME&quot;) hrefPath = Request.ServerVariables(&quot;PATH_INFO&quot;) fileNamePos = InStr( 1, hrefPath, &quot;/GetData.asp&quot;, 1 ) hrefPath = Left(hrefPath, fileNamePos ) serverPath = &quot;http://&quot; + serverName + hrefPath ' Set our return content type. Response.ContentType = &quot;text/xml&quot;

'Create an XML document to return. Dim oXML Set oXML = Server.CreateObject(&quot;MSXML.DOMDocument&quot;) 'Create the XML header that all XML documents must have. dim myPI1 set myPI1 = oXML.createProcessingInstruction(&quot;xml&quot;, &quot;version=&quot;&quot;1.0&quot;&quot; encoding=&quot;&quot;UTF-8&quot;&quot;&quot;) oXML.insertBefore myPI1, oXML.childNodes.item(0) 'Create the mso-infopathSolution processing instruction that binds the XML from this page to the 'TwoField.xsn InfoPath form template. dim myPI2 set myPI2 = oXML.createProcessingInstruction(&quot;mso-infoPathSolution&quot;, &quot;solutionVersion=&quot;&quot;1.0.0.1&quot;&quot; productVersion=&quot;&quot;11.0.5531&quot;&quot; PIVersion=&quot;&quot;1.0.0.0&quot;&quot; href=&quot;&quot;&quot; + serverPath + &quot;TwoField.xsn&quot;&quot;&quot;) oXML.insertBefore myPI2, oXML.childNodes.item(1) 'Create the mso-application processing instruction that marks the XML from this page as an InfoPath form. dim myPI3 set myPI3 = oXML.createProcessingInstruction(&quot;mso-application&quot;, &quot;progid=&quot;&quot;InfoPath.Document&quot;&quot;&quot;) oXML.insertBefore myPI3, oXML.childNodes.item(2) 'Generate the XML for the form. dim myFields set myFields = oXML.createNode( 1, &quot;myFields&quot;, strNamespace ) oXML.appendChild myFields dim field1 set field1 = oXML.createNode( 1, &quot;field1&quot;, strNamespace ) dim field2 set field2 = oXML.createNode( 1, &quot;field2&quot;, strNamespace ) dim requestedData requestedData = Request(&quot;dataRequest&quot;) Select Case requestedData Case &quot;1&quot; field1.text = &quot;test field 1&quot; field2.text = &quot;test field 2&quot; Case &quot;2&quot; field1.text = &quot;test field 3&quot; field2.text = &quot;test field 4&quot; Case &quot;3&quot; field1.text = &quot;test field 5&quot; field2.text = &quot;test field 6&quot; Case Else field1.text = &quot;test field 7&quot; field2.text = &quot;test field 8&quot; end Select myFields.appendChild field1 myFields.appendChild field2 'Return the XML to the client. Response.Write oXML.XML %> </li> <li>On the File menu, click Save As, type GetData.asp in the File name box, and then click Save.</li> <li>Start a second instance of Notepad.</li> <li>On the File menu, click Open, click Form1.xml, and then click Open.</li> <li>Locate the xmlns:my namespace declaration in Form1.xml, and then copy its value.

Note The value resembles the following:

http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-04-21T15:25:26

</li> <li>In the GetData.asp fle, locate the following line:

strNamespace = &quot;http://schemas.microsoft.com/office/infopath/2003/myXSD/2003-04-21T15:25:26&quot;

</li> <li>Replace the value of the strNamespace variable with the value that you copied in step 9.</li> <li>Click Save, and then exit Notepad.</li> <li>Copy the GetData.asp file to the same virtual directory where the Twofield.xsn form template is located.</li></ol>

Add code to the InfoPath form to automatically load data from the ASP page
<ol> <li>Start InfoPath.</li> <li>In InfoPath 2007, click Design a Form Template in the left pane. In InfoPath 2003, click Design a Form in the left pane.</li> <li>In InfoPath 2007, click On My Computer under Open a form template. In InfoPath 2003, click On My Computer under Open a form in design mode in the Design a Form task pane.</li> <li>Type the URL of the form template that you published.

Note The URL resembles the following:

http://myServer/myVirtualDirectory/twofield.xsn

</li> <li>On the Tools menu, click Programming, and then click OnLoad Event.</li> <li> Replace the default OnLoad event code with the following code. function XDocument::OnLoad(eventObj) {  //Only load data if the fields are empty. if (XDocument.DOM.selectSingleNode(&quot;/my:myFields/my:field1&quot;).text == &quot;&quot; ) {     //Work out the location of the data file based on the current install directory. searchExpression = new RegExp( &quot;TwoField.xsn&quot;, &quot;ig&quot; ); searchResults = XDocument.Solution.URI.match( searchExpression ); if (searchResults.length == 0) return; //Clear the &quot;TwoField.xsn&quot; part of the string. var targetDirectory = XDocument.Solution.URI.slice(0, searchResults.index ); //Load the example data into and XML document. var objExternalData = new ActiveXObject(&quot;MSXML2.DomDocument.5.0&quot;); objExternalData.async = false; objExternalData.validateOnParse = false; objExternalData.load( targetDirectory + &quot;getData.asp?dataRequest=3&quot; ); //Set the SelectionNamespaces property so that we can search the example data. var strSelectionNamespaces = XDocument.DOM.getProperty(&quot;SelectionNamespaces&quot;); objExternalData.setProperty(&quot;SelectionNamespaces&quot;, strSelectionNamespaces ); //Find the root node in the example data and import it into the InfoPath document. var objExternalXML = objExternalData.selectSingleNode(&quot;my:myFields&quot;); var objReplacementXML = XDocument.DOM.importNode( objExternalXML, true ); //Find the original data in the form and replace it with the example data. var objOriginalXML = XDocument.DOM.selectSingleNode(&quot;my:myFields&quot;); XDocument.DOM.replaceChild( objReplacementXML, objOriginalXML ); } } </li> <li>On the File menu, click Save, and then close the InfoPath form.</li></ol>

Load data by using the OnLoad event
The TwoField.xsn form OnLoad event loads data from the GetData.asp page if no other data is in the form. To load the data, follow these steps: <ol> <li>Click Start, click Run, type the path of the form, and then click OK. The path that you type resembles the following:

http:// / /TwoField.xsn

</li> <li>If you are prompted, click Open.</li></ol>

Notice that InfoPath starts, and the TwoField.xsn form opens. Field1 contains &quot;test field 5&quot;, and Field2 contains &quot;test field 6&quot;. These are the values that GetData.asp returns when its dataRequest parameter is set to &quot;3&quot;.

Load a default form by using ExternalApplication.New
The Default.asp page creates client-side script to automate the ExternalApplication object to start InfoPath and then to create a new form. The new form is based on the XML data that is returned by GetData.asp. To create the new form, click Start, click Run, type the path of the Default.asp page, and then click OK. The path that you type resembles the following:

http:// / / .asp

Note To use this method, you must click Prompt or Enable for the Initialize and script ActiveX controls not marked as safe setting in Internet Explorer.

Notice that InfoPath starts, and the TwoField.xsn form opens. Field1 contains &quot;test field 3&quot;, and Field2 contains &quot;test field 4&quot;. These are the values that GetData.asp returns when its dataRequest parameter is set to &quot;2&quot;.

Load an existing form from an ASP page
The GetData.asp page returns XML in the correct format for InfoPath to open it as a form file. To do this, click Start, click Run, type the path of the GetData.asp page, and then click OK. The path that you type resembles the following:

http:// / /getData.asp?dataRequest=1

Notice that InfoPath starts, and the GetData.asp form opens. Field1 contains &quot;test field 1&quot; and Field2 contains &quot;test field 2&quot;. These are the values that GetData.asp returns when its dataRequest parameter is set to &quot;1&quot;.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Keywords: kbscript kbasp kbhowto KB896451

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.