确定 SPQuery 返回的项目总数

发布于 2024-09-28 22:09:22 字数 513 浏览 4 评论 0原文

我正在针对 SharePoint 2010 中的大型列表运行大量 CAML 查询,并在网格视图中向最终用户显示结果。我希望对查询返回的结果进行分页以提高性能。但是,我需要在分页控件上显示查询返回的项目的总数。我的问题是,如何确定每个查询将返回的项目总数,而不实际将它们全部返回到单个 SPListItemCollection 中?准确地说,我希望一次分页浏览 10 项结果;我怎样才能做到这一点并且仍然拥有查询返回的所有项目的总数?

更新

到目前为止,给出的答案都没有解决我的问题 - 因此,我提供赏金。我需要能够获取 CAML 查询将返回的项目总数,而无需运行查询并返回所有项目。这将使我能够向最终用户显示此总计数值(设定要求),同时对项目集合进行分页以在网格视图中显示特定的结果页面。这将避免首次加载包含 gridview 的页面上的大型列表对性能造成巨大影响。

如果没有人对上述问题提供有效的答案,我将接受一个答案,该答案提供了一篇 MSDN 文章的链接,该文章明确指出上述功能无法实现。

谢谢,魔术安迪。

I'm running a number of CAML queries against a large list in SharePoint 2010, and displaying the results in a gridview to the end user. I wish to page through the results returned by the query to improve performance. However, I am required to display a total count of the items returned by the query on the paging control. My question is, how can I determine the total number of items that will be returned by each query without actually returning them all in a single SPListItemCollection? To be precise, I wish to page through the results 10 items at a time; how can I do this and still have a total count of all items returned by the query?

Update

So far, none of the answers given have addressed my question - as such, I'm offering a bounty. I need to be able to get a total count of the number of items that a CAML query will return without having to run the query and return all of the items. This will enable me to display this total count value to the end user (a set requirement), while paging through the items collection to display a specific page of results in a gridview. This would avoid a massive performance hit for large lists on the page containing the gridview first loading.

If no one offers a valid answer to the above, I will accept an answer that gives a link to an MSDN article that explicitly says that the above functionality cannot be implemented.

Thanks, MagicAndi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

—━☆沉默づ 2024-10-05 22:09:22

不幸的是,我认为您不会从 MSDN 中得到明确的答案,说明什么是不可能……而只能是是什么。以下是 msdn 上的 CAML 查询架构页面的完整内容。那里没有提到任何类型的“计数”功能。

至于解决您的问题,首先您需要选择正确的查找项目的方法。例如,这里有几个案例:

  1. 一个列表中的项目? CAML
  2. 来自同一站点中几个列表的 项目? SPSiteDataQuery
  3. 来自许多站点的许多列表的项目?搜索 API

其次,您可以在 Web 部件中实现某种缓存,以减少排序/分页/筛选所需的时间。我喜欢使用 System.Web.HttpRuntime.Cache 来存储 DataTable (可以轻松更改检索项目的方法,同时网格可以继续使用数据表)。您还可以使用 Page.Session 而不是缓存。

Unfortunately, I don't think you're going to get a definitive answer from MSDN saying what isn't possible... only what is. Here is the entire CAML Query Schema page on msdn. There is no mention of any type of "count" function there.

As for solving your problem, first you need to choose the correct method for finding items. For example, here are a couple cases:

  1. Items from one list? CAML
  2. Items from a couple lists in the same site? SPSiteDataQuery
  3. Items from many lists in many sites? Search API

Second, you can implement some sort of caching in your web part to reduce the time it takes to sort/page/filter. I like using the System.Web.HttpRuntime.Cache to store a DataTable (makes it easy to change the method for retrieving items while your grid can keep using the datatable). You could also use Page.Session instead of caching.

狂之美人 2024-10-05 22:09:22

这是 SharePoint 的限制。这就是为什么 SharePoint 不显示您查看的页面数量。

It is a SharePoint limitation. This is why SharePoint doesn't show how many pages you have in view.

回忆凄美了谁 2024-10-05 22:09:22

您可以为该列表创建一个视图,每页的项目数限制为 10 个。然后使用 listview 控件(而不是 gridview)并绑定该视图。

You can create a view for that list with items per page limited to say 10. Then use a listview control (instead of gridview) and bind the view.

柏拉图鍀咏恒 2024-10-05 22:09:22

抱歉,CAML 查询没有通过...所以再一次

    <Aggregations Value="On">
        <FieldRef Name="LinkTitle" Type="COUNT"/>
    </Aggregations>

Sorry it seams the CAML query didn't go through... so once again

    <Aggregations Value="On">
        <FieldRef Name="LinkTitle" Type="COUNT"/>
    </Aggregations>
