Adding more than three filters in CQWP (Ricardo Loo)

Problem

You want to roll up content from your site (and subsites), no problem, CQWP is there to help.

But, hey! the tool part UI only offers support for three filters:

image

What if you need more filters?

A (simple) solution

One way to allow for more than three filters is to override the QueryOverride property of the web part. This involves the following general steps:

  1. Export the web part to file system.
  2. Build the CAML required to filter the list.
  3. Update the QueryOverride property
  4. Import the web part to the web part page.
Export the web part to the file system

With the page in Edit Mode, expand the Content Query Web Part Menu and select Export…

Save the web part to the local file system.

image

Build the CAML required to filter the list

If you’re not comfortable with CAML there are some tools and resources that could help:

In my site there is a custom list with the following structure and items:

image

Update the QueryOverride property

If I need four filters (one for each custom column) I could use the following value in the QueryOverride property:

         <property name="QueryOverride" type="string">
          <![CDATA[
           <Where>
              <And>
                 <And>
                    <BeginsWith>
                       <FieldRef Name='NewColumn1' />
                       <Value Type='Text'>NewColumn1 Item1</Value>
                    </BeginsWith>
                    <BeginsWith>
                       <FieldRef Name='NewColumn2' />
                       <Value Type='Text'>NewColumn2 Item1</Value>
                    </BeginsWith>
                 </And>
                 <And>
                    <BeginsWith>
                       <FieldRef Name='NewColumn3' />
                       <Value Type='Text'>NewColumn3 Item1</Value>
                    </BeginsWith>
                    <BeginsWith>
                       <FieldRef Name='NewColumn4' />
                       <Value Type='Text'>NewColumn4 Item10</Value>
                    </BeginsWith>
                 </And>
              </And>
           </Where>
          ]]>
        </property>

Note you only need the Where section of the CAML query and it has to be contained within a CDATA element.

Import the web part to the web part page

With the web part page in Edit Mode, click the Add a Web Part link to open the Web Part Pane.

Under the Upload a Web Part section select the modified web part file in the local file system and click Upload. This will load your web part under the Imported Web Parts category.

image

Select your web part and click Add to import the web part into the web part page.

Additional considerations

When we use the overrides properties within the CQWP some of the elements in the UI will be disabled, in this case we cannot add more filters from the Content Query Tool Part.

image