How to dynamically populate a drop-down list by calling a Web service in InfoPath 2003 (826994)



The information in this article applies to:

  • 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 2003.

Microsoft Office InfoPath 2003 Service Pack 1 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.

back to the top

MORE INFORMATION

Use a script to filter data in Office InfoPath 2003

back to the top

Create a simple Web service

  1. Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project.
  2. From the list of Project Types, click Visual Basic Projects. From the list of Templates, click ASP.NET Web Service.
  3. In the Location box, type http://<SERVER>/PopulateCities.

    The placeholder <SERVER> is the name of your Web server.

    Click OK.
  4. Right-click Service1.asmx.vb, and then click View Code.
  5. Add the following Web service method to the Service1 class.
    <WebMethod()> _
    Public Function GetCities(ByVal sState As String) As String()
        If sState = "FL" Then
            GetCities = New String() {"Coral Gables", "Miami", "Orlando", "Tallahassee"}
        ElseIf sState = "MI" Then
            GetCities = New String() {"Detroit", "Flint", "Lansing", "Livonia"}
        Else
            GetCities = New String() {"Unknown State"}
        End If
    End Function
  6. On the Build menu, click Build Solution.
back to the top

Create an InfoPath form

  1. Start InfoPath. On the File menu, click Design a Form.
  2. To add controls to your new form, follow these steps:
    1. In the Design a Form task pane, click New Blank Form.
    2. In the Design Tasks task pane, click Controls.
    3. In the Insert Controls list, click Drop-Down List Box.

      A new drop-down list that is named field1 is added to the form.
    4. In Microsoft InfoPath Designer, right-click field1, and then click Drop-Down List Box Properties.
    5. In the Drop-Down List Properties dialog box, click Add. In the Value box of the Add Choice dialog box, type FL, and then click OK.
    6. In the Drop-Down List Properties dialog box, click Add. In the Value box of the Add Choice dialog box, type MI, and then click OK.
    7. In the Drop-Down List Box Properties dialog box, click OK.
    8. In the Insert Controls list, click Drop-Down List Box.

      A new drop-down list that is named field2 is added to the form.
  3. On the Tools menu, click Secondary Data Sources. In the Secondary Data Sources dialog box, click Add.
  4. Click Web Service, and then click Next.
  5. In the Location box, type http://<SERVER>/PopulateCities/Service1.asmx?wsdl. Click Next.
  6. In the Select an operation list, click GetCities, and then click Next.
  7. In the Data Source Setup Wizard dialog box, click Set Value. In the Set Value dialog box, type FL, and then click OK.
  8. In the Data Source Setup Wizard dialog box, click Next, and then click Finish.
  9. In the Secondary Data Sources dialog box, click Close.
  10. In the Editor window, right-click field2, and then click Drop-Down List Box Properties.
  11. In the List box entries list, click Look up in a database, Web service, or file.
  12. Click Select XPath.

    Select XPath is to the right of the Entries box.

    Expand all nodes, and then click string. Click OK.
  13. In the Drop-Down List Box Properties dialog box, click OK.
back to the top

Add a script to query the Web service when the first drop-down list changes

  1. In the Designer window, right-click field1, and then click Drop-Down List Box Properties.
  2. In the Validation list, click Data Validation.
  3. In the Events drop-down list, click OnAfterChange, and then click Edit.

    Microsoft Script Editor starts.
  4. Add the following code sample after the code comment.
    A field change has occurred...
    VBScript
    UpdateCities
    XDocument.View.ForceUpdate
    JScript
    UpdateCities();
    XDocument.View.ForceUpdate();
  5. Add the UpdateCities procedure to the script file.

    VBScript
    Sub UpdateCities
    	Dim fieldValue
    	set fieldValue = XDocument.DOM.selectSingleNode( "/my:myFields/my:field1" )
    
    	'Get a reference to the SDS bound to the Web Service.	
    	Dim theDataObject
    	set theDataObject = XDocument.DataObjects.Item("GetCities")
    
    	'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"SelectionNamespaces", _
    		"xmlns:dfs=""http://schemas.microsoft.com/office/infopath/2003/dataFormSolution"" " &_
                    "xmlns:s0=""http://tempuri.org/"""
    
    	Dim queryValue
    	set queryValue = theDataObject.DOM.selectSingleNode( _
    		"/dfs:myFields/dfs:queryFields/s0:GetCities/s0:sState" )
    
    	queryValue.text = fieldValue.text
    	theDataObject.Query	
    End Sub
    
    JScript
    function UpdateCities()
    {
    
    	var fieldValue = XDocument.DOM.selectSingleNode( "/my:myFields/my:field1");
    
    	//Get a reference to the SDS bound to the Web Service.	
    	var theDataObject = XDocument.DataObjects.Item("GetCities");
    
    	//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("SelectionNamespaces", 
    		'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" ' +
                    'xmlns:s0="http://tempuri.org/"');
    
    	var queryValue = theDataObject.DOM.selectSingleNode( 
    		"/dfs:myFields/dfs:queryFields/s0:GetCities/s0:sState" );
    
    	queryValue.text = fieldValue.text;
    	theDataObject.Query();	
    }
  6. Switch back to InfoPath. In the Data Validation dialog box, click OK. In the Drop-Down List Box Properties dialog box, click OK.
  7. On the Tools menu, click Script, and then click On Load Event.

    Script Editor starts, and the XDocument_OnLoad event handler is added.
  8. Add the following code sample to the implementation of XDocument_OnLoad.

    VBScript
    UpdateCities

    JScript
    UpdateCities();
  9. On the File menu, click Save, and then exit Script Editor.
