You should that SharePoint usually offers lots of ways to do more or less the same thing, and, of course, filtering a list view is no exception! In this post I am going to talk a bit about the ways that you can do this.
Built-in URL Filtering
SharePoint’s list view controls (XsltListViewWebPart, ListViewWebPart, etc) recognize a number of parameters from the query string that can be used to filter and sort automatically a list view, without requiring any additional effort.
Filter by single value:
FilterField must be an internal field’s name and FilterValue is case insensitive.
Filter by single value and an operator:
The list of possible operators (FilterOp) is:
Eq: equal (default);
Neq: not equal;
Gt: greater than;
Lt: less than;
Geq: greater or equal to;
Leq: less or equal to;
BeginsWith: the start of a string;
Contains: part of a string.
You can add up to 10 FilterField/FilterValue/FilterOp entries, the items must match all conditions (AND operator). If you wish to filter on an additional field of a lookup field, use this syntax:
Filter by one of multiple exact values:
FilterName is also the internal name of the field and the values in FilterMultiValue are treated as case-insensitive. Separate then by ;.
Filter by one of multiple partial values:
The * character acts as a wildcard, matching anything before or after. Placing a term between * is the same as using FilterField/FilterValue with the Contains operator in FilterOp.
Filter by a taxonomy field:
If you want to filter by a managed metadata field, you can either search by the display name using FilterField and FilterValue, or you can use the ids of the terms. A little known fact is that all usages of managed metadata values – values of fields of managed metadata type – are stored in the TaxonomyHiddenList hidden list. If you want to filter by an id of a term, you need to use the following syntax (search by one of two values with FilterOp=In, values separated by commas):
Or by a single value (no need for FilterOp):
The FilterLookupId=1 is only there to tell SharePoint that it needs to search not by value (FilterValue) passed in the query string, but by term pointed by it. You can find the ids for FilterValue in the TaxonomyHiddenList list.
Possible values for SortDir are pretty obvious:
Asc: ascending (default);
You can only sort by a single field.
Update: for SharePoint Online, you may need the useFiltersInViewXml=1 parameter too.
Filtering a XSLT Web Part by a Query String Parameter
Notice that in case the param value is not present in the query string, the second condition in the Or disjunction will apply, which will try to match an empty string inside of the Author (Created By) field, which will always be true. Be careful, however, that while this works most of the time, if you specify a value for param that is indeed part of the creator of the field, it will be matched. As of now, I don’t have an 100% foolproof solution for this, if you know of any, do let me know!
Filtering by a Form Field
This is a variation of the previous technique, where we use a Location of Form in a ParameterBinding. In this case, however, filtering will only be applied when the page is post back. Add the following HTML to your page:
And change the previous ParameterBinding to use the param input:
The Search button will cause a postback, thus applying the filter, and the Clear button will navigate to the current page, using the GET verb, thus clearing the filter.
If instead of HTML we want to use a server side control, we certainly can:
1: <ParameterBinding Name="param" Location="Control(param,Text)"/>
Filtering by a Web Part Connection
Filter web parts:
Another option is to filter by a web part that implements any of the filtering interfaces, used for supplying a value to another web part.
SharePoint Enterprise contains a collection of filtering web parts. These cover a number of scenarios:
Filtering from a query string parameter;
Filtering from a text box;
Filtering from a date picker;
Filtering from a choice of values;
Filtering from a SharePoint list;
Filtering from a Business Connectivity Service;
Filtering from a SQL Server Analysis Services cube;
Filtering from current user properties (id, profile property);
Filtering from a field in the current page ().
For the sake of simplicity, let’s pick filtering from the query string. In a web part page’s edit mode, add the Query String (URL) Filter web part to a web part zone:
Next, add a connection from a list view web part in your page:
Again, you are free to do it in markup:
Do replace the ConsumerID and ProviderID by the actual IDs of your web parts, where ProviderID is the ID of the QueryStringFilterWebPart and ConsumerID is the ID of the list view web part and also the ConsumerFieldNames for the list field you want to filter on and ProviderFieldNames for the value you used in FilterName.
Filter by a custom web part:
All you need is to implement interface IWebPartField. Here’s a sample implementation of a web part that filters by a query string value, think of it as a poor man’s Query String Filter web part:
After you deploy this web part and add it to a page, you will be able to create a connection to a list view web part so as to enable its filtering.
Filtering an External List
An external list is filterable by a finder operation, which is an operation of type Read List that you configure on the external content type. You can use SharePoint Designer to create views on the external list on top of an existing Read List operation. In the view’s page, the finder method – the name of the operation, not its display name – is specified as a Method entry in the CAML for the View element in the XSLT list view web part, for example:
Of course, you can also specify parameters to a finder method (a filter):
Filtering by XSLT
The last technique is done on the “client-side”, through XSLT. This offers possibly the worst performance, since all records are retrieved before they are filtered, but the greatest flexibility, because you can apply conditional rules that would be very difficult, if not impossible, using the previous techniques. XSLT filtering can be applied on top of web part connections, external lists, etc.
So, in your XSLT list view web part, inside xsl:stylesheet, add something like:
This goes through all the returned records and outputs a message depending on the value of the PercentComplete field. Because it is a number field, we must use the syntax PercentComplete. (a dot in the end).
As you can see, a lot of functionality is available without having to resort to programming, SharePoint is very good at it. XSLT offers the greatest flexibility, because you can use conditional logic to express your exact intents. Whenever possible, however, try to filter records at CAML level, because less data will be returned.