Thursday, October 15, 2009

CAML: Working with DateTime Queries in SharePoint


Reference: SharePoint Magazine – Writing CAML Queries for Retrieving List Items From A SharePoint List

<Where><Gt><FieldRef Name="StartDate" /><Value Type="DateTime">2009-08-10T10:00:00Z</Value></Gt></Where>

-- Important! Your date must be in ISO8601 format so that it can be identified by MOSS. Use SPUtility.CreateISO8601DateTimeFromSystemDateTime(..)

-- The query returns all list items with a start date as of Aug 10 2009

-- The query does NOT take the time element into account



<Where><Gt><FieldRef Name="StartDate" IncludeTimeValue="TRUE" /><Value Type="DateTime">2009-08-10T10:00:00Z</Value></Gt></Where>

-- The query returns all list items with start date as of Aug 10 2009, 10am.



<Where><Gt><FieldRef Name="StartDate" /><Value Type="DateTime"><Today /></Value></Gt></Where>

-- Today does not take into account the time part

-- There is no Now element (unfortunately)



<Where><Gt><FieldRef Name="StartDate" /><Value Type="DateTime"><Today Offset="10" /></Value></Gt></Where>

-- Offset attribute adds or subtracts the given number of days from today's date

-- Negative is -10
--if Today Offset="1"  does not work for you, try Today OffsetDays="1" 

No comments:

Post a Comment