Recently I’ve spent a lot of time working with the XSLT List View Web Part so it was a nice change last week to work on a solution involving the Content Query Web Part (CQWP). For my particular task of aggregating data from multiple lists the CQWP was the obvious choice – and by writing custom XSL I knew that I’d be able to format the results in the style I needed.
The interesting part of the solution was to restrict the query so that it only queried multiple specific lists, i.e. not the whole site collection, nor all lists in a site, nor one specific list – which are the default options when working with the CQWP – but, for example, list A and list B.
[My default position would normally to have been to have a ‘parent’ content type, with additional content types inheriting from this parent – and then associate the content types to my lists. I would then be able to query based on content type. In this case, however, the lists were already created and so that option was ruled out.]
So, how to do this? Well after a moments head scratching it actually turned out to be quite straight forward. I initially went to look at the QueryOverride property, but after exporting a CQWP I saw the ListsOverride property in the .webpart file. It is this property that we need to change.
First of all get the IDs of your lists that you wish to restrict the query to. Then export a CQWP and edit the .webpart file.
Find the line that looks like this:
<property name="ListsOverride" type="string" />
And replace it with a line that looks like this (replacing your list IDs):
<property name="ListsOverride" type="string"><![CDATA[<Lists><List ID="31A7C09D-52CC-4585-B998-A3F180A9BD07"/><List ID="7382D66D-2E4C-47CB-A95C-5EECD68F5E61"/></Lists> ]]></property>
Save the .webpart file and import it back into your page and you will now by querying only those specific lists! Note, you will need to setup the other parts of the query such as list type/ content type etc – unless you have also specified these in the .webpart file. Also, looking at the properties of the CQWP you will also note that it informs you that the query can no longer be set through the user interface.