Extend Content Query Web Part with QueryOverride

The Content Query Web Part is still one of the coolest things in SharePoint for site builders (IMHO). There’s a lot of literature about it and I’m not going to write about what CQWP is nor how to configure it.

So, what am I writing about? I just want to add my two cents about how to extend the Content Query Web Part with QueryOverride.

First of all: what the hell is QueryOverride? Well, QueryOverride is a property of the Content Query Web Part that you can only set by code to override the original query. I.e. recently I had to query a site (SPWeb) for documents of two different content types. Unfortunately you cannot do this by UI and here comes QueryOverride.

You can do this in two diffent ways. If you placed the CQWP in a web part zone you can export it and set the value of the QueryOverride property or, if you placed the CQWP in a page layout you can add the QueryOverride property in the ContentByQueryWebPart control.

In both cases you have to add the new query, a CAML query, in a CDATA section. The difference is just about how you set the query. If you export the CQWP you have to look for the QueryOverride property and change it from:

to:

Otherwise, working with the CQWP in page layouts you have to add the QueryOverride property the following way:

Note that I had to encode the “special” character in the query, I mean < > and ‘. The first time I set the QueryOverride property I “forgot” to encode the query and then the CQWP gave no results. In addition I found a message in the ULS logs that saying that there was an error in the CAML query, not a great help!

I know, we’re in 2013 (SP era) and I’m talking about CAML… Maybe some of you (the lucky one) never wrote a CAML query or some other forgot how to write it. Even if I love CAML I’m not a caml-ninja at all, that’s why I love CAML Designer 2013, that you can download for free from the Belgian Information Workfer User Group web site (http://www.biwug.be/resources).

Long life to CAML!
Ri.

 

Submit a Comment