Dynamic List Filter Menu Using Data View Grouping - SharePoint Development & Administration + InfoPath

Saturday, March 19, 2011

Dynamic List Filter Menu Using Data View Grouping

This article explains how to create a dynamic filter menu for content in SharePoint.  Methods from the article SharePoint: List Distinct (unique) values from a specific column in a list are used to obtain unique values from a specific column in a SharePoint list to be displayed as each menu item.  When an filter option from the menu is selected, content on the page will be filtered based on the selection.  

For a single menu displaying filter options dynamically, you only need to associate the menu with one column from the main list.  When an option is selected, the browser is redirected to the same page with a query string parameter set to the selected filter parameter using JavaScript.  The main list on the page is then configured to filter contents based on the parameter.

  • Document Library with custom column "Heading"
  • One Web Part page with the following components:
    • Data View Web Part displaying the actual documents in the library
    • Data View web Part displaying the possible filter options (from the Heading column)

  • When a filter option is selected from the menu Data View, the browser will be sent to the current page with the selected option included as a Query String Parameter (using link or JS).
  • The main Data View web part would be configured with a custom parameter obtained from the Query String.  The custom parameter would be used as filter criteria for the Data View.  The result is that when an option from the dynamic menu is selected, the documents listed in the main Data View will be dynamically filtered based on the selected option.

JavaScript Reference:
The “How To” instructions below refer to JavaScript functions used for Query String Manipulation. To view details about the functions referred to below, as well as other functions for hyperlink/query string manipulation, see:

Query String Manipulation using JavaScript
    - Add Query String Parameter using JavaScript - addParameter()

How To:
  1. Create a new Document Library, or use and existing one if required.
  2. Add a custom column to the library (or content type if using custom content types).  In this example the column will have the title “Heading”, and will be used to list menu options, but it can have any title as required.
  3. From the view menu of the document library or the setting page, select the “Create view” option to add a new view.
  4. When viewing the newly created View page, select the “Edit in SharePoint Designer” from the file menu, or manually open the site and page for editing in SharePoint Designer.
  5. Add a second instance of the same Document Library Web Part to the page above the existing.  Set the crome state to None in the Appearance section if required.
  6. Right click on the new Web Part and select Convert to Data View option.
  7. Once a Data View Web Part, open the Sort and Group dialog, and add criteria to group the list by the “Heading” column.
  8. In the design pane of the page, right click on one of the listed items (documents) and remove the row from the table.  This will remove all list items resulting in only the groups remaining.  Remove additional functionality from the groups, such as the expand, contract button/link or css styles.
  9. Format the group value as a hyperlink, with “#” as the value for the href attribute.
  10. Add an “onclick” attribute, with a call to the addParameter() JavaScript function.  The value of the selected filter option and the parameter name is passed to the JavaScript function which adds the parameter to the current url using a redirect.

If there are no existing parameters on the current page url, the easiest method to configure a hyperlink to add a Query String Parameter to the current page is to set the href attribute to the following:

<a href="?main={$fieldvalue}"><xsl:value-of select="$fieldvalue" /></a>

Using the above (simple) method will replace any existing parameters in the current url with the new parameter and value.  For a single dimensional menu, using this method to add Query String parameter and value to use as filter criteria on other Web Part s on the page would be fine.  For multi dimensional menus (main and sub menu), you may need to retain the parameter in the Query String that determines which main/primary heading when adding the sub-heading filter parameter.  In this case, JavaScript can be used to add the new parameter to the existing Query String.  Using JavaScript to manipulate the query string is a better method to use in either case as existing parameters will always be retain, but would obviously require that JavaScript be enabled on client browsers.  

More Resources for SharePoint Development & Customisation:
Share this article:
Stumble This Delicious
submit to reddit

1 comment:

Cowgirl Kendra said...

Hi - thanks for making this post. This concept is exactly what I would like to do, except, I want to take it one step further and filter a document library as well as other lists (on the same page) at the same time. For example, if I select "Accounting" from the drop-down menu, I would like the doc library, task list and calendar list to all filter for items that match the "Accounting" criteria. Do you know if that is possible based on the instructions that you've listed?

Post a Comment