Wednesday, March 31, 2010

SharePoint Designer - Data View - Filter - Query String - Show All

SharePoint Designer Data Views are a powerful way to access the information in SharePoint Lists. You have control over the XSL and can combine that with CSS and JavaScript to build some interesting tools.

One common request seems to be pulling a list, and making items in that list link to more details in another page. This can be easily handled by passing along a field in the Query String for the second page to use as a filter value. In the Data View on the second page, select Filter, Create New Parameter, then choose the Query String option.

The trouble comes in with the Default field in the Query String setting. There is no way to have the page show all list items if no Query String value is found. If you don't set a default and no Query Sting variable is passed, then you get no results from your list. If you do set a default, you only get items that work with that filter. There is no "show all" or "select *" option.

Fortunately there is a workaround. Web Part Connections can use filter values passed from another web part, but if no filter value is passed, the default is to show all of the items... basically Web Part Connections default to show all, while Filter has the opposite default of show none.

To build the workaround you need 2 Data View web parts. Build the first one with the list of all the possible variables that could be passed in your Query String. Basically this is keying off the same list that your first page did. Select the column that is being passed in the Query String (typically Title) then set up the Filter, Create New Parameter, Query String. So you've filtered this first Data View web part to show exactly the same thing that was passed in the Query String. Redundant, but necessary.

The second web part contains whatever details you wanted to show. This time use the Web Part Connections to Get Filter Values From the first web part. Now if a filter value is passed by the Query String, it will set the first web part which will then set the second. If no value is passed by Query String, the first web part will be blank, and the second will show all items.