SharePoint Online: Delete Alerts using PowerShell

Requirement: Delete Alerts in SharePoint Online

How to Delete Alerts in SharePoint Online?
To delete an alert from SharePoint Online, follow these steps:
  • Login to SharePoint Online >> Navigate to Site Settings page.
  • Under "Site Administration" section, Click on "User Alerts" link
  • This page lists alerts of all users in the particular site (web). Pick the user from the drop down and click on "Update" button to retrieve all alerts of the user.
  • Once alerts are listed, can select either single alert or multiple alerts and then click on "Delelte Selected Alerts" link.
    sharepoint online delete alerts

SharePoint Online: Delete All Alerts of a User using PowerShell
We can use PowerShell to delete all alerts of a particular user in 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://crescent.sharepoint.com/projects"
$UserID="Salaudeen@crescent.com"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
   
    #Get All Alerts of the User
    $User = $Ctx.Web.EnsureUser($UserID)
    $UserAlerts = $User.Alerts
    $Ctx.Load($User)
    $Ctx.Load($UserAlerts)
    $Ctx.ExecuteQuery()

    #Loop through each alert
    Write-host "Total Alerts Found:"$User.Alerts.Count
    ForEach($Alert in $UserAlerts)
    {
        #Delete the Alert        
        Write-host -f Green "Alert '$($Alert.Title)' is Deleted on $($Alert.AlertType)"
        $User.Alerts.DeleteAlert($Alert.ID)
        $Ctx.ExecuteQuery()
    }
}
Catch {
    write-host -f Red "Error Deleting User Alerts!" $_.Exception.Message
}
Let's wrap the above script into re-usable function and remove all alerts of a user 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"
    
#Remove User Alerts in a Site Collection
Function Remove-SPOUserAlerts($SiteURL, $UserID)
{
    Try {
        Write-host -f Yellow "Searching User Alerts in Site:"$SiteURL
        
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
    
        #Get the Web and its Subwebs
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()

        #Get All Alerts of the User in the site
        $User = $Web.EnsureUser($UserID)
        $UserAlerts = $User.Alerts
        $Ctx.Load($User)
        $Ctx.Load($UserAlerts)
        $Ctx.ExecuteQuery()
 
        #Loop through each alert
        Write-host "Total Number of User Alerts:"$User.Alerts.Count
        ForEach($Alert in $UserAlerts)
        {
            #Delete the Alert        
            $User.Alerts.DeleteAlert($Alert.ID)
            $Ctx.ExecuteQuery()
            Write-host -f Green "Alert '$($Alert.Title)' is Deleted on $($Alert.AlertType)"
        }

        #Process Subsites
        ForEach($Web in $Web.Webs)
        {
            #Call the function recursively
            Remove-SPOUserAlerts -SiteURL $Web.Url -UserID $UserID
        }
    }
    Catch {
        write-host -f Red "Error Deleting User Alerts!" $_.Exception.Message
    }
}
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$UserID="Salaudeen@crescent.com"

#Setup Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Call the function to remove alerts in site collection
Remove-SPOUserAlerts -SiteURL $SiteURL -UserID $UserID

Delete All User Alerts from the Web:
This PowerShell script deletes all alerts of all users in a SharePoint Online web.
#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://crescent.sharepoint.com/projects"

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

    #Loop through each alert
    Write-host "Total Alerts Found:"$WebAlerts.Count

    ForEach($Alert in $WebAlerts)
    {
        #Get the User of the Alert
        $Ctx.Load($Alert.User)
        $Ctx.ExecuteQuery()
        $AlertUser = $Alert.user.Title

        #Delete the Alert        
        $web.Alerts.DeleteAlert($Alert.ID)
        $Ctx.ExecuteQuery()

        Write-host -f Green "Alert '$($Alert.Title)' of '$($AlertUser)' is Deleted on $($Alert.AlertType)"
    }
}
Catch {
    write-host -f Red "Error Deleting Alerts!" $_.Exception.Message
}
If you want to disable alerts, instead of deleting, use: SharePoint Online Disable Alerts using PowerShell
SharePoint Online: Delete Alerts using PowerShell SharePoint Online: Delete Alerts using PowerShell Reviewed by Salaudeen Rajack on October 30, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.