SharePoint 的 List.GetItems(view) 返回所有项目而不是过滤的视图项目 [英] SharePoint's List.GetItems(view) returns ALL items instead of filtered view items

查看:42
本文介绍了SharePoint 的 List.GetItems(view) 返回所有项目而不是过滤的视图项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据视图获取列表中的项目数.到目前为止,我尝试过的每种方法都只返回列表的总数.我几乎尝试了搜索时遇到的所有方法,结果都是一样的.

I'm trying to get a count of items in a list, based on a view. Every method I've tried so far only returns the grand total of the list. I've tried just about every method I've run across while searching, and everything ends up with the same results.

这是我尝试过的方法之一:

Here's one of the methods I've tried:

SPWeb web = SPContext.Current.Web;
SPView view = web.GetViewFromUrl("url to my view");
int count = view.ParentList.GetItems(view).Count;

我的列表有 28 个项目,但我引用的视图对其进行过滤并显示四个项目.我希望我的计数是 4,而不是 28 - 但我总是得到 28.

My list has 28 items, but the view I'm referencing filters it and shows four items. I expect my count to be 4, not 28 - but 28 is what I always get.

这是我尝试过的另一种方法:

Here's another method I've tried:

SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();

SPQuery MyQuery = new SPQuery();
MyQuery.Query = "<Query><Where><Eq><FieldRef Name='strStatus' /><Value Type='Text'>submitted</Value></Eq></Where></Query>";

IEnumerable<SPListItem> results = web.Lists["Requests"].GetItems(MyQuery).Cast<SPListItem>();

// results.Count() is 28...  should be 4

所以在这个方法中,我跳过了视图,只是试图传入一个 CAML 查询.在 U2U 中测试我的查询时,按预期返回了四个结果...

So in this method I'm skipping the view and just trying to pass in a CAML query. When testing my query in U2U, four results are returned as expected...

更大的图景是我在自定义菜单控件的 OnMenuItemDataBound 事件处理程序中执行此操作.我不知道这是否有什么不同,但我的想法是链接到特定列表中的视图的每个项目都会在链接旁边显示该视图中的项目数.

The larger picture is that I'm doing this inside of my custom menu control's OnMenuItemDataBound event handler. I don't know if that makes a difference at all, but the idea I'm heading towards is that each item that links to a view in a specific list, will show the count of items in that view next to the link.

有什么想法为什么我得到的是列表总数而不是过滤后的总数?谢谢!

Any ideas why I'm getting a list total instead of the filtered totals? Thanks!

推荐答案

如果我提醒正确,您需要从 SPQuery 中删除 .CAML 构建器使用它,但在实际的SPQuery 中是不必要的.当然,您需要确保这些字段存在.

If I remind correctly, you need to remove the <Query> from your SPQuery. The CAML Builders use it but its unnecessary in the actual SPQuery. Of course you need to make sure the fields exist.

MyQuery.Query = "<Where><Eq><FieldRef Name='strStatus' /><Value Type='Text'>submitted</Value></Eq></Where>";

这篇关于SharePoint 的 List.GetItems(view) 返回所有项目而不是过滤的视图项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