I have a CAML query that queries a calendar list and I want it to return all the items in the list with an EventDate of today or later, ordered in ascending order. The CAML query I'm using is this:
<Query><Where><Geq><FieldRef Name="EventDate" /><Value Type="DateTime"><Today/></Value></Geq></Where><OrderBy><FieldRef Name="EventDate" Ascending="True" /></OrderBy></Query>
When I run the query in U2U's CAML builder I see the results I expect but when it's run in the WebPart I'm using to render the list into a page all of the items are fetched including those with a date earlier than today. Here's the relevant part of the code:
Dim site As SPSite = SPContext.Current.Site
Using web As SPWeb = site.OpenWeb
Dim controlHTML As New StringBuilder()
Try
Dim list As SPList = web.Lists("Diary")
defaultViewURL = list.DefaultViewUrl
controlHTML.Append(FirstPart)
Dim itemsByDateQuery As New SPQuery
itemsByDateQuery.Query = "<Query><Where><Geq><FieldRef Name=""EventDate"" /><Value Type=""DateTime""><Today/></Value></Geq></Where><OrderBy><FieldRef Name=""EventDate"" Ascending=""True"" /></OrderBy></Query>"
Dim items As SPListItemCollection = list.GetItems(itemsByDateQuery)
Dim itemsToShowCount As Integer = Math.Min(items.Count, Me.ItemsToShow)
controlHTML.AppendLine(DiaryTable(items, itemsToShowCount))
controlHTML.AppendLine("<p>" & System.Web.HttpUtility.HtmlEncode(itemsByDateQuery.Query) & "</p>")
Catch ex As Exception
controlHTML.AppendLine(String.Format("<p>Couldn’t render the list. The error was {0}", ex.Message))
End Try
controlHTML.AppendLine(LastPart)
Me.Controls.Add(New LiteralControl(controlHTML.ToString))
End Using
Am I missing something obvious here? I don't understand why the query is returning items with an EventDate earlier than today when run in the WebPart but not when the CAML is run in CAML Builder.