CQWP #1: Aggregating items from the last 7 days

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:

   1: <Where>

   2:  <Geq>

   3:   <FieldRef Name="Created"/>

   4:   <Value Type="DateTime">

   5:    <Today OffsetDays="-30"/>

   6:   </Value>

   7:  </Geq>

   8: </Where>

And so the lines we need to insert into the properties of the web part are:

   1: <property name="QueryOverride" type="string"><![CDATA[<Where>

   2:     <Geq>

   3:         <FieldRef Name="Created"/>

   4:         <Value Type="DateTime">

   5:             <Today OffsetDays="-30"/>

   6:         </Value>

   7:     </Geq>

   8: </Where>]]></property>

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.

CQWP - Recent Items CQWP - QueryOverride

share and enjoy
  • Print
  • Twitter
  • Digg
  • del.icio.us
  • StumbleUpon
  • Yahoo! Buzz
  • Google Bookmarks
  • Facebook

2 comments to CQWP #1: Aggregating items from the last 7 days

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>