Whilst clearing some space on my laptop – a regular occurrence when running multiple hyper-v machines! – I found a document I’d written a while ago, noting down some of the more obscure but useful tips for working with the Content Query Web Part (CQWP) in SharePoint 2007. Despite looking at SharePoint 2010 over the past few months (and hopefully for the next few too!), I recognise that SharePoint 2007 is still alive and kicking, and so here is the first of my notes for the CQWP.
A fairly common request when working with items returned by the CQWP is to limit the result set to items that have been created recently, or perhaps modified recently. This means that we want to return items that have been created in the past x days – in addition to setting up regular filters such as location, type of library and specific column values etc.
Whilst there is no way to achieve this requirement via modifying the properties of the CQWP in the browser it is possible to do this by making use of a property known as QueryOverride. The steps to implement this are broadly:
1. Add and configure a CQWP to a page
2. Export the web part
3. Locate the property QueryOverride in the exported web part
4. Insert a CAML query for this property
5. Save and import the web part
The difficult step here is inserting the necessary CAML query to restrict items to those created during the last x days. Now a query for returning items created during the last 30 days looks something like this:
And so the lines we need to insert into the properties of the web part are:
The screenshots below show an example of an unmodified CQWP returning news articles and a modified CQWP returning news articles with an article date within the last 7 days.