0

I have a list containing folders and items. The folders are a custom content type based on folders, with some extra columns like URL and some Yes/No Checks. One contenttype can contain n items and contenttypes.

So lets say I have this structure:

FolderA
   ItemA.1
   FolderA.2
      ItemA.2.1
      ItemA.2.2
   FolderA.3
FolderB
  ItemB.1
  ItemB.2

How can I get this structure without using the list-webservice? I know how to get all items in the list, but that returns only items, not the content types. It's also (in my opinion) no good way to do a single query against each folder because the list will get very large.

Thank you very much!

1

I found a working solution on my own:

http://blog.krichie.com/2007/01/30/traversing-sharepoint-list-folder-hierarchies/

Edit (5 years later):

From the site mentioned above

It’s actually very easy, but I would vote it’s a horrible way to have to go about it.

Enter SPQuery:

private static void TraverseListFolder(SPFolder folder)
{
    // Get the collection of items from this folder
    SPQuery qry = new SPQuery();
    qry.Folder = folder;

    SPWeb web = null;
    try {
        web = folder.ParentWeb;
        SPListItemCollection ic = web.Lists[folder.ParentListId].GetItems(qry);

      foreach (SPListItem subitem in ic){
            Console.WriteLine(“List item: “ + subitem.Name);
         Console.WriteLine(“List item type: “ + subitem.ContentType.Name);

            if (subitem.Folder != null)
                TraverseListFolder(subitem.Folder);
     }
  } catch (Exception e) {
        Console.WriteLine(e.Message);
  } finally {
        // Cleanup that nasty web reference.
      if (web != null)
            web.Dispose();    
  }
}

Ok so cool, we now have a way to get ONLY the items in a folder within a list at any level without introducing any items that are not in scope.

So, we’ll just use this method, and to traverse the list, but hey, where do you start? Well, fortunately SPList exposes a RootFolder property that gives you a SPFolder object to start with J

So we can write our List traversal Kick off method as such:

private static void TraverseList(SPList list)
{
    Console.WriteLine(“Traversing list: “ + list.Title);
    Console.WriteLine(“Base Type: “ + list.BaseType.ToString());
    TraverseListFolder(list.RootFolder);
}

Simply pass in a reference to a list, and away we go. Very compact, and it works regardless if the list being passed in is a Document Library or any kind of list.

  • 1
    +1 Good job finding your solution. You should summarize what you learnt from the link here (in case the link goes down, and to have a more useful post here). – Mzn Aug 22 '16 at 10:25
  • @Mzn You're right - I implemented the important part from the post :-) – LMW-HH Aug 22 '16 at 14:46
  • I just noticed that it's 5 years old! Good job expanding your answer; it looks so much better now! – Mzn Aug 22 '16 at 17:02

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.