Wednesday, July 15, 2015

Disable Alerts on SharePoint 2013 List using PowerShell

Requirement: Temporarily disable Alert Emails on a SharePoint list.

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

Related Posts:



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

2 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

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...