Monday, April 13, 2009

Recursively reading file title

Scenario:
Some one asked me how to recursively read file titles from all the subfolders.

Solution:
Here's a recursive way to get. Not the very best as you can use CAML query to get the same information. But for treeview below code will work.

Code:

SPWeb web= SPContext.Current.Web; 
SPList list = web.Lists["List_Name"];

foreach(SPFolder rootfolder in list.Folders)
ReadFilesfromFolder(rootfolder);
Code:
public void ReadFilesfromFolder(SPFolder folder)
{
foreach (SPFile file in folder.Files)
{
Console.WriteLine("File Title: " + file.Title) ;
foreach (SPFolder subfolder in folder.SubFolders)
ReadFilesfromFolder(subfolder);
}
}
Article:
http://blog.krichie.com/2007/01/30/traversing-sharepoint-list-folder-hierarchies/

0 comments: