DateRangesOverlap always returns all results
-
29-09-2020 - |
Question
I have a problem with SPQuery and DataRangesOverlap.
In my calendar there are three events that don't happen today. In SPQuery.query I want to exclude these events. Unfortunately all events are returned. The contains filter for a text column works fine but the DataRangesOverlap doesn't do any filtering. Here is my code:
SPListCollection oLists = oSiteCollection.AllWebs["MYSITE"].Lists;
String res = "";
SPQuery spquery = new SPQuery();
string datasel = SPUtility.CreateISO8601DateTimeFromSystemDateTime(caldata.SelectedDate.Date);
spquery.Query = "<Where><And><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\" IncludeTimeValue=\"True\">Today</Value></DateRangesOverlap><Or><Contains><FieldRef Name=\"Title\" /><Value Type=\"Text\">" + tbnome.Text + "</Value></Contains></Or></And></Where>";
spquery.ExpandRecurrence = true;
spquery.RowLimit = 100;
foreach (SPList l in oLists) {
String title = l.Title;
if (title.IndexOf("MyCalendar")==0) {
SPListItemCollection items = l.GetItems(spquery);
foreach (SPListItem item in items)
{
res += "<li>" + item["Title"] + " date1 " + item["Start Time"] + " date2 " + item["End Time"] + "</li>";
}
}
}
res = "<ul>"+res+"</ul>";
Solution
I wrote
<Value Type=\"DateTime\" IncludeTimeValue=\"True\">Today</Value>
instead of
<Value Type=\"DateTime\" IncludeTimeValue=\"True\"><Today /></Value>
OTHER TIPS
Unfortunately, the caml date stuff pretty much sucks in SharePoint. It won't compare based on time and I think the daterangesoverlap only works for recurring events. Are the events you expect NOT to be shown recurring or single instance events?
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange