Wednesday, June 24, 2009

Using the Today token in a CAML query

I just noticed today that the CAML creator application that I am using (by U2U) does not handle the today token properly (at least the version I have).
The Today token is used in views, when you want to filter on a date based on the current date. For example, if you want to set up a view that shows future events you set the filter on start date > "[Today]".

However, in CAML, you cannot use "[Today]" - the square brackets cause the query to break. Instead, you use "<Today />".
For example:
<Where><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'><Today /></Value></Geq></Where>


Sandeep said...

I think we can also do Today - 7 to get all items from past one week...

Francois said...

To add or retract days with the "Today" tag, use the 'OffsetDays' property:
* <Today OffsetDays="-5" /> = today - 5 days
* <Today OffsetDays="5" /> = today + 5 days

Karine Bosch said...

Hi Ishai,

[Today] is translated in <Today /> in the assemblies of the CAML query builder. I added this (and other things) to make working with DateTime values more flexible. You could reference and use these assemblies in your projects but that's not generally known.
Perhaps I should rethink this when making a new version of the tool.