Tuesday, April 10, 2012

Disable Alerts from a SharePoint List or Library

End-user came up with a requirement: Disable alerts from a particular SharePoint list, where 100's of people subscribed already.

Its apparent that we can disable alerts for web application either from Central Administration or executing the stsadm:
stsadm -o setproperty -url http://server_name -pn alerts-enabled -pv false

But here the requirement is to disable alert for a particular library ONLY for some time, and enable it back once the user done with his editing.

I remember SPAlert class has a Status property. why don't to try setting it OFF? Sounds good! Lets jump to coding, make a console application and give it to the SharePoint Administrator to disable alerts on a SharePoint 2010/2007 list.

Code to disable alerts for SharePoint list:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace TurnAlertsOnOff
{
    class Program
    {
        static void Main(string[] args)
        {
            string site;
            string web;
            string list;
            string option;
            SPSite tmpSite=null;

            try
            {
                //Get the site collection , Sub-site, Lists
                if (args.Length != 3)
                {
                    //Site collection
                    Console.WriteLine("Enter the Site collection URL (E.g.)'http://intranet.company.com' : \n ----------------------------------- \n");
                    site = Console.ReadLine();

                    //Sub-Site
                    Console.WriteLine("\n Enter the Sub-Site URL (E.g.) '/Sales/us' (  '/' for root web): \n -------------------------------------- ");
                    web = Console.ReadLine();

                    //List
                    Console.WriteLine("\n Enter the List Name(Title): (E.g) 'Announcements' : \n -------------------------------------- ");
                    list = Console.ReadLine();

                    //Option ON or OFF
                    Console.WriteLine("\n Enter the Option ON or OFF: \n ----------------------------------- \n ");
                    option = Console.ReadLine();

                }
                else
                {
                    site = args[0];
                    web = args[1];
                    list = args[2];
                    option = args[3];
                }

                tmpSite = new SPSite(site);
                SPWeb tmpWeb = tmpSite.OpenWeb(web);
                SPList tmpList = tmpWeb.Lists[list];

                long alertCounter = 0;
                foreach (SPAlert tmpAlert in tmpWeb.Alerts)
                {
                    option = option.ToUpper();

                    if (option == "ON") //Turn it ON
                    {
                        if ((tmpAlert.List.Title == tmpList.Title) & (tmpAlert.Status == SPAlertStatus.Off))
                        {
                            tmpAlert.Status = SPAlertStatus.On;
                            Console.WriteLine("Turning ON Alert: {0} in List: {1}", tmpAlert.Title, tmpList.Title);
                            tmpAlert.Update();
                            alertCounter++;
                        }
                    }
                    else if (option == "OFF") //Turn it ON
                    {
                        if ((tmpAlert.List.Title == tmpList.Title) & (tmpAlert.Status == SPAlertStatus.On))
                        {
                            tmpAlert.Status = SPAlertStatus.Off;
                            Console.WriteLine("Turning OFF Alert: {0} in List :{1}", tmpAlert.Title, tmpList.Title);
                            tmpAlert.Update();
                            alertCounter++;
                        }

                    }
                }

                //Just to pause
                Console.WriteLine("Done, Processed {0} Alerts! Press a key to Exit.", alertCounter);
                Console.ReadLine();

            }

            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                System.Diagnostics.EventLog.WriteEntry("Disable/Enable Alerts", ex.Message);
                Console.ReadLine();
            }

            finally
            {
                //Dispose of the Root Site Object
                tmpSite.Dispose();
            }
        }
    }
}

Compile and execute the code from server will temporarily remove alert from SharePoint list. You can disable alerts in SharePoint list using PowerShell as well. Refer my old posts: Create - Edit - Find - Delete SharePoint Alerts using PowerShell

Tail: 
You can set the immediate alerts job interval to 1 minute by running:  
stsadm –o setproperty –pn job-immediate-alerts –url https://Intranet.company.com –pv "every 1 minutes"

Related post: Disable alerts in SharePoint using PowerShell



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:

4 comments :

  1. Hi Salaudeen,
    i need to programmatically turn off and on alerts for a list and do batch operation on list data, i followed your blog post and created a alertfunction and implemented like this

    alertfunction(off)
    batchfunction();
    alertfunction(on)

    but still iam getting email alerts,any help will be appreciated

    ReplyDelete
    Replies
    1. This code disables ONLY existing alerts in a specific list or library! It doesn't (of course, can't!) disable any new alerts you are creating after executing this code 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

      Delete
  2. Niranjan,
    Make sure you are updating the Alert Objects after setting its properties!

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...