Tuesday, September 1, 2009

Finding items between dates

Scenario:
Wanted to find out documents getting expired in next 30 min ?
Wanted to find out announcements getting expired in next 30 min ?
Wanted to find out news going live in next 30 min ?

Solution:
Simple CAML query can do the trick. You can also tweak it to do things like..

Documents got modified in past 30 min ?
Issues created in past 30 min ?

Code:

int gapInMin  = 30;

string currentDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now);
string currentDatePlusMinutes = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddMinutes(gapInMin));


string CAMLQuery = string.Format(@"<Where>"+
"<And>"+
"<Gt>"+
"<FieldRef Name='Expires' />"+
"<Value Type='DateTime' IncludeTimeValue='TRUE' >{0}</Value>"+
"</Gt>"+
"<Leq>"+
"<FieldRef Name='Expires' />"+
"<Value Type='DateTime' IncludeTimeValue='TRUE' >{1}</Value>"+
"</Leq>"+
"</And>"+
"</Where>",currentDate,currentDatePlusMinutes);

0 comments: