0

My company has a document library with several levels of sub folders. I'm aware this isn't ideal, meta data would be better, etc., but this is what I have to work with. I'm looking to list out the contents of this document library in an unordered list on an ASPX page. Each sublevel would be another nested UL, under a heading which would be the folder name.

Long story short - how do I A) get subfolder names in C#, and B) get the files in those subfolders?

I'm familiar with iterating through files ala

    SPSite site = new SPSite("****");
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists["docLibrary"];

    foreach (SPListItem i in list.Items)
    {
        formList.Text += i["Name"] + "<br />";
    }

but I don't quite know where to go from here. Thanks.

0

You can use below pesudocode in a recursive function to get all files and folders in a list:

foreach (SPFolder oFolder in List.RootFolder.SubFolders)
    {
            if(oFolder.Files.Count>0) 
    {
                           foreach (SPFile oFile in oFolder.Files)
            {

            }
    }

    }
0

The recursive approach is not a good idea IMO.

You are better off simply getting all of the objects and using the EncodedAbsUrl. Retrieve the objects using SPSiteDataQuery.

If the list size is large, consider doing it in chunks (paged) instead of retrieving the entire set at once.

Using the EncodedAbsUrl of the result is a good approach because the URL is inherently hierarchical and you can build the tree view pretty easily just by breaking the URL into the component parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.