How to: Consume the Multi-Select CheckBox Filter Web Part

SharePoint 2007

In this topic, let's look at creating a filter consumer Web Part that displays filtered news headlines depending on the regions selected in the Region filter Web Part that is connected to it.

NoteNote:

The Region filter Web Part is discussed in How to: Write a Multi-Select CheckBox Filter Web Part.

Other than creating a user interface with a DataGrid control for displaying the headlines, the steps we need to follow to create the Headlines filter consumer Web Part are:

  1. Create a ConsumerParameters interface instance to tell the filter provider about the parameter names. Windows SharePoint Services 3.0 will use this interface to populate the user interface for connecting the Web Parts at run time.

  2. Expose a Consumer connection point to request the IFilterValues interface from the filter provider Web Part.

  3. Use the interface in PreRender() and display the current filter value.

  4. You can test this Web Part by connecting to the Region filter or using an out-of-the-box filter Web Part such as the Text filter or the Authored List filter.

    NoteNote:

    You will also need the Filter Actions Web Part to test this sample. The Filter Actions Web Part provides an Apply Filters button that you can use to apply filters on the page.

Here's the sample code for the consumer Web Part. Follow the steps in the Walkthrough: Walkthrough: Writing a Simple Filter Consumer Web Part Sample to deploy this Web Part.

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using aspnetwebparts = System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Server.Utilities;
using wsswebparts = Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Portal.WebControls;
using System.Collections.ObjectModel;
using Microsoft.SharePoint.Utilities;
using System.Data;
using System.Collections;

namespace MyWebPartLibrary
{
    public class DataBoundHeadlinesFilterConsumerWebPart : aspnetwebparts.WebPart
    {

        public class Headline
        {
            private string title;
            private string region;

            public Headline(string Title, string Region)
            {

                this.title = Title;
                this.region = Region;

            }
            public string Title
            {
                get
                {
                    return this.title;
                }
                set
                {
                    this.title = value;
                }
            }

            public string Region
            {
                get
                {
                    return this.region;
                }
                set
                {
                    this.Region = value;
                }
            }



        }

        List<wsswebparts.IFilterValues> providers = new List<wsswebparts.IFilterValues>();
        List<Headline> headlines;
       
        DataGrid DataListHeadlines;

        protected override void CreateChildControls()
        {
            DataListHeadlines = new DataGrid();

            headlines = new List<Headline>();

            headlines.Add(new Headline("This week in Redmond", "Redmond"));
            headlines.Add(new Headline("Traffic in Redmond", "Redmond"));
            headlines.Add(new Headline("Sports highlights in Redmond", "Redmond"));
            headlines.Add(new Headline("Bitter cold, wind to follow today's snow", "Seattle"));
            headlines.Add(new Headline("This week in Seattle", "Seattle"));
            headlines.Add(new Headline("Sports News in US", "US"));
            headlines.Add(new Headline("This week in the US", "US"));
            headlines.Add(new Headline("This week in the world", "World"));
            headlines.Add(new Headline("Last week sports highlights", "World"));


            DataListHeadlines.ID = "list1";
            Controls.Add(DataListHeadlines);

            base.CreateChildControls();
        }


        [aspnetwebparts.ConnectionConsumer("Headlines Filter", "IFilterValues", AllowsMultipleConnections = true)]
        public void SetConnectionInterface(wsswebparts.IFilterValues provider)
        {
            if (provider != null)
            {
                this.providers.Add(provider);

                List<wsswebparts.ConsumerParameter> l = new List<wsswebparts.ConsumerParameter>();
                l.Add(new wsswebparts.ConsumerParameter("Region", 
                    wsswebparts.ConsumerParameterCapabilities.SupportsMultipleValues | wsswebparts.ConsumerParameterCapabilities.SupportsAllValue));

                provider.SetConsumerParameters(new ReadOnlyCollection<wsswebparts.ConsumerParameter>(l));
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            this.EnsureChildControls();

            // The filtering logic is to perform a union of all of the filters
            // (a logical OR). 
            // If we didn't get any filter providers or if any of the filters 
            // send the "All" value (i.e. if provider.ParameterValues == null)
            // then we don't need to filter and we can just send back all of 
            // the headlines.
            
            List<Headline> filteredHeadlines = null;

            bool shouldFilter = true;
            if (this.providers.Count == 0)
            {
                shouldFilter = false;
            }
            else if (this.providers.Count > 0)
            {
                foreach( wsswebparts.IFilterValues filter in this.providers)
                {
                    if (filter.ParameterValues == null)
                    {
                        // Some filter sent "All" - don't bother with the rest
                        // of the filtering.
                        shouldFilter = false;
                        break;
                    }
                }
            }
            

            if (!shouldFilter)
            {
                // the "filtered" headlines are unfiltered.
                filteredHeadlines = this.headlines;
            }
            else
            {
                // Just fill in the headlines that match the filters.

                filteredHeadlines = new List<Headline>();

                // Create a lookup from region to a list of headlines which 
                // correspond to that region.
                Dictionary<string, List<Headline>> regionHeadlineMap = new Dictionary<string,List<Headline>>();
                foreach (Headline headline in this.headlines)
                {
                    List<Headline> headlinesForRegion = null;
                    if (!regionHeadlineMap.TryGetValue(headline.Region, out headlinesForRegion))
                    {
                        headlinesForRegion = new List<Headline>();
                        regionHeadlineMap.Add(headline.Region, headlinesForRegion);
                    }

                    headlinesForRegion.Add(headline);
                }

                foreach (wsswebparts.IFilterValues provider in this.providers)
                {

                    ReadOnlyCollection<String> values = provider.ParameterValues;
                    if (values != null)
                    {
                        foreach (string v in values)
                        {
                            if (v == null)
                            {
                                // This indicates the "Empty" value, which this
                                // doesn't apply to headlines, since they all 
                                // have a Region.
                            }
                            else
                            {
                                List<Headline> matchedHeadlines;
                                if (regionHeadlineMap.TryGetValue(v, out matchedHeadlines))
                                {
                                    foreach (Headline matchedHeadline in matchedHeadlines)
                                    {
                                        if (!filteredHeadlines.Contains(matchedHeadline))
                                        {
                                            filteredHeadlines.Add(matchedHeadline);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Display the filtered headlines.
            DataListHeadlines.DataSource = filteredHeadlines;
            DataListHeadlines.DataBind();

            base.OnPreRender(e);
        }
    }
}

See Also

Community Additions

ADD
Show: