SharePoint includes a couple of useful web parts that can be used to filter other web parts. The problem is, they are only available in the Enterprise edition. It is easy, however, to build our own filters.
The key interface here is ITransformableFilterValues; it defines the contract for passing one or more values into another web part for the purpose of filtering it. Another useful interface is IDefaultFilterValue, which, guess what, defines a default value to be returned in case no one exists to be returned. Let’s see an example that retrieves values from the query string:
There are a couple of public properties:
ParameterName: the query string key whose value is to be returned;
AllowAllValue: whether to allow the all (null) value;
AllowEmptyValue: whether to allow the empty (“”) value;
AllowMultipleValues: whether to allow multiple values or not;
MultiValueHandling: what to do if multiple values are found for the ParameterName key: select the first only, return all or combine them all into one;
MultiValueSeparator: the separator for combining multiple values.
The web part is invisible and will only show the chrome when the page is in edit mode. After you add it to a page, you can add a connection of type filter to another web part and select the field on the other web part that you want to filter by. The actual
ITransformableFilterValues implementation is returned by the GetFilterValues method, which is marked with the ASP.NET ConnectionProvider attribute so as to make it a connection provider, and can feed several other web parts. The logic inside ParameterValues is a bit tricky because of the AllowAllValue and AllowEmptyValue properties but I think you’ll have no problems following it.
You would normally apply a filter using the browser interface, but you can also do it in markup:
In this example, I am binding the LinkTitle field of a list view web part to the TaskName query string parameter provided by the QueryStringFilter web part. If the query string contains a TaskName parameter that matches the list view’s LinkTitle, it will show these records.