如何从共享点列表中获取所有文件夹和项目?
我有一个包含文件夹和项目的列表。文件夹是基于文件夹的特定内容类型,但具有属性。
文件夹可以包含子文件夹和子项目。子文件夹可以包含子子文件夹等等。我已经设法使用这种方式获取所有项目和文件夹:
void TraverseList(SPList list)
{
Trace.WriteLine("Traversing list: " + list.Title);
Trace.WriteLine("Base type: " + list.BaseType.ToString());
TraverseListFolder(list.RootFolder);
}
void TraverseListFolder(SPFolder folder)
{
SPQuery qry = new SPQuery();
qry.Folder = folder;
Trace.WriteLine("Foldername: " + folder.Name);
SPWeb web = null;
try
{
web = folder.ParentWeb;
SPListItemCollection ic = web.Lists[folder.ParentListId].GetItems(qry);
foreach (SPListItem subitem in ic)
{
SPFieldLookupValue temp = new SPFieldLookupValue(subitem["TargetPage"].ToString());
Trace.WriteLine("TargetPage: " + temp);
Trace.WriteLine("ItemName: " + subitem.Name);
if (subitem.Folder != null)
{
TraverseListFolder(subitem.Folder);
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
throw;
}
finally
{
if (web != null)
{
web.Dispose();
}
}
}
此解决方案中的问题是我必须为每个文件夹发送一个新查询,当列表不断增长时,这会变得性能不佳。有没有一种方法可以通过一次调用获取整个列表,而不丢失文件夹/项目结构?
感谢您阅读本文!
编辑:使用 CAML 不是必需的。但有一个限制我忘记了:由于客户限制,我无法使用网络服务。
I have a list containing folders and items. The folders are a specific content type based on folder, but with properties.
A folder can contain subfolders and subitems. A subfolder can contain sub-subfolders and so on. I already managed to get all items and folders using this way:
void TraverseList(SPList list)
{
Trace.WriteLine("Traversing list: " + list.Title);
Trace.WriteLine("Base type: " + list.BaseType.ToString());
TraverseListFolder(list.RootFolder);
}
void TraverseListFolder(SPFolder folder)
{
SPQuery qry = new SPQuery();
qry.Folder = folder;
Trace.WriteLine("Foldername: " + folder.Name);
SPWeb web = null;
try
{
web = folder.ParentWeb;
SPListItemCollection ic = web.Lists[folder.ParentListId].GetItems(qry);
foreach (SPListItem subitem in ic)
{
SPFieldLookupValue temp = new SPFieldLookupValue(subitem["TargetPage"].ToString());
Trace.WriteLine("TargetPage: " + temp);
Trace.WriteLine("ItemName: " + subitem.Name);
if (subitem.Folder != null)
{
TraverseListFolder(subitem.Folder);
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
throw;
}
finally
{
if (web != null)
{
web.Dispose();
}
}
}
The problem in this solution is that I have to send a new query for every folder, which is getting imperformant when the list is growing. Is there a way to get the whole list with one call, without loosing the folder/item structure?
Thank you for reading this!
Edit: It's not a requirement to use CAML. But there is a restriction I forgot: I'm not able to use webservices, due to customer restrictions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取根目录并使用
另请参阅:查询以获取列表中的所有项目,包括 SharePoint 中子文件夹中的项目
并且:http://www.ktskumar.com/blog/2009 /07/从列表中检索所有文件夹/
HTH
亚历克斯
Fetch the root and use
See also: Query to get all items in a list including items in the sub folders in SharePoint
And: http://www.ktskumar.com/blog/2009/07/retrieve-all-folders-from-list/
HTH
Alex