SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

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.



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



  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


Posted 09-10-2007 2:31 PM by Maarten Eekels

Comments

Links (9/11/2007) « Steve Pietrek’s SharePoint Stuff wrote Links (9/11/2007) « Steve Pietrek’s SharePoint Stuff
on 09-11-2007 7:17 PM

Pingback from  Links (9/11/2007) « Steve Pietrek’s SharePoint Stuff

SharePoint 2007 link love 09-13-2007 at Virtual Generations wrote SharePoint 2007 link love 09-13-2007 at Virtual Generations
on 09-13-2007 4:25 PM

Pingback from  SharePoint 2007 link love 09-13-2007 at  Virtual Generations

tbarnett wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 09-14-2007 2:05 PM

I achieved a similar result a much easier way;

Added this:

<ParameterBinding Name="currentID" Location="QueryString(ID)" DefaultValue=""/>

and this:

<xsl:param name="currentID" />

Then went into the dataview filter dialog and selected it from the dropdown.

willdownload wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 06-10-2008 11:29 AM

What I am trying to figure out is How To Filter On Project Name, if Project ID is what is passed in the URL.

The list I want to filter includes Project Name (via a Lookup).  ProjectID is referenced on the page but is not resolved until AFTER my list is filtered.  How do you:

1) Resolve Name from ID or

2) force my list filtering to happen after the rest of the page is completed?  

Suleyman wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 01-08-2009 8:52 AM

Hi,

This is exactly what I was looking for but I have one question. Is doing xslt formatting getting every item from the list and filtering them at moment of parsing to the webpage? If that is the case than it can become a load on the performance espacially when it queries a large list.

Ted wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 01-28-2009 11:06 AM

Thanks this was a big help.  I tried all kinds of things to get this to work right

Travis wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 01-28-2009 12:45 PM

I'm passing URL parameters to the DVWP per the above example, but I'd like to know if there a way to have the DVWP perform a default query if no URL parameters exist and only filter the DVWP if they do?

For example, I'd like the query to run without a filter if the URL looks like this:

report.aspx

But if there are parameters, I need to filter the DVWP based on them. For example:

report.aspx?program=TRANSPORTATION&pm=JOHN

Anyone know how to accomplish this (preferably using SharePoint Designer)?

Thanks.

Travis wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 01-28-2009 2:49 PM

Never mind... I actually just figured out the answer to my own question...

By setting each parameter's default value to "%" and by setting data source filters so that they filter based on a field that "contains" the value of any parameter, the query will return all values if no parameters are present. If parameters are present in the URL, the value of the parameter overrides the default value (%).

...maybe not the most efficient solution, but it appears to be working.

Alana wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 03-04-2009 7:30 PM

Thanks so much for this - exactly what I needed... so many other blogs that I followed contained code or didn't work - your's worked perfectly for me first go.

Thanks again!!

Si wrote re: How to filter data in a Data View Web Part, based on the URL of the Web Part Page
on 03-10-2009 5:29 AM

Man I can;t get my head round this, I'm a Newbie to this and I'm wanting to filter a data view list based on the item I've selected. " Lists 1. Assets 2.Devices.

Each Asset has numeours Devices and I want to show this on the Assets Form, but need it to show only those devices that match the Asset. Both Lists Have AssetID, Lists is uses a lookup on List1 for the AssetID

Any Ideas?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.