I wanted to write a web part that consumes query string data that gets passed to it by the Query String Filter web part. Unfortunately, te current SharePoint 2010 documentation still includes reference to an API interface which it says is obsolete: IFilterConsumer. The interface itself has a message that says “Use System.Web.UI.WebControls.WebParts.IWebPartParameters instead.” Well, unfortunately, I couldn’t find any good examples out there of writing a Web Part that uses the IWebPartParamters interface for consuming the Query String Filter web part. I finally figured it out, and thought I’d share.

In the following example, I’ve created a public property on the Web Part called Category which consumes the querystring filter value. (In my case, my querystring key is “Category” so I created a mirror property to consume that property value in my web part.)

using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

[ToolboxItemAttribute(false)]
public class FAQs : WebPart, IWebPartParameters
{
  private string category;
  IWebPartParameters provider = null;
  IDictionary data = null;
  
  public string Category
  {
    get { return category; }
    set { category = value; }
  }

  protected override void CreateChildControls()
  {
    base.CreateChildControls();        
  }

  [ConnectionConsumer("Parameters")]
  public void GetConnectionInterface(IWebPartParameters providerPart)
  {
    PropertyDescriptor[] property = { TypeDescriptor.GetProperties(this)["Category"] };
    PropertyDescriptorCollection schema = new PropertyDescriptorCollection(property);
    providerPart.SetConsumerSchema(schema);
    this.provider = providerPart;
  }

  protected override void OnPreRender(EventArgs e)
  {
    if (this.provider != null)
    {
      this.provider.GetParametersData(new ParametersCallback(SetProviderData));
    }
    
    base.OnPreRender(e);
  }

  public void SetProviderData(IDictionary dict)
  {
    this.data = dict;
  }

  protected override void Render(HtmlTextWriter writer)
  {
    if (this.provider != null && this.data != null)
    {
      if (data["Category"] != null)
      {
        category = data["Category"].ToString();
      }
    }

    base.Render(writer);
  }

  public void SetConsumerSchema(PropertyDescriptorCollection schema)
  {
    get { throw new Exception("Not implemented."); }
  }

  public void GetParametersData(ParametersCallback callback)
  {
    get { throw new Exception("Not implemented."); }
  }

  public PropertyDescriptorCollection Schema
  {
    get { throw new Exception("Not implemented."); }  
  }
}