Wednesday, 2 October 2013

DateTime Fields and CAML Queries , OffsetDays...

Hi Friends,
 Today I will share you some tips regarding CAML queries in SharePoint. Most of you would have played with CAML Queries.
OK , Now I will tel you how to deal with the Dates in CAML Query.

Scenario 1 : If you are checking for the items having "Due Date" field value as "Today", then this would be the query for u.

<Where>
<Eq>
<FieldRef Name="DueDate" />
<Value Type="DateTime">
<Today/>
</Value>
</Eq>
</Where>

Scenario 2 : what if u want something dynamic???
 EX: if u want to get the items, having "Due Date" 5 days before Today. For this you can't give a static day, because "Today" changes daily.
     Option is "OffsetDays".
The Query goes like this :

<Where>
<Eq>
<FieldRef Name="DueDate" />
<Value Type="DateTime">
<Today OffsetDays="-5" />  ----------> for future dates the value should be positive.
</Value>
</Eq>
</Where>

In many documents they have mentioned Offset instead of "OffsetDays"... better be careful while checking...

Have a good Day :)

2 comments: