Thursday, October 29, 2009

<Lt> and <Gt> CAML Mess!

Ok, CAML SharePoint, we had came to love (and hate). The darn thing still surprises me even after few years of regular use! Okay maybe I am not technical enough, let me explain, and you judge!

Was dealing with some date/time checking, and came came across the need to compare some dates, the initial query as following:

<Where>
  <
Lt>
    <
FieldRef Name='StartDate' />
    <
Value Type='DateTime' IncludeTimeValue='true'>{0}</Value>
  </
Lt>
</
Where>

Okay, this executes and give me all data where with StartDate < {0}. As expected, so far. Now, the business logic changed, I would need to get the opposites instead: {0} < StartDate, so, I did some small logical change on the xml by reversing the FiedRef and Value tags’ position:

<Where>
  <
Lt>
    <Value Type='DateTime' IncludeTimeValue='true'>{0}</Value>
    <FieldRef Name='StartDate' />
  </
Lt>
</
Where>

Logic? No!!! To my horror, this query gives me the exact same result as previous!!!!  I am shooting myself in the foot, I should just use Gt instead of changing the order… Anyway, now only I learnt (after 5 years using CAML), that <Lt> and <Gt> does not honors the order of the elements inside!!! It will always return FieldRef Gt/Lt Value. *Ouch*

Nice catch CAML, nice one.

No comments:

Post a Comment