Microsoft KB Archive/826994

= How to dynamically populate a drop-down list by calling a Web service in InfoPath =

Article ID: 826994

Article Last Modified on 3/23/2007

-

APPLIES TO


 * Microsoft Office InfoPath 2007
 * Microsoft Office InfoPath 2003
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

-



INTRODUCTION
This article describes how to call a Web service to dynamically populate a drop-down list that is based on the selection in a second drop-down list in Microsoft Office InfoPath.

Microsoft Office InfoPath adds a new data filter feature. This new feature lets you use the user interface to specify a value in an input field. Then, that value is passed to a Web service that in turn populates a drop-down list with the results that match the value in the input field. This article also describes how to call a Web service to dynamically populate a drop-down list that is based on the selection in a second drop-down list by using the new data filter feature.



Create a simple Web service
 Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. In the Project Types list, select Visual Basic Projects. In the Templates list, select ASP.NET Web Service. In the Location box, type http:// /PopulateCities, and then click OK.

Note The placeholder  represents the name of the Web server. Right-click Service1.asmx.vb, and then click View Code.  Add the following Web service method to the Service1 class. <WebMethod> _ Public Function GetCities(ByVal sState As String) As String If sState = &quot;FL&quot; Then GetCities = New String {&quot;Coral Gables&quot;, &quot;Miami&quot;, &quot;Orlando&quot;, &quot;Tallahassee&quot;} ElseIf sState = &quot;MI&quot; Then GetCities = New String {&quot;Detroit&quot;, &quot;Flint&quot;, &quot;Lansing&quot;, &quot;Livonia&quot;} Else GetCities = New String {&quot;Unknown State&quot;} End If End Function </li> On the Build menu, click Build Solution.</li></ol>

Create an InfoPath form
<ol> Start InfoPath.</li> On the File menu in InfoPath 2007, click Design a Form Template. On the File menu in InfoPath 2003, click Design a Form.</li> Add controls to the new form. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> In the Design a Form Template task pane in InfoPath 2007, click Blank, and then click OK. In the Design a Form task pane in InfoPath 2003, click New Blank Form.</li> In the Design Tasks task pane, click Controls.</li> In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named field1 is added to the form.</li> In Microsoft InfoPath Designer, right-click field1, and then click Drop-Down List Box Properties.</li> In the Drop-Down List Properties dialog box, click Add.</li> In the Value box of the Add Choice dialog box, type FL, and then click OK.</li> In the Drop-Down List Properties dialog box, click Add.</li> In the Value box of the Add Choice dialog box, type MI, and then click OK.</li> In the Drop-Down List Box Properties dialog box, click OK.</li> In the Insert Controls list, click Drop-Down List Box. A new drop-down list that is named field2 is added to the form.</li></ol> </li> In InfoPath 2007, follow these steps: <ol style="list-style-type: lower-alpha;"> On the Tools menu, click Data Connection.</li> <li>In the Data Connection Wizard, click Create a new connection to, click Receive data, and then click Next.</li></ol>

In InfoPath 2003, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>On the Tools menu, click Secondary Data Sources.</li> <li>In the Secondary Data Sources dialog box, click Add.</li></ol> </li> <li>Click Web Service, and then click Next.</li> <li>In the Location box, type http:// /PopulateCities/Service1.asmx?wsdl, and then click Next.</li> <li>In the Select an operation list, select GetCities, and then click Next.</li> <li>In the Data Source Setup Wizard dialog box, click Set Value. In the Set Value dialog box, type FL, and then click OK.</li> <li>In the Data Source Setup Wizard dialog box, click Next, and then click Finish.</li> <li>In the Secondary Data Sources dialog box, click Close.</li> <li>In the Editor window, right-click field2, and then click Drop-Down List Box Properties.</li> <li>In the List box entries list, select Look up in a database, Web service, or file.</li> <li>Click Select XPath to the right of the Entries box.</li> <li>Expand all nodes, and then click string. Click OK.</li> <li>In the Drop-Down List Box Properties dialog box, click OK.</li></ol>

Add a script to query the Web service when the first drop-down list changes
<ol> <li>For InfoPath 2007, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the Designer window, right-click field1.</li> <li>Click Programming, and then click On After Change Event. Microsoft Script Editor starts.</li></ol>

For InfoPath 2003, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the Designer window, right-click field1, and then click Drop-Down List Box Properties.</li> <li>In the Validation list, click Data Validation.</li> <li>In the Events list, click OnAfterChange, and then click Edit. Microsoft Script Editor starts.</li></ol> </li> <li> Locate the following code comment. A field change has occurred... </li> <li> Add the following code sample after the code comment.

VBScript UpdateCities XDocument.View.ForceUpdate JScript UpdateCities; XDocument.View.ForceUpdate; </li> <li> Add the UpdateCities procedure to the script file.

VBScript Sub UpdateCities Dim fieldValue set fieldValue = XDocument.DOM.selectSingleNode( &quot;/my:myFields/my:field1&quot; )

'Get a reference to the SDS bound to the Web Service. Dim theDataObject set theDataObject = XDocument.DataObjects.Item(&quot;GetCities&quot;)

'Set the SelectionNamespaces so that you can find the correct field. 'Note: If the Web service was created with Visual Studio .NET 2003, the xmlns:s0 namespace is        'http://tempuri.org/PopulateCities/Service1 theDataObject.DOM.setProperty&quot;SelectionNamespaces&quot;, _ &quot;xmlns:dfs=&quot;&quot;http://schemas.microsoft.com/office/infopath/2003/dataFormSolution&quot;&quot; &quot; &_ &quot;xmlns:s0=&quot;&quot;http://tempuri.org/&quot;&quot;&quot;

Dim queryValue set queryValue = theDataObject.DOM.selectSingleNode( _       &quot;/dfs:myFields/dfs:queryFields/s0:GetCities/s0:sState&quot; )

queryValue.text = fieldValue.text theDataObject.Query End Sub JScript function UpdateCities {

var fieldValue = XDocument.DOM.selectSingleNode( &quot;/my:myFields/my:field1&quot;);

//Get a reference to the SDS bound to the Web Service. var theDataObject = XDocument.DataObjects.Item(&quot;GetCities&quot;);

//Set the SelectionNamespaces so that you can find the correct field. // Note: If the Web service was created with Visual Studio .NET 2003, the xmlns:s0 namespace is     //  http://tempuri.org/PopulateCities/Service1 theDataObject.DOM.setProperty(&quot;SelectionNamespaces&quot;,        'xmlns:dfs=&quot;http://schemas.microsoft.com/office/infopath/2003/dataFormSolution&quot; ' +                'xmlns:s0=&quot;http://tempuri.org/&quot;');

var queryValue = theDataObject.DOM.selectSingleNode(        &quot;/dfs:myFields/dfs:queryFields/s0:GetCities/s0:sState&quot; );

queryValue.text = fieldValue.text; theDataObject.Query; } </li> <li>Switch back to InfoPath. In the Data Validation dialog box, click OK.</li> <li>In the Drop-Down List Box Properties dialog box, click OK.</li> <li>On the Tools menu in InfoPath 2007, click Programming, and then click On Load Event. On the Tools menu in InfoPath 2003, click Script, and then click On Load Event.

Script Editor starts, and the XDocument_OnLoad event handler is added.</li> <li> Add the following code sample to the implementation of the XDocument_OnLoad event handler.

VBScript UpdateCities JScript UpdateCities; </li> <li>On the File menu, click Save, and then exit Script Editor.</li></ol>

Test the form

 * 1) On the File menu in InfoPath 2007, point to Preview, and then click Form. On the File menu in InfoPath 2003, point to Preview Form, and then click Default.

When the form appears, Select appears in the first drop-down list.
 * 1) Display the list in the second drop-down list. Notice that Unknown State appears as expected.
 * 2) Click FL, and then click MI in the first drop-down list. Notice that the second drop-down list updates as expected.