back to the top

Test Your Form

  1. On the File menu, point to Preview Form, and then click Default.

    When the form appears, Select appears in the first drop-down list.
  2. Display the list in the second drop-down list.

    Notice that Unknown State appears as you expect.
  3. Click FL, and then click MI in the first drop-down list.

    Notice that the second drop-down list updates as you expect.
back to the top

Use the new filter data feature in Office InfoPath 2003 Service Pack 1

back to the top

Create a simple Web service XML data source

  1. Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project.
  2. From the list of Project Types, click Visual Basic Projects. From the list of Templates, click ASP.NET Web Service.
  3. In the Location box, type http://<SERVER>/PopulateCities/Service1.asmx?wsdl. Click Next.
  4. Right-click PopulateCities in Solution Explorer, point to Add, and then click Add New Item.

    The Add New Item dialog box appears.
  5. Click XML File in the Templates section, type Allcities.xml in the Name box, and then click Open.
  6. Replace everything with the following code, and then click Save.
    <?xml version="1.0" encoding="utf-8" ?>
    <States>
    	<State value="FL">
    		<ArrayOfString>
    			<string>Coral Gables</string>
    			<string>Miami</string>
    			<string>Orlando</string>
    			<string>Tallahassee</string>
    		</ArrayOfString>
    	</State>
    	<State value="MI">
    		<ArrayOfString>
    			<string>Detroit</string>
    			<string>Flint</string>
    			<string>Lansing</string>
    			<string>Livonia</string>
    		</ArrayOfString>
    	</State>
    	<State value="Unknown">
    		<ArrayOfString>
    			<string>Unknown State</string>
    		</ArrayOfString>
    	</State>
    </States>
  7. Right-click Service1.asmx, and then click View Code.
  8. Add the following code sample to the top of the Code page.
    Imports System.Xml
    Imports System.IO
    
  9. 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("AllCities.xml")
    
        Dim doc As New System.Xml.XmlDocument
        doc.Load(path)
    
        GetCities2 = doc
    End Function
  10. On the Build menu, click Build Solution.
back to the top

Create an InfoPath form

  1. Start InfoPath. On the File menu, click Design a Form.
  2. To add controls to your new form, follow these steps:
    1. In the Design a Form task pane, click New Blank Form.
    2. In the Design Tasks task pane, click Controls.
    3. In the Insert Controls list, click Drop-Down List Box.

      A new drop-down list that is named field1 is added to the form.
    4. In InfoPath Designer, right-click field1, and then click Drop-Down List Box Properties.
    5. In the Drop-Down List Properties dialog box, click Add. In the Value box of the Add Choice dialog box, type FL, and then click OK.
    6. Repeat step e, add value "MI" and value "Unknown" to the Entry list.
    7. In the Drop-Down List Box Properties dialog box, click OK.
    8. In the Insert Controls list, click Drop-Down List Box.

      A new drop-down list that is named field2 is added to the form.
  3. On the Tools menu, click Data Connections. In the Data Connections dialog box, click Add.
  4. In the Data Connection Wizard dialog box, click Receive data, and then click Next.
  5. Click Web service, and then click Next.
  6. In the Location box, type http://<SERVER>/PopulateCities/Service1.asmx?wsdl. Click Next.
  7. In the Select an operation list, click GetCities2, and then click Next.
  8. In the Data Connection Wizard dialog box, click Finish.
  9. In the Data Connections dialog box, click Close.
  10. In InfoPath Designer, right-click field2, and then click Drop-Down List Box Properties.
  11. In the List box entries list, click Look up values in a data connection to a database, Web service, file, or SharePoint library or list.
  12. Click Select XPath that is to the right of the Entries box.

    The Select a Field or Group dialog box appears.
  13. To add a filter to the return data, follow this steps:
    1. Expand All Notes, click String, and then click Filter Data.

      The Filter Data dialog box appears.
    2. Click Add.

      The Specify Filter Conditions box appears.
    3. Click Select a Field or Group in the first drop-down list. Click GetCities (Secondary) in the Data Source section. Click Value in the dataFields node, and then click OK.
    4. Click is equal to for the second drop-down list.
    5. Click Select a Field or Group in the third drop-down list. Click Main in the Data Source section. Click field1, and then click OK.
  14. Click OK to close all open dialog boxes.
  15. Click Save to save your form.
