Pooja L
Pooja L

Reputation: 59

CAML Query filtering data within a date range

I want to retrieve items within a daterange from an SPList. The start date will be today and end date will be the day after 30 days from today. Here is my CAML query.

query.Query = string.Concat(@
    "<Where>
        <And>
          <Geq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today /></Value>
          </Geq>
          <Leq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today offset='30'/></Value>
          </Leq>
       </And>
     </Where>
     <OrderBy>
       <FieldRef Name='EventDate' Ascending='True' />
     </OrderBy>");
SPListItemCollection items = list.GetItems(query);

But this will return only the items having today's date.

Upvotes: 2

Views: 15023

Answers (1)

temarsden
temarsden

Reputation: 332

Try this, using OffsetDays or Offset not offset in the <Today/> value...

query.Query = string.Concat(@
    "<Where>
        <And>
          <Geq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today /></Value>
          </Geq>
          <Leq>
            <FieldRef Name='EventDate' />
            <Value IncludeTimeValue='False' Type='DateTime'><Today OffsetDays='30'/></Value>
          </Leq>
       </And>
     </Where>
     <OrderBy>
       <FieldRef Name='EventDate' Ascending='True' />
     </OrderBy>");

What is the difference between CAML Offset and OffsetDays?

Alternatively you could create DateTime objects and use the SPUtility.CreateISO8601DateTimeFromSystemDateTime method

Upvotes: 1

Related Questions