Getting User Alerts from Site CSOM

Getting User Alerts from Site CSOM

Facing an issue to find all user alerts for all Lists, this will work only in SharePoint Online

public static void GetAlerts(out List<string> p)
{
	p  = new List<string>();
	var oSite = Ctx.Site;
	Ctx.Load(oSite);
	var oWeb = Ctx.Web;
	Ctx.Load(oWeb);
	Ctx.Load(oWeb.SiteUsers);
	Ctx.Load(oWeb.Alerts);
	Ctx.Load(oWeb.SiteGroups);

	var siteUsers = Ctx.Web.SiteUsers;
	Ctx.Load(siteUsers);
	var alerts = oWeb.Alerts;
	Ctx.Load(alerts);

	Ctx.ExecuteQuery();
	StreamWriter writetext = new StreamWriter("C:\\Temp\\ALM.txt");
	foreach (var alert in alerts)
	{
		foreach (var siteUser in siteUsers)
		{
			
			if (siteUser.Id == alert.UserId)
			{
				
					writetext.WriteLine(alert.Title + ";" + siteUser.LoginName + ";" + siteUser.Email);
			}
		}
	}
	writetext.Dispose();
}

Leave a Reply

Your email address will not be published.

RSS
LinkedIn