0

I need a simple script that returns a count of Custom Lists and Document Libraries in a Site Collection. I don't need the items count or the files size totals, just the number of libraries.

Powershell or C# will work.

0
0

Try this script:

$username = "user@tenantname.onmicrosoft.com"
$password = "xxx"
$SiteCollectionUrl = "https://tenantname.sharepoint.com/Sites/sitename"
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 GetClientContext($SiteCollectionUrl, $username, $password) {
     $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
     $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollectionUrl) 
     $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
     $context.Credentials = $credentials
     return $context
}




# function to loop through subsites
function getlistcount ($siteurl){
    $clientContext = GetClientContext $siteurl $username $password
    $rootWeb = $clientContext.Web
    $childWebs = $rootWeb.Webs
    $clientContext.Load($rootWeb)
    $clientContext.Load($childWebs)
    $clientContext.ExecuteQuery()
    #do something on top level site
    write-host $rootWeb.url -ForegroundColor Yellow
     $count=0
    foreach ($childWeb in $childWebs)
    {

          write-host $childWeb.url -ForegroundColor Yellow
          $clientContext.Load($childWeb.Lists)
          $clientContext.ExecuteQuery()
         $count+=$childWeb.Lists.Count

    }
     write-host ($count)
}


getlistcount $SiteCollectionUrl
1
  • this only works for subsites and doesn't give the top level count – Michael Colbs Dec 20 '17 at 15:35

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.