SharePoint Online: Get All Folders from List using PowerShell

Requirement: Get All Folders from a SharePoint Online list using PowerShell
PowerShell to Get All Folders in a SharePoint Online List

PowerShell to Get All Folders in a SharePoint Online List
Here is the CAML based approach to get all folders and sub-folders recursively from a list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"

#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the List
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    
    #Define CAML Query to get all folders from list recursively
    $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $CAMLQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>1</Value></Eq></Where></Query></View>"

    #Get All Folders from the List
    $Folders = $List.GetItems($CAMLQuery)
    $Ctx.Load($Folders)
    $Ctx.ExecuteQuery()

    #Iterate through Each Folder
    ForEach($Folder in $Folders)
    {
        #Get the Folder's Server Relative URL
        Write-host $Folder.FieldValues['FileRef']
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

SharePoint Online: Get Sub-Folders from a SharePoint Online List using PowerShell
Let's get all top-level folders from a list or library in SharePoint Online.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"

#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the List
    $List = $Ctx.web.Lists.GetByTitle($ListName)

    #Get Sub-Folders of the List
    $SubFolders = $List.RootFolder.Folders
    $Ctx.Load($SubFolders)
    $Ctx.ExecuteQuery()
    
    #Iterate through Each SubFolder
    ForEach($Folder in $SubFolders)
    {
        #Get the Folder's Server Relative URL
        Write-host $Folder.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Similarly, If you want to get all sub-folder's of a specific folder, use:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$FolderRelativeURL = "/sites/marketing/Shared Documents/2018"

#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get Sub-Folders of a Folder
    $SubFolders = $Ctx.web.GetFolderByServerRelativeUrl($FolderRelativeURL).Folders
    $Ctx.Load($SubFolders)
    $Ctx.ExecuteQuery()
    
    #Iterate through Each SubFolder
    ForEach($Folder in $SubFolders)
    {
        #Get the Folder's Server Relative URL
        Write-host $Folder.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

SharePoint Online: Get All Folders Recursively from a SharePoint Online List using PowerShell
Here is PowerShell script to recursively get all folders from a SharePoint Online List or library.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Get Sub-folders of a Folder in SharePoint Online
Function Get-SPOFolders([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Try {         
        Write-host $Folder.ServerRelativeUrl
 
        #Process all Sub Folders
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()
 
        #Iterate through each sub-folder of the folder
        Foreach ($Folder in $Folder.Folders)
        {
            #Call the function recursively
            Get-SPOFolders $Folder
        }
    }
    Catch {
        write-host -f Red "Error Getting Folder!" $_.Exception.Message
    }
}

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"

#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Root Folder of a List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $RootFolder = $List.RootFolder
    $Ctx.Load($RootFolder)
    $Ctx.ExecuteQuery()
    
    #Call the Function to get all folders recursively from the list
    Get-SPOFolders $RootFolder
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online: Get All Folders from List using PowerShell SharePoint Online: Get All Folders from List using PowerShell Reviewed by Salaudeen Rajack on September 19, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.