How to filter data in a Data View Web Part, based on the URL of the Web Part Page

A scenario that we encounter a lot, is where you have a project landing site with individual project sites below. On the landing site you have a SharePoint list with all your projects and on each individual project site, you would like to show the project details from this list of the parent site.

Basically this would mean you would have to add a Data View Web Part on the project site to show data from the Projects list of the parent site, and filter the data is some sort of way to show only the data for the appropriate project. This works quite well of course, but you don’t want your users to have to set that filter every time they create a new project site. So what we would like is to set the filter in the project site template.

Of course you never know beforehand what actual value to filter the list data on, so we have to create a dynamic filter solution. My solution is to filter the list data in the Data View Web Part using the URL of the project site. This means that people creating a project site, should use the project name or project number (or any other unique column value you use in your Projects list) as the URL of the project site. In my example, the URL of the projects landing site is http://servername/sites/projectnet.

I assume you know how to add a Data View Web Part to the default.aspx page with SharePoint Designer. You’ll also need SharePoint Designer to make the modifications I suggest below.

  1. From the “Common Data View Tasks” menu, click “Parameters.”, click “New Parameter”, type “CurrentURL” as the Parameter Name, choose “Server Variable” as the Parameter Source, and type “URL” as the Server Variable Name.

    image

  2. From the “Common Data View Tasks” menu, click “Filter:”, enable “Add XSLT Filtering”, and click “Edit.”.

    image

  3. Enter the following XPath expression:

    [@ProjectName=substring-after(substring-before($CurrentURL,’/default.aspx’),’/projectnet/’)]

    Where @ProjectName is the name of the column from your Projects list you would like to filter on, and /projectnet/ is the part of the URL preceding your project site URL (probably your project landing site).

Save your site as a SharePoint Site Template and every new project site should automatically show the project details on its default.aspx page.

Leave a Reply