kwizcom banner advertisement

SharePoint Online: Disable Alerts using PowerShell

Requirement: Disable Alerts in SharePoint Online!

How to disable alerts in SharePoint Online?
Well, there is no way to disable alerts from SharePoint Online user interface. (You can delete alerts however!). We can use PowerShell to disable alerts in a SharePoint site.
sharepoint online disable alerts


PowerShell to Disable All Alerts of a User:
Lets disable all alerts of a particular user in a given web - SharePoint Alerts are scoped at web object!
#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"
$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)
    $Ctx.Load($User)
    $Ctx.Load($User.Alerts)
    $Ctx.ExecuteQuery()

    #Loop through each alert
    Write-host "Total Alerts Found:"$User.Alerts.Count
    ForEach($Alert in $User.Alerts)
    {
        #Check the Alert Status
        If($Alert.Status -ne "Off")
        {
            #Disable the Alert
            $Alert.Status="Off"
            $Alert.UpdateAlert()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Alert '$($Alert.Title)' is disabled on $($Alert.AlertType)"
        }
        Else
        {
            Write-host -f Yellow "Alert '$($Alert.Title)' is already disabled on $($Alert.AlertType)!"
        }
    }
}
Catch {
    write-host -f Red "Error Disabling User Alerts!" $_.Exception.Message
}
To Turn ON the Alert back, use:
 ForEach($Alert in $User.Alerts)
    {
        #Check the Alert Status
        If($Alert.Status -eq "Off")
        {
            #Enable the Alert
            $Alert.Status="On"
            $Alert.UpdateAlert()
            $Ctx.ExecuteQuery()
            Write-host -f Green "Alert '$($Alert.Title)' is Enabled on $($Alert.AlertType)"
        }
        Else
        {
            Write-host -f Yellow "Alert '$($Alert.Title)' is already Enabled on $($Alert.AlertType)!"
        }
    }

Turn-Off All Alerts of a Specific List in SharePoint Online:
Here is the PowerShell to To disable alerts created in a specific list or document library 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/Sites/Marketing"
$ListName="Documents"

#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 from the Web
    $Web = $Ctx.Web
    $WebAlerts = $Web.Alerts
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($Web)
    $Ctx.Load($WebAlerts)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #Loop through each alert of the web and get List alerts
    ForEach($Alert in $webAlerts)
    {
        #Get the List and User of the Alert
        $Ctx.Load($Alert.User)
        $Ctx.Load($Alert.List)
        $Ctx.ExecuteQuery()
        $AlertUser = $Alert.user.Title
        
        #Filter the specific List Alerts from web alerts
        If($Alert.List.ID -eq $List.ID)
        {
            #Check the Alert Status
            If ($Alert.Status -ne "Off")
            {
                #Disable the Alert
                $Alert.Status="Off"
                $Alert.UpdateAlert()
                $Ctx.ExecuteQuery()
             
                Write-host -f Green "Alert '$($Alert.Title)' of '$($AlertUser)' is disabled on $($List.Title)!"
            }
            Else
            {
                Write-host -f Yellow "Alert '$($Alert.Title)' of '$($AlertUser)' is already disabled on $($List.Title)!"
            }
        }
    }
}
Catch {
    write-host -f Red "Error Disabling Alerts!" $_.Exception.Message
}

PowerShell to Disable All Alerts from a Web: 
To disable alerts of all users in a specific web, use this PowerShell script.
#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://abraaj.sharepoint.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 Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.Alerts)
    $Ctx.ExecuteQuery()

    #Loop through each alert
    Write-host "Total Alerts Found:"$Web.Alerts.Count
    ForEach($Alert in $Web.Alerts)
    {
        #Get the User
        $Ctx.Load($Alert.User)
        $Ctx.ExecuteQuery()
        $AlertUser = $Alert.user.Title            

        #Check the Alert Status
        If($Alert.Status -ne "Off")
        {
            #Disable the Alert
            $Alert.Status="Off"
            $Alert.UpdateAlert()
            $Ctx.ExecuteQuery()
            
            Write-host -f Green "Alert '$($Alert.Title)' of '$($AlertUser)' is disabled on $($Alert.AlertType)!"
        }
        Else
        {
            Write-host -f Yellow "Alert '$($Alert.Title)' of '$($AlertUser)' is already disabled on $($Alert.AlertType)!"
        }
    }
}
Catch {
    write-host -f Red "Error Disabling Alerts!" $_.Exception.Message
}
If you want to delete alerts, you can use: SharePoint Online Delete Alerts using PowerShell
SharePoint Online: Disable Alerts using PowerShell SharePoint Online: Disable Alerts using PowerShell Reviewed by Salaudeen Rajack on November 30, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.