比较 CAML 查询中每日、每周和每月持续时间的日期时间



我的列表中插入了 3 个日期,它们是:

  • 2014/1/1 19:00
  • 2013/12/2 每周19:00
  • 12/
  • 26/2013 19:00 每月

我希望 CAML 查询的方式是我当前的日期时间是 1/2/2014 6:22 PM。我希望所有这些记录都处于下一个小时和给定的持续时间时被选中。
请问任何人都可以建议我如何为此编写查询吗?

CAML 查询如下所示:

<Query>
<Where>
  <Or>
     <And>
        <Eq>
           <FieldRef Name='DeliveryFrequency' />
           <Value Type='Choice'>Daily</Value>
        </Eq>
        <And>
           <Geq>
              <FieldRef Name='LastDeliveryTime' />
              <Value Type='DateTime'>[Today-1Day(s)]</Value>
           </Geq>
           <And>
              <Geq>
                 <FieldRef Name='LastDeliveryTime' />
                 <Value IncludeTimeValue='TRUE' Type='DateTime'>[Now-1Day(s)]</Value>
              </Geq>
              <Leq>
                 <FieldRef Name='LastDeliveryTime' />
                 <Value IncludeTimeValue='TRUE' Type='DateTime'>[Now+60Minute(s)]</Value>
              </Leq>
           </And>
        </And>
     </And>
     <Or>
        <And>
           <Eq>
              <FieldRef Name='DeliveryFrequency' />
              <Value Type='Choice'>Weekly</Value>
           </Eq>
           <And>
              <Eq>
                 <FieldRef Name='LastDeliveryTime' />
                 <Value Type='DateTime'>[Today-7Day(s)]</Value>
              </Eq>
              <And>
                 <Geq>
                    <FieldRef Name='LastDeliveryTime' />
                    <Value IncludeTimeValue='TRUE' Type='DateTime'>[Now-7Day(s)]</Value>
                 </Geq>
                 <Leq>
                    <FieldRef Name='LastDeliveryTime' />
                    <Value IncludeTimeValue='TRUE' Type='DateTime'>[Now-7Day(s)+60Minute(s)]</Value>
                 </Leq>
              </And>
           </And>
        </And>
        <And>
           <Eq>
              <FieldRef Name='DeliveryFrequency' />
              <Value Type='Choice'>Monthly</Value>
           </Eq>
           <And>
              <Eq>
                 <FieldRef Name='LastDeliveryTime' />
                 <Value Type='DateTime'>[Today-1Month(s)]</Value>
              </Eq>
              <And>
                 <Geq>
                    <FieldRef Name='LastDeliveryTime' />
                    <Value IncludeTimeValue='TRUE' Type='DateTime'>[Now-1Month(s)]</Value>
                 </Geq>
                 <Leq>
                    <FieldRef Name='LastDeliveryTime' />
                    <Value IncludeTimeValue='TRUE' Type='DateTime'>[Now-1Month(s)+60Minute(s)]</Value>
                 </Leq>
              </And>
           </And>
        </And>
     </Or>
  </Or>

这是我的查询在 CAML 查询生成器中工作正常,但在 SPQuery 中它不返回任何记录。我尝试通过向"RecursiveAll"提供ViewAttributes。给定的代码有什么问题?

我已经自己解决了这个错误。我已经从代码端删除了像今天和现在这样具有我自己的价值的内置函数。

  Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(
DateTime.Now.AddMonths(-1))

更完整的示例

string queryString_getPersonalAlerts = string.Format(@"<Where><Or>
            <And><Eq><FieldRef Name='{0}' />
            <Value Type='Choice'>Daily</Value></Eq><And><Geq><FieldRef Name='{1}' />
            <Value Type='DateTime'>{2}</Value></Geq><And><Geq><FieldRef Name='{1}' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>{2}</Value></Geq><Leq><FieldRef Name='{1}' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>{3}</Value></Leq></And></And></And><Or><And><Eq><FieldRef Name='{0}' />
            <Value Type='Choice'>Weekly</Value></Eq><And><Eq><FieldRef Name='{1}' />
            <Value Type='DateTime'>{4}</Value></Eq><And><Geq><FieldRef Name='{1}' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>{4}</Value></Geq><Leq><FieldRef Name='{1}' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>{5}</Value></Leq></And></And></And><And><Eq><FieldRef Name='{0}' />
            <Value Type='Choice'>Monthly</Value></Eq><And><Eq><FieldRef Name='{1}' />
            <Value Type='DateTime'>{6}</Value></Eq><And><Geq><FieldRef Name='{1}' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>{6}</Value></Geq><Leq><FieldRef Name='{1}' />
            <Value IncludeTimeValue='TRUE' Type='DateTime'>{7}</Value></Leq></And></And></And></Or></Or></Where>",    
         PersonalAlerts.Fields.DeliveryFrequency.ToString(),     
        PersonalAlerts.Fields.LastDeliveryTime.ToString(),
    SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(-1)), 
        SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(-1).AddMinutes(60)), 
SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(-7)), 
        SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddDays(-7).AddMinutes(60)), 
        SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddMonths(-1)), 
        SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddMonths(-1).AddMinutes(60)));

相关内容

  • 没有找到相关文章

最新更新