Create a simple Web service XML data source
<ol> <li>Start Microsoft Visual Studio .NET.</li> <li>On the File menu, click New, and then click Project.</li> <li>In the Project Types list, select Visual Basic Projects.</li> <li>In the Templates list, select ASP.NET Web Service.</li> <li>In the Location box, type http:// /PopulateCities/Service1.asmx?wsdl, and then click Next.</li> <li>In Solution Explorer, right-click PopulateCities, point to Add, and then click Add New Item. The Add New Item dialog box appears.</li> <li>In the Templates section, click XML File, type Allcities.xml in the Name box, and then click Open.</li> <li> Replace everything with the following code, and then click Save. <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <States> <State value=&quot;FL&quot;> <ArrayOfString> Coral Gables Miami Orlando Tallahassee </ArrayOfString> </State> <State value=&quot;MI&quot;> <ArrayOfString> Detroit Flint Lansing Livonia </ArrayOfString> </State> <State value=&quot;Unknown&quot;> <ArrayOfString> Unknown State </ArrayOfString> </State> </States> </li> <li>Right-click Service1.asmx, and then click View Code.</li> <li> Add the following code sample to the top of the code page. Imports System.Xml Imports System.IO </li> <li> Add the following Web service method to the Service1 class. <WebMethod> _ Public Function GetCities2 As System.Xml.XmlDocument Dim path As String path = Server.MapPath(&quot;AllCities.xml&quot;)

Dim doc As New System.Xml.XmlDocument doc.Load(path)

GetCities2 = doc End Function </li> <li>On the Build menu, click Build Solution.</li></ol>

Create an InfoPath form
<ol> <li>Start InfoPath.</li> <li>On the File menu in InfoPath 2007, click Design a Form Template. On the File menu in InfoPath 2003, click Design a Form.</li> <li>Add controls to the new form. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the Design a Form Template task pane in InfoPath 2007, click Blank, and then click OK. In the Design a Form task pane in InfoPath 2003, click New Blank Form.</li> <li>In the Design Tasks task pane, click Controls.</li> <li>In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named field1 is added to the form.</li> <li>In Microsoft InfoPath Designer, right-click field1, and then click Drop-Down List Box Properties.</li> <li>In the Drop-Down List Properties dialog box, click Add.</li> <li>In the Value box of the Add Choice dialog box, type FL, and then click OK.</li> <li>In the Drop-Down List Properties dialog box, click Add.</li> <li>In the Value box of the Add Choice dialog box, type MI, and then click OK.</li> <li>In the Drop-Down List Box Properties dialog box, click OK.</li> <li>In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named field2 is added to the form.</li></ol> </li> <li>On the Tools menu, click Data Connections.</li> <li>In the Data Connections dialog box, click Add.</li> <li>In the Data Connection Wizard dialog box, click Receive data, and then click Next.</li> <li>Click Web service, and then click Next.</li> <li>In the Location box, type http:// /PopulateCities/Service1.asmx?wsdl, and then click Next.</li> <li>In the Select an operation list, click GetCities2, and then click Next.</li> <li>In the Data Connection Wizard dialog box, click Finish.</li> <li>In the Data Connections dialog box, click Close.</li> <li>In InfoPath Designer, right-click field2, and then click Drop-Down List Box Properties.</li> <li>In the List box entries list in InfoPath 2007, select Look up values in the form's data source. In the List box entries list in InfoPath 2003, select Look up values in a data connection to a database, Web service, file, or SharePoint library or list.</li> <li>Click Select XPath to the right of the Entries box. The Select a Field or Group dialog box appears.</li> <li>Add a filter to the return data. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Expand All Notes, click String, and then click Filter Data. The Filter Data dialog box appears.</li> <li>Click Add. The Specify Filter Conditions box appears.</li> <li>In the first drop-down list, click Select a Field or Group.</li> <li>In the Data Source section, click GetCities (Secondary).</li> <li>In the dataFields node, Click Value, and then click OK.</li> <li>Click is equal to for the second drop-down list.</li> <li>In the third drop-down list, click Select a Field or Group.</li> <li>n the Data Source section, click Main.</li> <li>Click field1, and then click OK.</li></ol> </li> <li>Click OK to close all open dialog boxes.</li> <li>Click Save to save the form.</li></ol>

