开发者

SharePoint's List.GetItems(view) returns ALL items instead of filtered view items

开发者 https://www.devze.com 2022-12-24 01:07 出处:网络
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 sea

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;

My list has 28 items, but the view I'm referencing filters it and开发者_JS百科 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

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...

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!


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>";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号