SharePoint Online: PowerShell to List All Document Libraries

Requirement:  PowerShell to List All Document Libraries in SharePoint Online.


The "Site Contents" page in SharePoint displays all lists and libraries in a site.
SharePoint Online PowerShell to Get All Document Libraries

SharePoint Online PowerShell to List Document Libraries
Here is the PowerShell to get all document libraries from a SharePoint Online site.
#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"
   
#Config Parameters
$SiteURL="https://crescenttech.sharepoint.com"
  
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
   
#Get Lists from the web
$Ctx.Load($Ctx.Web.Lists)
$Ctx.executeQuery()
 
#Filter Document Libraries from Lists collection
$Lists = $Ctx.Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

#Loop through each document library and Get the Title
Foreach ($List in $Lists)
{
    Write-host $List.Title
}
This gets all document libraries in SharePoint Online using PowerShell.

SharePoint Online PowerShell to List All Document Libraries in a Site Collection
Lets wrap the script into reusable function and add some error handling to get document libraries from a SharePoint Online site collection.
#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 all documents Libraries in a SharePoint Online Site Collection
Function Get-SPODocumentLibrary($SiteURL)
{
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
   
        #Get the web and Its subsites from given URL
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Lists)
        $Ctx.Load($web.Webs)
        $Ctx.executeQuery()
 
        Write-host -f Yellow "Processing Site: $SiteURL"
 
        #Filter Document Libraries from Lists collection
        $Lists = $Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

        #Loop through each document library and Get the Title
        Foreach ($List in $Lists)
        {
            Write-host $List.Title
        }
  
        #Iterate through each subsite of the current web and call the function recursively
        ForEach ($Subweb in $Web.Webs)
        {
            #Call the function recursively to process all subsites
            Get-SPODocumentLibrary($Subweb.url)
        }
    }
    Catch {
        write-host -f Red "Error Getting Document Libraries!" $_.Exception.Message
    }
}
  
#Config Parameters
$SiteCollURL="https://crescenttech.sharepoint.com"
  
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Call the function to get all document libraries in a site collection
Get-SPODocumentLibrary $SiteCollURL
SharePoint Online: PowerShell to List All Document Libraries SharePoint Online: PowerShell to List All Document Libraries Reviewed by Salaudeen Rajack on March 19, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.