CQWP #2: Aggregating items with multiple content types

As with my earlier post CQWP #1: Aggregating items from the last 7 days, it is sometimes a requirement to configure the Content Query Web Part beyond that which is possible through the UI and the browser. One such example of this is configuring the CQWP to aggregate items from different content types.

The recommended approach here is to use parent/child relationships for the content types if possible. For example it could be possible to have a parent content type inheriting from the out-of-the-box Document content type called Project Document. Then any actual project documents where a content type is required could inherit from this, such as a word Project Information Document or an excel Financial Tracking Spreadsheet.

This approach is only valid assuming that the content types share a base content type, such as Document in the example above. Another scenario could be to have two content types that are actually related, but with different base content types – and this is where it’s necessary to go beyond what is possible from the UI by using the QueryOverride property of the web part. 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 query we want to add to the web part needs to say ‘use this content type OR this content type’ and so the lines we need to add to the properties of the web part are:

   1: <property name="QueryOverride" type="string"><![CDATA[<Where>
   2:     <Or>
   3:         <Eq><FieldRef Name="ContentType"/><Value Type="Text">Content Type One</Value></Eq>
   4:         <Eq><FieldRef Name="ContentType"/><Value Type="Text">Content Type Two</Value></Eq>
   5:     </Or>
   6: </Where>]]>
   7: </property>

The screenshots below shown an example of a modified CQWP returning items from different content types (indicated by simply naming the items after the content type!), and a section of the modified properties of the web part.

CQWP - QueryOverride2 image

Note: In the example above it assumes that both of the content types are coming from the same library or type of library – and therefore this can be configured in the UI of the web part. However it may also be necessary to use the ListsOverride property to specify a base type of library for the query. [One thing I’ve not been able to work out is how to use the ListsOverride property to aggregate from different list base types.]

]] >

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

4 comments to CQWP #2: Aggregating items with multiple content types

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>