Test the form

 * 1) On the File menu in InfoPath 2007, point to Preview, and then click Form. On the File menu in InfoPath 2003, point to Preview Form, and then click Default.
 * 2) Click FL, click MI, and then click Unknown from the first drop-down list. Notice that the second drop-down list updates as expected.

Create a simple Web service
Use the same Web service that is mentioned in the &quot;Use a script to filter data in InfoPath&quot; section.

Create an InfoPath form
<ol> <li>Start InfoPath.</li> <li>On the File menu in InfoPath 2007, click Design a Form Template. On the File menu in InfoPath 2003, click Design a Form.</li> <li>Add controls to the new form. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the Design a Form Template task pane in InfoPath 2007, click Blank and then click OK. In the Design a Form task pane in InfoPath 2003, click New Blank Form.</li> <li>In the Design Tasks task pane, click Controls.</li> <li>In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named field1 is added to the form.</li> <li>In Microsoft InfoPath Designer, right-click field1, and then click Drop-Down List Box Properties.</li> <li>In the Drop-Down List Properties dialog box, click Add.</li> <li>In the Value box of the Add Choice dialog box, type FL, and then click OK.</li> <li>In the Drop-Down List Properties dialog box, click Add.</li> <li>In the Value box of the Add Choice dialog box, type MI, and then click OK.</li> <li>In the Drop-Down List Box Properties dialog box, click OK.</li> <li>In the Insert Controls list, click Drop-Down List Box. A new drop-down list that is named field2 is added to the form.</li></ol> </li> <li>On the Tools menu, click Data Connections.</li> <li>In the Data Connections dialog box, click Add.</li> <li>In the Data Connection Wizard dialog box, click Receive data, and then click Next.</li> <li>Click Web service, and then click Next.</li> <li>In the Location box, type http:// /PopulateCities/Service1.asmx?wsdl, and then click Next.</li> <li>In the Select an operation list, select GetCities, and then click Next.</li> <li>In the Data Connection Wizard dialog box, click Finish.</li> <li>In the Data Connections dialog box, click Close.</li> <li>In InfoPath Designer, right-click field2, and then click Drop-Down List Box Properties.</li> <li>In the List box entries list in InfoPath 2007, select Look up values in the form's data source. In the List box entries list in InfoPath 2003, select Look up values in a data connection to a database, Web service, file, or SharePoint library or list.</li> <li>Click Select XPath to the right of the Entries box. The Select a Field or Group dialog box appears.</li> <li>Expand all nodes, and then click string.</li> <li>Click OK.</li> <li>Add rules to filter the return data. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the Designer window, right-click the drop-down list that is named field1, and then click Drop-Down List Box Properties.</li> <li>In the Validation and Rules list, click Rules. The Rules dialog box appears.</li> <li>Click Add, and then click Add Action.</li> <li>In the drop-down list in the Action section, select Set a field’s value.</li> <li>Click Select XPath to the right of the Field box.</li> <li>Under queryFields in the GetCities (secondary) data source, click the State node, and then click OK.</li> <li>Click fx to the right of the Value box.</li> <li>Click Insert Field or Group.</li> <li>Under myFields in the Main data source, click the field1 node, and then click OK.</li> <li>Click OK to return to the Rules dialog box.</li> <li>Click Add, and then click Add Action.</li> <li>In the drop-down list of the Action section, click Query using a data connection.</li> <li>In the Data Connection section, click GetCities, and then click OK.</li></ol> </li> <li>Click OK to close all open dialog boxes.</li> <li>Click Save to save the form.</li></ol>

Test the form

 * 1) Start InfoPath.
 * 2) On the File menu in InfoPath 2007, click Design a Form Template. On the File menu in InfoPath 2003, click Design a Form.
 * 3) In the first drop-down list, click FL, click MI, and then click Unknown. Notice that the second drop-down list updates as expected.

<div class="references_section">