Export SharePoint User Alerts using C#

Hello friends, in this blog article we will see how we can export all the user alerts from SharePoint Online Site. It will export the list of user alerts from defined site collection in CSV format. The exported data will contain all the details about user and his alerts like on which lists he subscribed for the alerts, frequency of the same.

We know that we can see a list of user alerts from site settings > User alerts. But it has some limitations like a number of users. It won’t provide the list of all users once it crossed the specified limit. So to export all user alerts, we need to write either PowerShell or some console application.

Here are steps to get all alerts from SharePoint online site. You can tweak the code according to your need.

1) Open Visual Studio 2013
2) Create Window console project
3) Open Tools → NuGet Package Manager → Package Manager Console
4) Run following command in the console

Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.7317.1200

5) Add following code in the main class

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace Alerts {
    class Program {
        static void Main(string[] args) {

            string targetSiteURL = @ "https://myclassbook.sharepoint.com/sites/test123";
            var login = "[email protected]";
            var password = "XXX";
            var securePassword = new SecureString();

            foreach(char c in password) {
                securePassword.AppendChar(c);
            }

            SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
            ClientContext ctx = new ClientContext(targetSiteURL);
            ctx.Credentials = onlineCredentials;
            var users = ctx.Web.SiteUsers;
            ctx.Load(users);
            ctx.ExecuteQuery();
            var csv = new StringBuilder();

            foreach(var user in users) {
                var alerts = user.Alerts;
                ctx.Load(alerts);
                ctx.ExecuteQuery();
                if (alerts.Count > 0) {
                    csv.AppendLine(string.Format("Display alerts for {0}({1})", user.Title, user.LoginName));
                    foreach(var alert in alerts) {
                        csv.AppendLine(string.Format("Frequency:{0}, Delivery Method(s):{1}, AlertName:{2}", alert.AlertFrequency, alert.DeliveryChannels, alert.Title));
                    }
                }
            }
            System.IO.File.WriteAllText("C:\\Alerts\\alerts.csv", csv.ToString());
        }
    }
}

6) Change Url and login details. Resolve references
7) Add “Alerts” folder in C: drive
8) Run code

See also: Manage, view, or delete SharePoint alerts

I hope you liked this article. Please share it with your friends and like our facebook page. If you have any queries, feel free to ask in the comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *