CAML Query not ordering properly

Jason picture Jason · Feb 27, 2009 · Viewed 8.6k times · Source

Can anyone help me with this CAML query? When I flip the Ascending attribute from TRUE to FALSE (have also tried True and False), it doesn't re-order the result set.

The rest of the CAML is correct, it is being generated by a tool and the appropriate results are being returned.

<Where>
  <And>
    <And>
      <Eq>
        <FieldRef Name="Branch"/>
        <Value Type="Text">Camp 1</Value>
      </Eq>      
      <Eq>
        <FieldRef Name="Type"/>
        <Value Type="Choice">Day</Value>
      </Eq>
    </And>
    <Geq>
      <FieldRef Name="StartDateTime"/>
      <Value Type="DateTime">2009-01-05T00:00:00Z</Value>
    </Geq>
  </And>
  <OrderBy>
    <FieldRef Ascending="TRUE" Name="Title" />
  </OrderBy>
</Where>

Answer

Jamie McAllister picture Jamie McAllister · Feb 27, 2009

Doesn't the OrderBy have to be outside the Where clause?

    <Where>
  <And>
    <And>
      <Eq>
        <FieldRef Name="Branch"/>
        <Value Type="Text">Camp 1</Value>
      </Eq>      
      <Eq>
        <FieldRef Name="Type"/>
        <Value Type="Choice">Day</Value>
      </Eq>
    </And>
    <Geq>
      <FieldRef Name="StartDateTime"/>
      <Value Type="DateTime">2009-01-05T00:00:00Z</Value>
    </Geq>
  </And>
  </Where>
<OrderBy>
    <FieldRef Ascending="TRUE" Name="Title" />
  </OrderBy>

See http://msdn.microsoft.com/en-us/library/ms442728.aspx