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 8 and 2 and type the answer here:
  • Post
  • Hello

    I have q question.

    What if i have 3 dropdown lists. I applied same rule to 1est and 2nd as u did at "SelectCustomer " but its not working, it shows data in first ddl, but no data is selected in 2nd ddl.

    Pleaser help

    Thanks

  • I solved the above problem it was my mistek. Thanks agin for this article

  • u How to do custom themes for MOSS http://www.sharepointblogs.com/tigirry/archive/2007/07/03/custom-site-theme-for-sharepoint-2007-moss-2007-and-wss-3-0.aspx

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

  • Excellent article!  Thanks so much.  I needed to populate cascading dropdowns from SharePoint lists in the main site collection, so I just replaced the web service file with this:

    <code>

    <%@ WebService Language="C#" Class="ListAccess.ListAccessService" %>

    <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>

    using System;

    using System.Web;

    using System.Web.Services;

    using System.Web.Services.Protocols;

    using Microsoft.SharePoint;

    using System.Xml;

    namespace ListAccess

    {

       [WebService(Namespace = "http://localhost/ListAccess", Description = "Provides access to SharePoint Lists")]

       class ListAccessService : WebService

       {

           [WebMethod(Description = @"Gets filtered items from the specified list.  

    Be sure to use the internal name for the filterField.")]

           public XmlDocument GetListItems(string listName, string filterField, string filterValue)

           {

               XmlDocument returnDoc = new XmlDocument();

               using (SPSite site = SPContext.Current.Site)

               {

                   using (SPWeb web = site.RootWeb)

                   {

                       SPList list = web.Lists[listName];

                       string queryText = String.Format(@"<Where>

    <Eq>

    <FieldRef Name=""{0}"" />

    <Value Type=""Text"">{1}</Value>

    </Eq>

    </Where>", filterField, filterValue);

                       SPQuery query = new SPQuery();

                       query.Query = queryText;

                       SPListItemCollection items = list.GetItems(query);

                       returnDoc.LoadXml(items.Xml);

                   }

               }

               return returnDoc;

           }

       }

    }

    </code>

  • Thanks Elliott!

  • Hi,

    Is there anywhere a walkthrough for this procedure? I'm quite a newbe on this, an on step 2 visual web developpers 2008 express already giving the error: does not support opening Sharepoint Web sites.

  • Hi pbakker_67,

    I don't believe the "express" edition will provide the functionality you will need. I believe you will need a "Professional" level or greater to get the features for this type of functionality.

    Scott

  • Is it possible to have rules or seperate xml's without creating a webservice to make this cascading work for browser enabled forms?

    I am able to set the second dropdown value based on the first, BUT it only takes the first value of the "new" xml list??

  • anyone tell me what version of VB to use with SP 2007 server 3.0 and form services 07?

  • I think you need to correct this post as it is misleading. Because you can execute filters on web based infopath forms in Sharepoint 2010. As demonstrated here:

    sharepointsolutions.com/.../sharepoint-2010-tutorial-video-drop-down-filters

  • great article. it works in default website generated from VS2005/08. but not in under (IIS)localhost. it throws error when clicking invoke btn for getcustomers as (The user is not associated with a trusted sql server connection.)

    i am having sql server seperately (but in same domain)and calling name of that server in data source..

    pls help me..as iam in the halfway

  • Hi Scott,

    Nice article! Thanks for posting it! :)

  • May i know what are the basic tasks in Microsoft InfoPath 2010?

    <a href="www.sampleforms.net/">Sample Forms</a>

Page 2 of 2 (29 items) 12