Saturday, March 31, 2012

SharePoint Alerts Report for a List or Library

End-User's Requirement: I want to see all people who subscribed for my Project site's "Lessons Learn" List.

Solution: Wrote object model code to retrieve all alerts created on a specific list.

C# Object Model Code to Get All Alerts Created on a SharePoint List or Library & Generate Report:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace SharePoint.AdminReports
    class GetAlertsForList
        static void Main(string[] args)
            string site;
            string web;
            string list;
            long alertCounter=0;
            StreamWriter SW;

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

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

                    Console.WriteLine("\nEnter the List/Library Name(Title): (E.g) Announcements: \n");
                    list = Console.ReadLine();
                    site = args[0];
                    web = args[1];
                    list = args[2];

                //objects for the CSV file generation
                SW = File.AppendText("c:\\AlertsOnList.csv");
                //Write the CSV Header
                SW.WriteLine("List URL \t Alert Name \t User Name \t Frequency \t Alert Event");

                using (SPSite tmpSite = new SPSite(site))
                    using (SPWeb tmpWeb = tmpSite.OpenWeb(web))
                        SPList tmpList = tmpWeb.Lists[list];
                            foreach (SPAlert tmpAlert in tmpWeb.Alerts)
                                if (tmpAlert.List.Title == tmpList.Title)
                                    SW.WriteLine(tmpWeb.Url + "/" + tmpList.RootFolder.Url + "\t" + tmpAlert.Title + "\t" + tmpAlert.User.Name + "\t" + tmpAlert.AlertFrequency + "\t" + tmpAlert.EventType.ToString());
                //Close the File object

                //Display the message to the user
                Console.WriteLine("Found {0} Alerts! Report has been generated! Press a key to Exit.", alertCounter);

            catch (Exception ex)
                Console.WriteLine("Error: " + ex.Message);
                System.Diagnostics.EventLog.WriteEntry("Get Alerts for List:", ex.Message);

Output? CSV file like the below screen:

sharepoint alerts report

You can Retrieve All alerts created on a SharePoint list or Library using PowerShell also. Refer my post : Create - Edit - Find - Delete SharePoint Alerts using PowerShell

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

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...