Tuesday, May 10, 2011

Filter Content Query Web Part based on page field value and query string

I wanted to blog about this excellent feature in SharePoint 2010 CQWP for quite a long time and here it is.
Can I filter my CQWP based on a field’s value in the current page?
YES! You can in SharePoint 2010 Smile
What’s the use?
Well, simple example would be that you want a related items web part, like – related news, related staff members.
Stop talking, show me an example?
Here is my staff page:
image
What we are missing is a web part to show the related members of the Photography department, which is nothing but the current page’s Department value – Photography.
Configuring the CQWP
It is very simple to configure the CQWP to filter based on the page field value. Construct the query in the Additional Filters options:
image
The key thing is to understand the token format:
[PageFieldValue: <field-display-name>]
PageFieldValue token will filter the items based on a current page’s field value. The query above will fetch me all the items whose Staff Department is Photography (in this example) and adding Title not equal to current page’s Title vale will eliminate the current item appearing in the results.
and here is our web part:
image
Job done!
BTW, my example is running in Office 365 beta! Smile
Can I use query string to filter?
Yes, you can! Just use the PageQueryString instead.