Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Cascading Dropdowns in Browser Forms

Cascading Dropdowns in Browser Forms

  • Comments 29
If you are building an InfoPath client-only solution and you need to filter drop-down list boxes, you can simply use the “Filter Data” feature when you set the Entries property for the control. However, since filters are not supported in browser-compatible form templates, how can you accomplish the same functionality?
 
This is where .NET web services can “save the day!” By creating web methods that accept parameters, you can add those web methods as data connections and then pass the selected value from one drop-down list box to the appropriate data connection “queryField”. Once the queryField has been set, simply execute that data connection to retrieve the associated values.
 
To setup this sample, you will need to have access to the SQL Server Northwind sample database and Visual Studio installed on your server.
 
First, let’s create the web service and the two web methods we will use in this sample:
 
Step 1: Open the appropriate web site
 
  1. Launch Visual Studio
  2. From the File menu, select Open and choose Web Site
  3. Select File System and then navigate to: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS
 
NOTE: By choosing to open the LAYOUTS folder, your web service will be available from all provisioned sites. If you want the web service only to be available from a specific site (i.e. the default site) you would want to open: C:\Inetpub\wwwroot\wss\VirtualDirectories\80
 
  1. Click Open
  2. In the Solution Explorer, right-click on the web site and choose New Folder
 
 
  1. Rename this folder to: WebServices
  2. Because you may have multiple web services, let’s add a sub folder here that is specific to our web service:
    1. Right-click on WebServices and choose New Folder
    2. Rename this folder to: NorthwindTables
 
 
Step 2: Create the web service
 
  1. Right-click on NorthwindTables and choose Add New Item
  2. From the Visual Studio installed templates list choose Web Service
  3. In the Name box, rename this to: NorthwindTable.asmx
 
 
  1. Uncheck the option “Place code in a separate file” and click Add
 
 
Step 3: Add the web methods
 
NOTE: For this sample, it is assumed the SQL Server database is installed on the same Microsoft Office SharePoint Server. 
 
  1. Add the following “using” declarations at the top of your code page: 

using
System.Data;
using System.Data.SqlClient;
 
  1. Add the following web method to retrieve the CustomerID values from the Customers table in the Northwind database:
 
[WebMethod]
public DataSet GetCustomers() {
            // Create a SQL connection to the Northwind sample database
            SqlConnection cn = new SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind");
 
            // Create data adapter object passing it the SELECT
            // statement to retrieve the customer ID values
            SqlDataAdapter da = new SqlDataAdapter("SELECT Customers.CustomerID FROM Customers Order By CustomerID", cn);
 
            // Create a dataset object to store the data
            DataSet ds = new DataSet();
 
            // Open the connection
            cn.Open();
 
            // Fill the dataset
            da.Fill(ds, "Customers");
 
            // Clean up
            cn.Close();
            cn = null;
            da = null;
            
            return ds;
            }
 
  1. Add the following web method to retrieve the associated orders for the selected customer:

[WebMethod]
public DataSet GetOrdersForSelectedCustomer(string strCustID) {
            // Create a SQL connection to the Northwind sample database
            SqlConnection cn = new SqlConnection("Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind");
 
            // Create a string variable for the modified SQL statement
            string strOrdersSQL = "";
 
            // Create a string variable for the default SQL statement
            string strOrdersOrigSQL = "SELECT * FROM Orders";
 
            // Some of the customer ID values contain apostrophe's - we need
            // to replace them with two single quotation marks so that all
            // single quotation marks in the CustomerID are parsed correctly.
            strCustID = strCustID.Replace("'", "''");
 
            // Concatenate the default SQL statement with the "Where" clause
            // and add an OrderBy clause
            strOrdersSQL = strOrdersOrigSQL + " Where CustomerID Like '%" + strCustID + "%' Order By OrderID";
 
            // Create data adapter object passing it the SELECT statement
            // to retrieve the OrderID values
            SqlDataAdapter daOrders = new SqlDataAdapter(strOrdersSQL, cn);
 
            // Create a dataset object to store the data
            DataSet Ds = new DataSet();
 
            // Open the connection
            cn.Open();
 
            // Fill the DataSet
            daOrders.Fill(Ds, "Orders");
 
            // Clean up
            cn.Close();
            cn = null;
            daOrders = null;
                 
            return Ds;
}
 
  1. Build and save the project
 
 
Step 4: Test the web methods
 
NOTE: The Identity account of the Application Pool for the web site where this web service is published will need to have access to the SQL Server database.
 
  1. Open a browser and navigate to: http://<server>/_layouts/WebServices/NorthwindTables/NorthwindTables.asmx (replace <server> with the name of your server)
  2. You should see the two web methods created above along with the default HelloWorld web method:
 
 
  1. Click the GetCustomers link and then click Invoke – this should return a list of the CustomerID values
  2. Click the GetOrdersForSelectedCustomer link, in the strCustID box enter: BERGS and then click Invoke – this should return a list of only those OrderID values for BERGS
 
 