倾`听者〃 2024-10-05 22:09:22

只是花了几个小时试图弄清楚如何做到这一点......下面列出了您需要执行的操作才能获得完整列表计数。

1) 修改您正在访问的列表视图,并在总计中将 COUNT 添加到其中一列

2) 将以下代码添加到 Web 部件内的 CAML 查询

3)添加以下代码来显示计数

            <xsl:value-of select="/dsQueryResponse/Rows/Row/@*[name()='YOUR_COLUMN_NAME_FROM_FIRST_STEP.COUNT']"/>

并享受...我已经检查了使用和不使用过滤器的情况。

艺术

Just spent couple of hours trying to figure out how to do that.... Below is a list of things that you need to do to get the full list count.

1) Modify the list view that you are accessing and within the totals add COUNT to one of the Columns

2) Add the following code to the CAML query within your webpart

3) Add the following code for displaying the count

            <xsl:value-of select="/dsQueryResponse/Rows/Row/@*[name()='YOUR_COLUMN_NAME_FROM_FIRST_STEP.COUNT']"/>

And enjoy... I have checked it with and without Filter.

Art

双手揣兜 2024-10-05 22:09:22

不知道我们是否在同一页面上...但您可以非常简单地计算 SPQuery 收到的项目:

   SPQuery query = new SPQuery();
   query.Query = string.Concat(
                  "<Where><Eq>",
                     "<FieldRef Name='Status'/>",
                     "<Value Type='CHOICE'>Not Started</Value>",
                  "</Eq></Where>",
                  "<OrderBy>",
                     "<FieldRef Name='DueDate' Ascending='TRUE' />",
                     "<FieldRef Name=’Priority’ Ascending='TRUE' />", 
                  "</OrderBy>");                    


   SPListItemCollection items = list.GetItems(query);
   double totalCount = items.Count;

也许这就是您的解决方案。

Don't know if we are on the same page... but you can count items received by SPQuery quite simple:

   SPQuery query = new SPQuery();
   query.Query = string.Concat(
                  "<Where><Eq>",
                     "<FieldRef Name='Status'/>",
                     "<Value Type='CHOICE'>Not Started</Value>",
                  "</Eq></Where>",
                  "<OrderBy>",
                     "<FieldRef Name='DueDate' Ascending='TRUE' />",
                     "<FieldRef Name=’Priority’ Ascending='TRUE' />", 
                  "</OrderBy>");                    


   SPListItemCollection items = list.GetItems(query);
   double totalCount = items.Count;

Maybe this is your solution.

撑一把青伞 2024-10-05 22:09:22

可能有两种解决方案,两者都有限制:

  • 执行没有定义视场的轻量级查询
  • 使用“无限分页器”(例如,如果您使用 DataGrid,请将 VirtualItemCount 设置为 10K 或以上)

Is possible two solutions, both with limitations:

  • perform lightweight query with no defined viewfields
  • use 'unlimited pager'(ex. if you use DataGrid, set VirtualItemCount to 10K or above)
很糊涂小朋友 2024-10-05 22:09:22

我只找到了一种“肮脏”的方式,完全超出了任何支持的范围:)
我已经在数据库上创建了一个视图并滥用 SQL Server 来为我计数。
完全损坏,完全不受支持,绝对会咬自己,但这是我发现的计算大量记录的唯一方法。
没有使用它,因为它对生产不安全。

编辑:所有列表数据都转储到一个巨大的表 AllUserData 中,如果您有 tp_listid,您可以创建一个计数,

SELECT        COUNT(nvarchar7) AS Expr1
FROM          dbo.AllUserData
WHERE        (tp_ListId = 'xxxxxxxx-9999-3333-7777-yyyyyyyyyyyy')

但是如果您使用它们等,则必须开始过滤版本

WHERE        (tp_Version =
             (SELECT        MAX(tp_Version) AS ex1
             FROM            {{view above}} AS V1
             WHERE        (key = M.key)))

。您可以获得计数,并且可以得到它速度快但脚遇枪......

I have only found a "dirty" way that's totally outside anything supported :)
I've created a view on the database and abused SQL server to count for me.
Totally broken and totally non supported and absolutely going to bite myself but the only way I've found to get a count on a large set of records.
Did not use it as it's not safe for production.

Edit: all list data is dumped into one huge table AllUserData and if you have the tp_listid you can create a count

SELECT        COUNT(nvarchar7) AS Expr1
FROM          dbo.AllUserData
WHERE        (tp_ListId = 'xxxxxxxx-9999-3333-7777-yyyyyyyyyyyy')

But then you have to start filtering for versions if you use them

WHERE        (tp_Version =
             (SELECT        MAX(tp_Version) AS ex1
             FROM            {{view above}} AS V1
             WHERE        (key = M.key)))

etc etc. You can get the count and you can get it very fast but foot meet gun.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文