Testing

webTiger Logo Wide

Using Query String Parameters to Quickly Filter SharePoint Lists

SharePoint 2013 Logo

There are many times when you want to display filtered views of SharePoint lists by default. Sure, you can create a custom view – and that is often the best approach – but what if you want a configurable query? Then this simple ‘hack’ can be perfect for the job.

There have been a few times when the static filtering offered by a custom list view just won’t cut it. It’s at this point I’m so pleased that Microsoft thought to offer list filtering capabilities via query string (QS) parameters.

You can filter a list quickly and simply by adding just two QS parameters: FilterField1 and FilterValue1. For example, to filter on Title field values you might do:

http://sp-server/sites/demo/Lists/Example/AllItems.aspx?FilterField1=Title&FilterValue1=SharePointCode language: plaintext (plaintext)

This would filter the list results to only those with a Title=SharePoint (i.e. it does an equality comparison by default).

You can add multiple field filters too. For example:

http://sp-server/sites/demo/Lists/Example/AllItems.aspx?FilterField1=Title&FilterValue1=SharePoint&FilterField2=ProductBuild&FilterField2=2013Code language: plaintext (plaintext)

But what if you don’t want a simple equality check? Well, Microsoft have you covered there too. You can add a FilterOp (1, 2, etc.) parameter to change the type of evaluation performed. The available operators are:

  • Eq – equality operator.
  • Neq – non-equality operator.
  • Gt – greater than operator.
  • Geq – greater than or equal to operator.
  • Lt – less than operator.
  • Leq – less than or equal to operator.
  • BeginsWith – operator that matches against the start of field values.
  • Contains – operator that matches against fields containing a value.

For example, to only return items created after 1st January 2016, you might do something like this:

http://sp-server/sites/demo/Lists/Example/AllItems.aspx?FilterField1=Created&FilterOp1=Gt&FilterValue1=2016-01-01Code language: plaintext (plaintext)

You can add up to 10 filters to a query, but a limitation is that they are all ANDed together.

You can also perform multi-value equality filtering operations, where search terms are semi-colon delimited, like this:

http://sp-server/sites/demo/Lists/Example/AllItems.aspx?FilterName=Title&FilterMultiValue=SharePoint;*Office*Code language: plaintext (plaintext)

The above query would filter the results to items where the Title equals ‘SharePoint’ or where the Title contains ‘Office’ (which is what the asterisk wildcard values do). A limitation of this approach is the multi-value filter doesn’t appear to work with other filter terms (i.e. you can’t mix and match with FilterField1=X, etc.)

You can also sort the results. Doing this is simple, and uses SortField and SortDir QS parameter names. For sort direction ‘Asc’ (ascending) and ‘Desc’ (descending) sort orders are supported. For example:

http://sp-server/sites/demo/Lists/Example/AllItems.aspx?FilterField1=Title&FilterValue1=SharePoint&SortField=ProductBuild&SortDir=AscCode language: plaintext (plaintext)