SharePoint: Content Query Web Part, more than three filters, QueryOverride and Logs

Scenario:

We need to get list items from all lists in a site collection based upon five conditions. OOTB content query web part (CWQP) provides three filters at max. The way foreword is to write a CAML query and put it inside QueryOverride property. To do this, you need to export OOTB CQWP web part. Open it in notepad or any XML viewer tool and set the property.

We wrote following CAML Query and set the QueryOverride property.

<property
name=QueryOverride
type=string ><![CDATA[

<Where>

<Or>

<And>

<And>

<Eq>

<FieldRef
Name=AnnouncementPropagate
Nullable=True
Type=Boolean/>

<Value
Type=Boolean>1</Value>

</Eq>

<Eq>

<FieldRef
Name=Region
Nullable=True
Type=Choice/>

<Value
Type=Choice>XXXXXXX</Value>

</Eq>

</And>

<Gt>

<FieldRef
Name=Expires
Nullable=True
Type=DateTime/>

<Value
Type=DateTime>

<Today/>

</Value>

</Gt>

</And>

<And>

<Eq>

<FieldRef
Name=Region
Nullable=True
Type=Choice/>

<Value
Type=Choice>
XXXXXXX</Value>

</Eq>

<Gt>

<FieldRef
Name=Expires
Nullable=True
Type=DateTime/>

<Value
Type=DateTime>

<Today/>

</Value>

</Gt>

</And>

</Or>

</Where>

]]></property>

An Or operator with three and two ANDs. Uploaded the web part and selected first option “Show items from all sites in this site collection”. After clicking “Apply”, web part display following error:

There is a problem with the query that this Web Part is issuing. Check the configuration of this Web Part and try again.

Resolution:

Tried different options and found out if we select a single list, web part works fine and if we select other two options, web part display above error.

SharePoint write the actual query created by selecting filters in the tool part of CQWP to the LOG file. So, we first configured three filters for three AND’s and copy the query from the Log file. Then, configured two filters for two ANDs and copy the query.  Joined them with Or and it did the trick.

Advertisements

4 Comments »

  1. nfatima said

    very useful! keep posting for SharePoint enthusiasts

  2. Daniel said

    Hi

    I am trying to do something similar but but with a custom CCWP built in VS2010 . I am trying some wizzy stuff in code to build the query dynamically. I see your error ….
    =
    Firstly, have you got around the single list constraint by setting the WebsOverride = Webs Recursive.

    In addition, I am having trouble getting the QueryOverride to replace query held in the UI … >. I know the query is being overriden as I am no longer able to select my custom content type however .. The UI still displays the “announcement” list CT – when it should be showing my custom CT.

    Daniel

    • Farhan Faiz said

      Daniel

      Did you try selecting list ONLY in the tool part?

      farhan

  3. Daniel said

    Hmmmm. For some reason I am getting a
    System.NullReferenceException: Object reference not set to an instance of an object.
    at Microsoft.SharePoint.Publishing.Internal.WebControls.ObjectSerializer.evaluateTestAutomationId(CachedList cachedListToEval)
    When I attempt To change the query scope in the UI to a single list. The though my CQWP is returning some content – go figure

    Whilst I strip back my code so that I eliminate probably cause. Would it help if I email you the contents of my OnInit method as this is where I am attempting my set QueryOverride Property.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: