<#
.SYNOPSIS
Can be used to loop through all lists available across the site collection, from all sites and their item counts which are added in last 30 days within SharePoint Online tenant. We are writting list title, relative url of list and last 30 days item count in .csv file
.EXAMPLE
PS C:\> .\Get-Last30DaysItemCountFromAllListInSiteCollection.ps1 -SiteCollectionUrl https://MySiteCollection.sharepoint.com
.EXAMPLE
PS C:\> $creds = Get-Credential
PS C:\> .\Get-Last30DaysItemCountFromAllListInSiteCollection.ps1 -SiteCollectionUrl https://contoso-admin.sharepoint.com -Credentials $creds
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, HelpMessage="Enter the URL of the target site collection , e.g. 'https://contoso.sharepoint.com'")]
[String]
$SiteCollectionUrl,
[Parameter(Mandatory = $false, HelpMessage="Optional administration credentials to site collection.")]
[PSCredential]
$Credentials
)
#Looping through all the webs and all their sub webs. Calling this function recursively.
function Loop-AllWebs{
Param(
[Microsoft.SharePoint.Client.ClientContext]$Context,
[Microsoft.SharePoint.Client.Web]$RootWeb
)
#Getting all webs
$Webs = $RootWeb.Webs
$Context.Load($Webs)
$Context.ExecuteQuery()
ForEach ($sWeb in $Webs)
{
Get-AllListsDetails -Context $Context -web $sWeb
#Looping through all sub webs
Loop-AllWebs -RootWeb $sWeb -Context $Context
}
}
function Get-AllListsDetails{
Param(
[Microsoft.SharePoint.Client.ClientContext]$Context,
[Microsoft.SharePoint.Client.Web]$web
)
$lists=$web.Lists
$Context.Load($lists)
$Context.ExecuteQuery()
foreach($list in $lists)
{
#Getting view for getting the list URL
$view = $list.DefaultView
$spQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
#CAML Query Using a DateTime Value and and Offset of Today
$query = '<View><Query><Where><Geq><FieldRef Name="Created" /><Value Type="DateTime"><Today OffsetDays="-30" /></Value></Geq></Where></Query></View>'
$spQuery.ViewXml = $query
$spListItemCol = $list.GetItems($spQuery)
$Context.Load($list)
$Context.Load($view)
$Context.Load($spListItemCol)
$Context.ExecuteQuery()
$line = $list.Title + "," + $view.ServerRelativeUrl + "," + $spListItemCol.Count
#Writting to CSV file
Add-Content Result.csv $line #Give the full path where you want to store the result. Currently it will be generated at the location of this script
}
}
# Get reference to SPO 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"
Write-Host -ForegroundColor White "---------------------------------------------------------------------------"
Write-Host -ForegroundColor White "Get All Lists across Site Collection and last 30 days Items counts from each and every lists "
Write-Host -ForegroundColor White "---------------------------------------------------------------------------"
Write-Host -ForegroundColor Yellow "Site Collection URL: $SiteCollectionUrl"
Write-Host ""
Write-Host ""
# Get credentials, if they were not provided
if($Credentials -eq $null)
{
$Credentials = Get-Credential -Message "Enter Admin Credentials"
}
[Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollectionUrl)
$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName, $Credentials.Password)
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
Loop-AllWebs -RootWeb $web -Context $clientContextThis code snippet is for powershell script for getting the items count which are added in last 30 days.
