Search code examples
c#sharepointweb-partscaml

Datetime comparison in CAML Query for Sharepoint


i'm trying to have some item from a sharepoint list, depends on date in a custom column.

I've created my query with U2U Caml Builder, and that's worked but when I put it in my own code in my webpart, it always return to me all the items od the list.

Here is my code:

DateTime startDate = new DateTime(Int32.Parse(year), 1, 1);
            DateTime endDate = new DateTime(Int32.Parse(year), 12, 31);
            SPQuery q = new SPQuery();
            q.Query = "<Query><Where><And><Geq><FieldRef Name='Publicate Date' /><Value IncludeTimeValue='FALSE' Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(startDate) + "</Value></Geq><Leq><FieldRef Name='Publicate_x0020_Date' /><Value IncludeTimeValue='FALSE' Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(endDate) + "</Value></Leq></And></Where></Query>";

            SPListItemCollection allItem = library.GetItems(q);

Solution

  • You don't need the <Query> tag. That's why you query isn't being executed.