Step 5: Create the InfoPath form
 
  1. Design a new, blank, browser-compatible InfoPath Form Template
  2. Add a drop-down list box to the view and modify the name to: SelectCustomer
  3. Add another drop-down list box to the view and modify the name to: SelectOrder
 
 
 
  1. Add a new “receive data” data connection to the NorthwindTables web service for each of the web methods created above as follows:
    1. GetCustomers:
      • Enable the option “Automatically retrieve data when the form is opened”
    2. GetOrdersForSelectedCustomer:
      • Use ALFKI as the sample value for the strCustID parameter when prompted in the Data Connection Wizard
      • Uncheck the option “Automatically retrieve data when the form is opened”
  2. Set the Data source for SelectCustomer to the GetCustomers data connection and use the CustomerID field for both the Value and Display name properties
  3. Set the Data source for SelectOrder to the GetOrdersForSelectedCustomer data connection and use the OrderID field for both the Value and Display name properties
  4. Create a Rule on SelectCustomer with the following actions:
    1. Set a field’s value: Set the SelectOrder field to nothing (e.g. leave the Value blank)
    2. Set a field’s value: Set the parameter value (strCustID) for the GetOrdersForSelectedCustomer data connection to the SelectCustomer field
    3. Query the GetOrdersForSelectedCustomer data connection
 
 
  1. Save the form locally as FilteredDrop-downs_IPFS.XSN
 
 
Step 6: Publish the form
 
  1. Publish the form to a server running InfoPath Form Services
  2. Navigate to the form library where the form was published and click the New button
  3. From SelectCustomer choose BERGS
  4. Click SelectOrder – only those orders for BERGS are displayed
  5. Select a different customer – notice the orders have also changed
 
Scott Heim
Support Engineer
Leave a Comment
  • Please add 3 and 8 and type the answer here:
  • Post
  • Encontrei num post do blog do JOPX , este conjunto de recursos sobre InfoPath 2007. General resources

  • InfoPath 2007 resources General resources InfoPath General Overview InfoPath team Blog Designing Form

  • I am a newbie in infopath and I have surfed almost every article to get my dependent dropdowns working! Most of the sites ask us to refer this URL.. This is a great article.

    I would like to add my comments for populating cascading dropdowns. You can also use repeating tables for this purpose. There is no need to call a webservice on change of each and every dropdown. Just write your logic in the codebehind and populate a repeating table onchange of a dropdown. Bind the dependent dropdown to this repeating table. Ensure to clear the reapeating table on next onchange event of your first dropdown!

    -Bhavana Bhat

  • Hi Bhavana,

    Thank you for your suggestion as this is certainly another option! One of the main reasons I used a web service in this manner is that the InfoPath form template (for this functionality) requires no code - as such, the form template does not need to be "Administrator" deployed.

    Scott Heim

  • When designing Microsoft Office InfoPath form templates, filtering can be used to limit the options that are displayed to users in certain controls. This out-of-the-box functionality can be used in list boxes, drop-down list boxes, combo boxes, repeating

  • When designing Microsoft Office InfoPath form templates, filtering can be used to limit the options that are displayed to users in certain controls. However, if you are designing an Office InfoPath 2007 form template for a browser scenario, it should

  • One of our customers here asked us to develop a simple InfoPath form, including dependant dropdown functionality.

  • This is a great example, but what do you do if the controls are in a repeating table? The dependant control (SelectOrder) is "filtered" for every row in the repeating table based on the selected value of SelectCustomer in the current row.

  • Hi Crisch,

    Are you referring to the behavior that when you click the SelectOrder box in an existing row that it only contains the values for the "newly added" row? If so, then correct - this would be expected behavior for this sample.

    If you wanted to be able to "refresh" the list when you move to a previously created row, you could add a new column with a button to the repeating table with a Rule that sets the query field and then queries the connection. The only other option may be using managed code but I have not explored that option as I was attempting to show how to accomplish this without using code in the InfoPath form template.

    Scott

  • Hi, I have to admit that this post is long overdue. In the last two weeks, I came across a lot of people

  • Hi, I have to admit that this post is long overdue. In the last two weeks, I came across a lot of people

  • The InfoPath Team Blog has a great article on how to implement cascading dropdowns in InfoPath Forms

  • Using managed code, it is possible to set the dropdown values dynamically even in the repeating context. The above URL contains an article on how to do it with a sample.

  • Hello

    You are the hero, i was trin to do that from three days without filters but ....

    You solved the problem.

    Thanks alot.

Page 1 of 2 (29 items) 12