如何在 SPFolder 中查找项目数? [英] How to find item count in a SPFolder?

查看:44
本文介绍了如何在 SPFolder 中查找项目数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以文件夹层次结构存储项目的列表.

I have a List that stores items in a folder hierarchy.

我注意到 SPFolder.Files.Count 始终为零.

I notice that SPFolder.Files.Count is always zero.

有没有办法找出一个文件夹中有多少个列表项?

Is there a way to find out how many list items are there in a folder?

推荐答案

我认为您正在寻找直系子项而不是后代(例如子文件夹中的项目).

I presume you are looking for direct children and not descendants (like items within a sub-folder).

您还想在计数中包括子文件夹吗?在这种情况下,您可以使用:SPFolder.ItemCount.

Do you also want to include sub-folders in the count? In which case you can use: SPFolder.ItemCount.

如果您只想要不是子文件夹的直接子列表项,那么您可以执行以下操作:

If you just want only the direct child listItems which are not subfolders then you can do something like the following:

using (SPSite site = new SPSite(mySPSite))
{
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists[myList];
    SPFolder folderInstance = list.RootFolder.SubFolders[folderUrl];

    SPQuery query = new SPQuery() ;
    query.Folder = folderInstance;

    SPListItemCollection items = list.GetItems(query) ;

    Console.WriteLine(items.Count);
}

我没试过.如果查询返回文件夹,您可能需要添加一个 where 子句来消除文件夹.

I haven't tried it. You might have to add a where clause to eliminate folders, if the query is returning that.

如果您想包含所有列表项,即使在子文件夹中,请将 SPQuery.ViewAttributes 字段设置为 query.ViewAttributes = "Scope=\"Recursive\"";

If you want to include all list-items, even within subfolders, set the SPQuery.ViewAttributes field as query.ViewAttributes = "Scope=\"Recursive\"";

这篇关于如何在 SPFolder 中查找项目数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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