简体   繁体   中英

CAML query retrieving date range in a sharepoint calendar

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());
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM