1

I have a document library and I want to loop through all the folders I have in it. I have this piece of code

           $Context.Load($List.RootFolder.Folders)
           $context.ExecuteQuery()
           ##write-host 'RootFolder.Folders.Count:', $list.RootFolder.Folders.Count
           foreach ($foldername in $list.RootFolder.Folders)
           {

               write-host $foldername.name 

           }

Which works for the folders in the root but not with subfolders. Also it returns a folder that is non existent called Forms, is this a oob folder that is hidden? How do I loop through subfolders as well?

2

You will have to write a recursive method for this. You can exclude Forms folder as all the List & Libraries in SharePoint has a default Forms folder where all the view/Add/Edit forms will be stored.

function GetChildFolders($RootFolder) 
{
        $Context.Load($RootFolder)
        $Context.Load($RootFolder.Folders)
        $Context.ExecuteQuery()
        write-host $RootFolder.Name 
        foreach($folder in $RootFolder.Folders)
        {
          if($folder.Name -ne "Forms")
          {
           Recurse $folder  
          }
        }
}

Call this method and pass $List.RootFolder

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.