Recursive function for traversing List folders and all folder Items
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;
namespace Location
{
class TraverseListItems
{
static void Main(string[] args)
{
using (SPSite
siteCollection = new SPSite("http://ServerName/"))
{
using (SPWeb
spweb = siteCollection.OpenWeb())
{
SPList splist = spweb.Lists["ListName"];
Trace.WriteLine("Traversing list: " + splist.Title);
Trace.WriteLine("Base type: " +
splist.BaseType.ToString());
TraverseListFolder(splist.RootFolder);
Console.ReadLine();
}
//SPWebCollection sites =
siteCollection.AllWebs;
//SPList spList=sites
}
}
private static void TraverseListFolder(SPFolder
folder)
{
SPQuery qry = new SPQuery();
qry.Folder = folder;
Console.WriteLine("Folder--
Foldername: " + folder.Name);
SPWeb web = null;
try
{
web = folder.ParentWeb;
SPListItemCollection ic = web.Lists[folder.ParentListId].GetItems(qry);
foreach (SPListItem
subitem in ic)
{
Console.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();
}
}
}
}
}