0

I am using CAML query to retrieve events in a range of dates. Currently, it retrieves nothing eventhough there are events in the range of dates

Is there something wrong with my CAML Query? It can retrieve all events when i take away the query line

Here is my code:

DateTime todayDate = DateTime.Now.Date; 

DateTime tomorrowDate = todayDate.AddDays(1);
tomorrowDate = tomorrowDate.AddSeconds(-1);

query.Query = "<Query><Where><And><Geq><FieldRef Name=\"StartTime\" /><value IncludeTimeValue=\"true\" type=\"DateTime\">" + todayDate + "</value></Geq><leq><FieldRef Name=\"EndDate\"/><Value IncludeTimeValue=\"true\" Type=\"DateTime\">" + tomorrowDate + "</value></leq></And></Where><Query>";
query.ExpandRecurrence = true; 

query.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='EventDate' /><FieldRef Name='EndDate' />";

SPListItemCollection items = list.GetItems(query);

foreach (SPListItem listItem in items)
{
    retrievedData.Add(listItem["Title"].ToString());
    retrievedData.Add(listItem["EventDate"].ToString());
    retrievedData.Add(listItem["EndDate"].ToString());
}
user247702
  • 23,641
  • 15
  • 110
  • 157
Li Failure
  • 1
  • 2
  • 4

1 Answers1

0

Your query contains elements value and leq with incorrect casing. The correct casing is Value and Leq (see this). You also use incorrect date format in the query. You must format the date to string using the SPUtility.CreateISO8601DateTimeFromSystemDateTime method (see this). Also use FALSE/TRUE instead of false/true in IncludeTimeValue attribute.

tomasdeml
  • 339
  • 2
  • 7