Tuesday, August 30, 2011

Using CAML to get items based on DateTime

Most of the Times we work on retrieve items from List or Document Library based on different Columns.This article is to target the datetime column.

Scenario 1: I want to get items which are greater than or equal to Today's date
<Query><Where><Geq><FieldRef Name="EndDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"><Today /></Value></Geq></Where><OrderBy><FieldRef Name="StartDate" Ascending="False" /></OrderBy></Query>

The attribute "Today" helps in defining the date to be Current Date
Scenario 2: We Want to get result from say 5 days from Today
<Query><Where><Geq><FieldRef Name="EndDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"><Today  Offset="5"/></Value></Geq></Where><OrderBy><FieldRef Name="StartDate" Ascending="False" /></OrderBy></Query>

The Attribute "Offset" defines the No.of days we want to go back




No comments:

Post a Comment