Category: Alerts

SharePoint Online: Using PowerShell to delete alerts in site/sub sites using CSOM

Metalogix Content Matrix is a great tool to migrate from SharePoint on-premises As part of migration to SharePoint Online we had to re-migrate some of the Here is a PowerShell script to delete alerts in SharePoint Online. This PowerShell script will delete all alerts in root web and all sub webs in SharePoint Online site.

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 09/14/2018
#  Description..: SharePoint Online: Delete alerts in site/sub sites using CSOM
#
#################################################################################################### 
CLS
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" -ErrorAction Stop
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" -ErrorAction Stop

#use the $AlertsExportFile to store existing Alers
$AlertsExportFile = "C:\temp\Alerts.csv"
if (Test-Path $AlertsExportFile) {
    Remove-Item $AlertsExportFile -Confirm:$false
}

$UserName = Read-Host "Please enter User Name"
$Password = Read-Host "Enter a Password" -AsSecureString
$SiteUrl = Read-Host "Please enter SharePoint Online Site URL"

$credentials= New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)

$ctx= New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = $credentials

$web = $ctx.Web
$ctx.Load($web)
$ctx.Load($web.Webs)
$siteUsers=$web.SiteUsers
$ctx.Load($siteUsers)
$siteUsers=$web.SiteUsers
$ctx.Load($siteUsers)
$alerts=$web.Alerts
$ctx.Load($alerts)
$ctx.ExecuteQuery()
Write-Host "Processing $($web.Url)"
#List all Alerts for RootWeb site    
if ($alerts.Count -gt 0) {
    $alerts |SELECT @{Label="SiteUrl";Expression={"$($web.Url)"}},AlertFrequency, AlertTime, AlertType, AlwaysNotify, DeliveryChannels, EventType, Filter, ID, UserID, Status | Export-Csv $AlertsExportFile -Append -NoTypeInformation
}
foreach($alert in $alerts) {
	$web.Alerts.DeleteAlert($alert.ID)
	$web.Update()
	$ctx.ExecuteQuery()
}
#enumerate through seub webs
foreach ($webSub in $web.Webs) {
    Write-Host "Processing $($webSub.Url)"
    $alertsSub = $Null
    $ctxSub= New-Object Microsoft.SharePoint.Client.ClientContext($webSub.Url)
    $ctxSub.Credentials = $credentials
    $webSub = $ctxSub.Web
    $ctxSub.Load($webSub)
    $alertsSub=$webSub.Alerts
    $ctxSub.Load($webSub)
    $ctxSub.Load($alertsSub)
    $ctxSub.ExecuteQuery()
    #List all Alerts for each sub web
    if ($alertsSub.Count -gt 0) {
        $alertsSub |SELECT @{Label="SiteUrl";Expression={"$($webSub.Url)"}},AlertFrequency, AlertTime, AlertType, AlwaysNotify, DeliveryChannels, EventType, Filter, ID, UserID, Status | Export-Csv $AlertsExportFile -Append -NoTypeInformation
    }
    foreach($alert in $alertsSub){        
	    $webSub.Alerts.DeleteAlert($alert.ID)
	    $webSub.Update()
	    $ctxSub.ExecuteQuery()
    }
}

###################################################################################################

That was another page in the Chronicles of SharePoint Bits, happy scripting!