Use the Filter Data feature in InfoPath
Create a simple Web service XML data source:
- 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://<server>/PopulateCities/Service1.asmx?wsdl, and then click Next.
- In Solution Explorer, right-click PopulateCities, point to Add, and then click Add New Item. The Add New Item dialog box appears.
- In the Templates section, click XML File, type Allcities.xml in the Name box, and then click Open.
- Replace everything with the following code, and then click Save.
="1.0"="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>
- Right-click Service1.asmx, and then click View Code.
- Add the following code sample to the top of the code page.
Imports System.Xml
Imports System.IO
Add the following Web service method to the Service1 class.
// VB.net code
<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
C# Code
[WebMethod()]
public System.Xml.XmlDocument GetCities2()
{
string path = null;
path = Server.MapPath("Allcities.xml");
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(path);
return doc;
}
On the Build menu, click Build Solution.
Create an InfoPath Form
- Start InfoPath.
- On the File menu in InfoPath 2007, click Design a Form Template. On the File menu in InfoPath 2003, click Design a Form.
- Add controls to the new form. To do this, follow these steps:
- 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.
- In the Design Tasks task pane, click Controls.
- In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named
field1
is added to the form. - In Microsoft InfoPath Designer, right-click
field1
, and then click Drop-Down List Box Properties. - 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.
- 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.
- In the Drop-Down List Box Properties dialog box, click OK.
- In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named
field2
is added to the form.
- On the Tools menu, click Data Connections.
- In the Data Connections dialog box, click Add.
- In the Data Connection Wizard dialog box, click Receive data, and then click Next.
- Click Web service, and then click Next.
- In the Location box, type http://<server>/PopulateCities/Service1.asmx?wsdl, and then click Next.
- In the Select an operation list, click
GetCities2
, and then click Next. - In the Data Connection Wizard dialog box, click Finish.
- In the Data Connections dialog box, click Close.
- In InfoPath Designer, right-click field2, and then click Drop-Down List Box Properties.
- 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.
- Click Select XPath to the right of the Entries box. The Select a Field or Group dialog box appears.
- Add a filter to the return data. To do this, follow these steps:
- Expand All Notes, click String, and then click Filter Data. The Filter Data dialog box appears.
- Click Add. The Specify Filter Conditions box appears.
- In the first drop-down list, click Select a Field or Group.
- In the Data Source section, click GetCities (Secondary).
- In the dataFields node, Click Value, and then click OK.
- Click is equal to for the second drop-down list.
- In the third drop-down list, click Select a Field or Group.
- In the Data Source section, click Main.
- Click
field1
, and then click OK. - Click OK to close all open dialog boxes.
- Click Save to save the form.
Test the Form
- 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.
- Click FL, click MI, and then click Unknown from the first drop-down list. Notice that the second drop-down list updates as expected.