CAML查询过滤日期范围内的数据

时间:2015-08-07 09:04:36

标签: c# asp.net sharepoint caml

我想从SPList检索日期范围内的项目。开始日期为今天,结束日期为自今天起30天后的第二天。这是我的CAML查询。

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);

但这只会返回具有今天日期的项目。

1 个答案:

答案 0 :(得分:1)

OffsetDays值中使用Offsetoffset而非<Today/>尝试此操作...

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?

或者,您可以创建DateTime个对象并使用SPUtility.CreateISO8601DateTimeFromSystemDateTime method