kwizcom banner advertisement

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. 
#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
}

Delete All Users 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.