back to the top

Test your form

  1. On the toolbar, click Preview Form.
  2. click FL, click MI, and then click Unknown from the first drop-down list.

    Notice that the second drop-down list updates as you expect.
back to the top

Use the new rules feature in Office InfoPath 2003 Service Pack 1

back to the top

Create a simple Web service

Use the same Web service that is mentioned in the "Use a script to filter data in Office InfoPath 2003" section.

back to the top

Create an InfoPath form

  1. Start InfoPath. On the File menu, click Design a Form.
  2. To add controls to your new form, follow these steps:
    1. In the Design a Form task pane, click New Blank Form.
    2. In the Design Tasks task pane, click Controls.
    3. In the Insert Controls list, click Drop-Down List Box.

      A new drop-down list that is named field1 is added to the form.
    4. In InfoPath Designer, right-click field1, and then click Drop-Down List Box Properties.
    5. In the Drop-Down List Properties dialog box, click Add. In the Value box of the Add Choice dialog box, type FL, and then click OK.
    6. Repeat step e. Add the value "MI" and the value "Unknown" to the Entry list.
    7. In the Drop-Down List Box Properties dialog box, click OK.
    8. In the Insert Controls list, click Drop-Down List Box.

      A new drop-down list that is named field2 is added to the form.
  3. On the Tools menu, click Data Connections. In the Data Connections dialog box, click Add.
  4. In the Data Connection Wizard dialog box, click Receive data, and then click Next.
  5. Click Web service, and then click Next.
  6. In the Location box, type http://<SERVER>/PopulateCities/Service1.asmx?wsdl. Click Next.
  7. In the Select an operation list, click GetCities, and then click Next.
  8. In the Data Connection Wizard dialog box, click Finish.
  9. In the Data Connections dialog box, click Close.
  10. In InfoPath Designer, right-click field2, and then click Drop-Down List Box Properties.
  11. In the List box entries list, click Look up values in a data connection to a database, Web service, file, or SharePoint library or list.
  12. Click Select XPath that is to the right of the Entries box. The Select a Field or Group dialog box appears. Expand all nodes, and then click string. Click OK.
  13. To add rules to filter the return data, follow these steps:
    1. In the Designer window, right-click the drop-down list that is named field1, and then click Drop-Down List Box Properties.
    2. In the Validation and Rules list, click Rules.

      The Rules dialog box appears.
    3. Click Add, and then click Add Action.
    4. Click Set a field's value in the drop-down list in the Action section.
    5. Click Select XPath that is to the right of the Field box. Click the State node under queryFields in the GetCities (secondary) data source. Click OK.
    6. Click fx that is to the right of the Value box. Click Insert Field or Group. Click the field1 node under myFields in the Main data source. Click OK.
    7. Click OK to return to the Rules dialog box.
    8. Click Add, and then click Add Action.
    9. Click Query using a data connection in the drop-down list of the Action section. Click GetCities in the Data Connection section. Click OK.
  14. Click OK to close all open dialog boxes.
  15. Click Save to save your form.
back to the top

Test Your Form

  1. On the toolbar, click Preview Form.
  2. In the first drop-down list, click FL, click MI, and then click Unknown.

    Notice that the second drop-down list updates as you expect.
back to the top

REFERENCES

For additional information about how to obtain the latest service pack for Microsoft Office, click the following article number to view the article in the Microsoft Knowledge Base:

870924 How to obtain the latest service pack for Office 2003

back to the top

Modification Type:MinorLast Reviewed:11/12/2004
Keywords:kbhowto kbWebServices KB826994 kbAudDeveloper