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

时间:2014-01-02 12:55:24

标签: sharepoint sharepoint-2010 caml

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

  • 2014年1月1日下午7:00
  • 12/2/2013每周7:00
  • 12/26/2013每月7:00 PM

我希望CAML查询的方式是我当前的DateTime 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中它不返回任何记录。我试过将ViewAttributes提供给“RecursiveAll”。给定代码有什么问题?

1 个答案:

答案 0 :(得分:1)

我已经解决了这个错误。 我已经删除了像今天这样的Inbuilt函数,现在我已经从代码端使用了我自己的值。

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