简体   繁体   English

在SharePoint 2010 CAML查询中比较日期

[英]Comparing dates in Sharepoint 2010 CAML query

I'm running into an interesting problem when trying to query a Sharepoint list. 尝试查询Sharepoint列表时遇到一个有趣的问题。

Here is the query so far: 到目前为止,这是查询:

camlQuery.ViewXml = new XElement("View",
    new XElement("Query",
        new XElement("Where",
            new XElement("And",
                new XElement("Geq",
                    new XElement("FieldRef", new XAttribute("Name", "Created_x0020_Date")),
                    new XElement("Value", new XAttribute("IncludeTimeValue", "TRUE"), new XAttribute("Type", "DateTime"), InStartDate.Get(context))),
                new XElement("Leq",
                    new XElement("FieldRef", new XAttribute("Name", "Created_x0020_Date")),
                        new XElement("Value", new XAttribute("IncludeTimeValue", "TRUE"), new XAttribute("Type", "DateTime"), InEndDate.Get(context))))))).ToString();

The Created_x0020_Date column is just when the row was created. Created_x0020_Date列仅在创建行时出现。

What's interesting is that, no matter what I make the start and end dates, the query errors out with "The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator." 有趣的是,无论我将开始日期和结束日期设为什么日期,查询错误都会显示为“尝试执行的操作被禁止,因为它超出了管理员强制执行的列表视图阈值”。 The error seems to think that I'm returning more rows than the threshold is prepared to handle. 该错误似乎认为我返回的行数超过了阈值准备处理的行数。 But this error occurs even when my end date is before my start date, which should mean that it's impossible to find a date that matches the where clause. 但是即使我的结束日期早于开始日期,也会发生此错误,这应该意味着不可能找到与where子句匹配的日期。

Let me know if I can provide any more information. 让我知道是否可以提供更多信息。

This has nothing whatsoever to do with dates, or how they are compared. 这与日期或如何进行日期无关。 You'd see this issue with just about any type of filtering that you try to perform on this list. 您将看到尝试在此列表上执行的几乎所有类型的过滤的此问题。

This is because the List View Threshold isn't about the size of the result set. 这是因为列表视图阈值与结果集的大小无关。

The LVT is triggered any time any intermediate result set is larger than the threshold. 任何中间结果集大于阈值时,都会触发LVT。 Your query requires searching all [size of your list] items in order to find out which items are less than/greater than the dates that you're searching on. 您的查询需要搜索所有[列表大小]项,以便找出哪些项小于/大于要搜索的日期。 The fact that the result set is smaller than the LVT doesn't matter; 结果集小于LVT的事实无关紧要; SharePoint gives up on you long before then. SharePoint在那之前很久就放弃了。

Of course, if your "Created Date" field had an index on it then it wouldn't need to search [size of list] items to figure out all of the dates within that range. 当然,如果您的“创建日期”字段上有索引,则无需搜索[列表大小]项目即可找出该范围内的所有日期。 It'd be able to use the index to get right at just those items, so in that case, as long as your result set is less than the LVT you're fine. 能够使用索引仅对那些项目正确,因此在这种情况下,只要您的结果集小于LVT,就可以了。

So as for actual solutions, you can: 因此,对于实际解决方案,您可以:

  • Add an index to this column. 向该列添加索引。 It may or may not be sufficient for your purposes as you may or may not be able to ensure that every query can use an indexed column to filter the list to less than the LVT every time. 它可能满足您的目的,也可能不够,因为您可能或可能无法确保每个查询每次都可以使用索引列来过滤列表,使其小于LVT。

  • Increase the LVT to greater than the size of the list. 将LVT增加到大于列表的大小。

  • Disable the LVT for this list. 禁用此列表的LVT。

  • Override the LVT for this query (which can be done when performing the queries in code) 覆盖此查询的LVT(可以在代码中执行查询时完成)

  • Ensure the user is an admin user (possibly privilege escalating) as they have a higher LVT. 确保他们是LVT较高的管理员用户(可能是特权升级)。

  • Make the list smaller, so that it is below the LVT. 将列表缩小,使其在LVT之下。

    • A variation on this is putting the items into folders in which each folder has less than the LVT. 对此的一种变化是将项目放入文件夹,其中每个文件夹的大小均小于LVT。 You can then do queries on each folder without issue (because an items folder is an indexed column). 然后,您可以对每个文件夹进行查询而不会出现问题(因为items文件夹是索引列)。
  • Pull down the entire list into memory (in pages less than the LVT) and perform your query on in-memory objects. 将整个列表下拉到内存中(比LVT少的页面),然后对内存中对象执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM