Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I need to use the CQWP with more than three filters, hence I'm going to use the QueryOverride with a CAML query like described here. I can't get this to work when deploying the web part instance using Elements.xml i.e. AllUsersWebPart and CDATA.

The way I did it with normal filters was like the following (from Elements.xml)

<AllUsersWebPart ID="FindingsCQWP" WebPartZoneID="Middle" WebPartOrder="1" >
          <![CDATA[
            <webParts>
              <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
                <metaData>
                  <type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
                  <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
                </metaData>
                <data>
                  <properties>
                    <property name="GroupBy" type="string" />
                    <property name="FilterValue2" type="string">Draft</property>
                    ... and so forth...

However QueryOverride requires using CDATA as well. The problem is that I dont' think 'nested' CDATA elements are allowed. I've tried HTML encoding the CAML as a workaround/approach, but to no avail.

What is the solution here? I guess I could write some feature receiver code, but I'd like to avoid that if I can.

share|improve this question
    
Have you considered extending the CQWP and set the query through code? –  eirikb Oct 22 '12 at 11:28
    
That seems to me like too much work, and if I'm going to code an easier approach is to set the property in the feature receiver code. –  tarjeieo Oct 22 '12 at 11:40
    
It is more work, true, but at least it would work :) I guess the feature event receiver approach could be discussed, in that case even using PS directly would be just as simple, or a console app –  eirikb Oct 22 '12 at 11:44
    
Well, I disagree that using PS or console app is just as simple. Sure, the code is much the same, but at least using a feature receiver the solution will automatically apply the change, while PS/console would need to be manually run after deploy. –  tarjeieo Oct 22 '12 at 12:00
    
It depends on your use case, but the point was that you would not need a deploy. Although I guess this is beside the point of your question –  eirikb Oct 22 '12 at 12:14
show 1 more comment

2 Answers

Kind of workaround, but still: deploy CQWP without this property to a page, set the property programmatically, and then export webpart to .webpart file.

I anticipate some kind of double HTML encoding nightmare like as a result.

P.S. Not tested.

P.S.S. If you will not be able to export the webpart from browser, try using programmatic approach (ExportWebPart method of SPLimitedWebPartManager).

share|improve this answer
    
Good idea. I used CDATA when the web part was deployed to the web part gallery, added it to the page and lo and behold it worked. The output was as you guessed some HTML encoding version of the string. It looks just like what I tried earlier, but I must have missed something, or maybe I had some encoding errors. I'll add a separate answer, as this is not really the answer to the question (but a good tip for getting there!) –  tarjeieo Oct 22 '12 at 12:48
add comment
up vote 0 down vote accepted

@Andrey's response pinpointed me to the answer of this question. The Query needs to be Encoded. I tried that in my initial approach, but I must have missed something.

I had this CAML that I wanted to have in my QueryOverride

<Where>
  <And>
     <And>
        <Or>
           <Eq>
              <FieldRef Name='Status' />
              <Value Type='Choice'>Not Started</Value>
           </Eq>
           <Eq>
              <FieldRef Name='Status' />
              <Value Type='Choice'>Not Closed</Value>
           </Eq>
        </Or>
        <Leq>
           <FieldRef Name='TargetImplDate' />
           <Value Type='DateTime'>
              <Today />
           </Value>
        </Leq>
     </And>
     <Eq>
        <FieldRef Name='Category' />
        <Value Type='Choice'></Value>
     </Eq>
  </And>
</Where>
<OrderBy>
  <FieldRef Name='ProcessChoice' />
  <FieldRef Name='TargetImplDate' />
</OrderBy>

Using CDATA when I added the web part to the web part gallery, and adding it to the page and exporting it afterwards got the result

<property name="QueryOverride" type="string">&lt;Where&gt;&lt;And&gt;&lt;And&gt;&lt;Or&gt;&lt;Eq&gt;&lt;FieldRef Name='Status' /&gt;&lt;Value Type='Choice'&gt;Not Started&lt;/Value&gt;&lt;/Eq&gt;&lt;Eq&gt;&lt;FieldRef Name='Status' /&gt;&lt;Value Type='Choice'&gt;Not Closed&lt;/Value&gt;&lt;/Eq&gt;&lt;/Or&gt;&lt;Leq&gt;&lt;FieldRef Name='TargetImplDate' /&gt;&lt;Value Type='DateTime'&gt;&lt;Today /&gt;&lt;/Value&gt;&lt;/Leq&gt;&lt;/And&gt;&lt;Eq&gt;&lt;FieldRef Name='Category' /&gt;&lt;Value Type='Choice'&gt;&lt;/Value&gt;&lt;/Eq&gt;&lt;/And&gt;&lt;/Where&gt;&lt;OrderBy&gt;&lt;FieldRef Name='ProcessChoice' /&gt;&lt;FieldRef Name='TargetImplDate' /&gt;&lt;/OrderBy&gt;</property>

which worked as expected. Thanks @Andrey

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.