kwizcom banner advertisement

Disable Alerts on SharePoint 2013 List using PowerShell

Requirement: Temporarily disable Alert Emails on a SharePoint list.
how to disable alerts in sharepoint using powershell

Solution:
SharePoint Alert objects have "Status" property which can be turned ON or OFF. As there is no UI to disable or enable alerts in SharePoint directly from the browser, we can do it programmatically with PowerShell. Here is my PowerShell script to disable alerts on SharePoint 2010/2013 list or library.
Important: This script disables ONLY existing alerts in a specific list or library! It doesn't (of course, can't!) disable any new alerts you create on the particular list! You may have to temporarily turn-off alerts on web application level by changing the flag "AlertsEnabled" to "false" as in How to Disable Alerts in SharePoint

PowerShell script to Disable alerts on SharePoint list
Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

#Function to Disable All Active Alerts on a Given List
Function Disable-ListAlerts($WebURL, $ListName)
{
    #Get the Web and List objects
    $Web = Get-SPWeb $WebURL
    $List = $web.Lists.TryGetList($ListName)

    #Get All Alerts created in the list - Which are Active
    $ListAlerts = $Web.Alerts | Where-Object {($_.List.Title -eq $List.Title) -and ($_.Status -eq "ON")}

    Write-host "Total Number of Active Alerts Found in the list: $($ListAlerts.Count)" 
    
    #Iterate through each alert and turn it OFF
    foreach($Alert in $ListAlerts)
        {
            $Alert.Status="OFF"
            $Alert.Update()
            write-host "Disabled the Alert' $($Alert.Title)' Created for User '$($Alert.User.Name)'"
        }
         
    #Dispose web object
    $Web.Dispose()
}

#Function to Enable all Disable Alerts on a Given List
Function Enable-ListAlerts($WebURL, $ListName)
{
    #Get the Web and List objects
    $Web = Get-SPWeb $WebURL
    $List = $web.Lists.TryGetList($ListName)

    #Get All Alerts created in the list - Which are in Disabled State
    $ListAlerts = $Web.Alerts | Where-Object { ($_.List.Title -eq $List.Title) -and ($_.Status -eq "OFF")}

    Write-host "Total Number of Disabled Alerts Found in the list: $($ListAlerts.Count)" 
    
    #Iterate through each alert and turn it OFF
    foreach($Alert in $ListAlerts)
        {
            $Alert.Status="ON"
            $Alert.Update()
            write-host "Enabled the Alert' $($Alert.Title)' Created for User '$($Alert.User.Name)'"
        }
         
    #Dispose web object
    $Web.Dispose()
}

#Variables
$WebURL = "http://operations.crescent.com/"
$ListName="Proposal Documents"

#Call the function Appropriately to Disable or Enable Alerts 
Disable-ListAlerts $WebURL $ListName

#To Enable it back
#Enable-ListAlerts $WebURL $ListName
Please note, You have to wait for Two minutes before enabling the alerts again (Because the Immediate alert timer job looks for items modified in past two minutes). Otherwise, You'll get Emails from disabled alerts! So: Disable Alerts - Do the changes to data - Wait for two minutes - Enable alerts again.
Related Posts:
Disable Alerts on SharePoint 2013 List using PowerShell Disable Alerts on SharePoint 2013 List using PowerShell Reviewed by Salaudeen Rajack on July 15, 2015 Rating: 5

3 comments:

  1. Great Blog with excellent PowerShell scripts. Bookmarked!

    ReplyDelete
  2. Excellent start, thank you. But this approach will enable alerts that were already disabled before the first function runs. I'll have to try to store those and then re-enable only the ones that this function disabled. Have to create an array with global scope maybe.

    ReplyDelete
    Replies
    1. I thought that too, so I created an array that I returned from the the delete function and fed back into the Enable function. The problem I'm having is that despite disabling the alerts, deleting the items, then re-enabling the alerts it doesn't seem to take effect and alerts are still sent :( I guess because we're doing everything inside of the same 5 minute window between the timer job firing.

      We need something that will prevent the event from being written to the event table in the DB...

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.