Tuesday, November 18, 2008

Filtering ListView with Query String

This is something I have needed for a while that I didn't think was possible. I have often needed to filter a list based on an attribute of the currently logged in user. In the past I have create multiple views for each of the possible combinations.

I thought there must be a better way. After filtering a few lists, I noticed the query string was changing and adding the filter values to the query string. I started creating my own query string filters and it worked a treat.

All you need to do is add the following to the query string:

?FilterField1=[FieldInternalName]&FilterValue1=[Filter Value]

You can repeat this for multiple columns by using FilterField2, FilterValue2 etc.

To filter on Person field, use their user name:

eg. FilterValue1=The%20Masked%20SharePointer

To filter on DateTime field, use US format:

eg. FilterValue1=11/20/2008

If you are having trouble working out how to filter a column, do the filter through the SharePoint interface and see how it does it!!

As an added bonus, the field you filter on doesn't even need to be on the current view!!

No comments: