Search, Filter or Sort Lists from a Query String in SharePoint 2007, 2010 and Office 365
What’s a Query String?
A query string is a collection of more or more parameters added to the end of a URL. You see these throughout SharePoint and at most interactive web sites. As an example do a search using Bing.com for “techtrainingnotes”. After you click the search button your search text is appended to the URL and will look something like this: http://www.bing.com/search?q=techtrainingnotes. The first parameter in a query string is identified with a question mark and each additional parameter is identified with and ampersand. Here the parameter name is “q” and the value is “techtrainingnotes”.
For the filter and sort tricks we will create a URL that looks something list this:
http://intranet/sites/training/Lists/Tasks/AllItems.aspx?FilterName=Title&FilterMultiValue=Task 2
The first part, “http://intranet/sites/training/Lists/Tasks/AllItems.aspx” is the path to a view of a list and everything from the question mark to the end is the query string.
Filtering One or More Columns
To filter a task list to show just “In Progress” tasks you would first visit a view that included the Status column. The URL to this list would look something like this:
intranet/sites/training/Lists/Tasks/AllItems.aspx
To filter this view you would add two query string parameters to the URL that specify the column to filter on and the value to filter for. This URL might look like this:
intranet/sites/training/Lists/Tasks/AllItems.aspx?FilterField1=Title&FilterValue1=In Progress
You filter on more than column by adding additional FilterFieldX and FilterValueX parameters to the URL. To find all of Luis Bonifaz’s In Progress tasks you might use this query string:
?FilterField1=AssignedTo&FilterValue1=Luis Bonifaz&FilterField2=Status&FilterValue2=Not Started
Why “AssignedTo” and not “Assigned To”? List columns can have an internal name and a display name. You will always need to use the internal name in these queries. See “Finding the Real Column Name” at the end of this article to see how to find these internal names.
Notes:
There can be multiple filters per query named FilterField1, FilterField2, FilterField3, …, FilterFieldxxx
FilterNameX is the column we are searching in – this column must be displayed in the page… i.e. it must be in the current view.
FilterNameX is case sensitive! A misspelled or miscapitalized FilterNameX column name causes the filter to be ignored and returns all rows from the list… So “Title” works and “title” does not.
FitlerNameX must be the internal name, which is not always the display name (details on how to find this name is at the end of this article)
FilterValueX is not case sensitive – searches for “in progress” and “In Progress” return the same results
FilterValueX does not support wild cards
All of the filters are AND’d together – i.e. the row in the list must match all of the filters to be displayed
Filtering a Single Column with Multiple Values and Wild Cards
The FilterField/FilterValue parameters can only find exact matches and only on a single value per column. You can also use “FilterName=” and “FilterMultiValue=” parameters to filter a column on more than one item or with wild cards. If you want to display all “In Progress” and all “Completed” tasks you could use this query string:
?FilterName=Status&FilterMultiValue=In Progress;Completed
You can also use wildcards in FilterMultiValue parameter:
?FilterName=Title&FilterMultiValue=*meeting*
You can combine FilterField/FilterValue with FilterName/FilterMultiValue when needed. Here’s an example to find all of Luis’s tasks with “meeting” in the task title:
?FilterField1=AssignedTo&FilterValue1=Luis Bonifaz&FilterName=Title&FilterMultiValue=*meeting*
Notes:
FilterName is the column we are searching in – this column must be displayed in the page… i.e. it must be in the current view.
FilterName is case sensitive! A misspelled or miscapitalized FilterName causes the filter to be ignored and returns all rows from the list… So “Title” works and “title” does not
FitlerName must be the internal name, which is not always the display name (details on how to find this name is at the end of this article)
FilterMultiValue is not case sensitive – search for “in progress” and “In Progress” return the same results
FilterMultiValue supports wild cards! – Searching for “task*” finds “Task 1”, “Task 2” etc
As FilterMultValue contains the word “Multi” then you would expect to be able to filter on multiple terms… and you can – separate each term with a semicolon (;) like this: &FilterMultiValue=In Progress;Not Started
Be careful not to add any extra spaces. The following will not find “Not Started” because of the extra space after the semicolon: &FilterMultiValue=In Progress; Not Started
To sort the items
You can use the SortField/SortDir to sort on any column in the view:
?SortField=Title&SortDir=Desc
Sort can also be used with the other filters:
?FilterField1=AssignedTo&FilterValue1=Luis Bonifaz
&FilterName=Title&FilterMultiValue=*meeting*
&SortField=Title&SortDir=Desc
Filtering from Quick Launch and other Links
So how do you use this little tidbit of information? You can generally do what these filters do with a view. Views do have one have annoying limitation… you can only display the first 50 views created. Technically you can create more than 50, but only the first 50 get displayed. Besides, it’s kind of tedious creating all of those views.
Let’s say you have a list with your 350 retail stores and you wanted to be able to quickly display a view of the stores filtered by any one state. You could create the 50+ views (50 states plus the other postal codes), or you could:
Add links in Quick Launch to selected states:
intranet/sites/training/Lists/Tasks/AllItems.aspx?FilterField1=State&FilterValue1=Ohio
Create a links list with all of the state codes and display it as a web part
Create a links list with all of the state codes, but display it as a dropdown list
Create an HTML dropdown list with the state code and add a little JavaScript to create the filtered URL and redirect to it
Finding the Real Column Name
The column name used as the FilterName parameter must be the internal name used by SharePoint, not the displayed name. As an example, in a task list there is a column named “% Complete”. The internal name of this column is “PercentComplete”. To find this name:
Display the list
Click the List (or Library) tab in the ribbon and click List (or Library) Settings
Scroll to the bottom or the page and click the column name
Explore the URL to find the internal name
intranet/sites/training/_layouts/FldEdit.aspx?List=%7B64C528B3%2D224B%2D4613%2D9D60%2D216300B5BCE0%7D&Field=PercentComplete