CAML Query filtering data within a date range

13,447

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

Share:
13,447
Pooja L
Author by

Pooja L

Updated on June 04, 2022

Comments

  • Pooja L
    Pooja L almost 2 years

    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.