Recursive function to go through folders

27/10/2012 14:09

 

 SPWeb web = SPContext.Current.Web;
            string folderUrl = SPUtility.ConcatUrls(web.Url, "Pages");
            SPFolder folder = web.GetFolder(folderUrl);
            SPFileCollection files = folder.Files;
 
            StringBuilder writer = new StringBuilder();
            writer.Append("<ul>");
 
            VisitAllSubfolders(folder, writer);
        
            writer.Append("</ul>");
            lblInfo.Text = writer.ToString(); //lblInfo is label in aspx page
 
private void VisitAllSubfolders(SPFolder folder, StringBuilder writer)
        {
            if (folder.SubFolders != null)
            {
                writer.Append("\t");
                foreach (SPFolder subfolder in folder.SubFolders)
                {
                    if (subfolder.Name != "Forms")
                    {
                        VisitAllSubfolders(subfolder, writer);
                    }
                }
            }
            writer.Append(string.Format("<b>{0}</b></br>", folder.Name));
            SPFileCollection files = folder.Files;
            foreach (SPFile file in files)
            {
                writer.Append(string.Format("<li>{0}</li>", file.Name));
            }
 
        }