public override void Execute(Guid targetInstanceId) { //Get the webapplication on which it is activated //Get all the site collections in web application foreach (SPSite site in base.WebApplication.Sites) { //Get all the root level sites in the site collections if (site != null) { //check do we have Alert pro active for that site collection by checking hidden list if possible feature if (Utilities.IsAdvancedAlertFeatureEnabledForsite(site)) { //Sync the alerts for the site collection AlertsCache.SynchroniseAlerts(site.Url, this.Title); //Get all the alerts for the site collection from buffer if not exist add to buffer Dictionary<int, Alert> siteAlerts = AlertsCache.GetAlertsForSiteCollection(site.Url); //Group alerts by web so that no need to create web object again and again Dictionary<string, List<Alert>> siteAlertByWeb = TimerJobHelper.GroupAlertsByWeb(siteAlerts); // get the current time DateTime dtNow = DateTime.Now; AlertManager alertManager = null; try { // if we get any alerts then validate and prepare for sending email. foreach (string webId in siteAlertByWeb.Keys) { try { //create web object using (SPWeb web = site.OpenWeb(new Guid(webId))) { //iterate all the alerts for this web foreach (Alert alert in siteAlertByWeb[webId]) { DateTime dtWebTime = web.RegionalSettings.TimeZone.UTCToLocalTime(dtNow.ToUniversalTime()); //1. Handling Timer based alerts if (alert.AlertType.Contains(AlertEventType.DateColumn)) { // Calling ExecuteTimerAlert SPList list = null; try { list = web.Lists[new Guid(alert.ListId)]; } catch { continue; } if (isValidTimerAlert(alert, web,dtWebTime)) { this.ExecuteTimerAlert(web, list, alert); } } if (alert.SendType == SendType.Daily) { if (alert.DailyBusinessDays.Contains((WeekDays)DateTime.UtcNow.DayOfWeek)) { if (alert.SendHour.Equals(DateTime.UtcNow.Hour)) { if (alertManager == null) { alertManager = new AlertManager(site.Url); } alertManager.ExecuteDelayedMessages(alert); } } } else if (alert.SendType == SendType.Weekly) { if (alert.SendDay.Equals((WeekDays)DateTime.UtcNow.Day)) { if (alert.SendHour.Equals(DateTime.UtcNow.Hour)) { if (alertManager == null) { alertManager = new AlertManager(site.Url); } alertManager.ExecuteDelayedMessages(alert); } } } //2. Handling Delayed alerts for daily bu specific time and send as single message //if (alert.AlertType != SendType.Immediate) //if (alert.SendType != SendType.ImmediateAlways) //{ // //if (((info2.SendHour == time2.Hour) && (time2.Minute < 30)) && (((info2.Timing == SendTiming.Daily) && info2.DailyBusinessDays.Contains(time2.DayOfWeek)) || (info2.SendWeekday == time2.DayOfWeek))) // // { // //if ((alert.SendHour == dtWebTime.Hour) && (((alert.SendType == SendType.Daily) && alert.DailyBusinessDays.Contains(dtWebTime.DayOfWeek)) || (alert.SendDay == dtWebTime.DayOfWeek))) // if ((alert.SendHour == dtWebTime.Hour) && (((alert.SendType == SendType.Daily) && Utilities.ContainsDay(alert.DailyBusinessDays, Convert.ToInt32(dtWebTime.DayOfWeek))) || (alert.SendDay == Convert.ToInt32(dtWebTime.DayOfWeek)))) // { // if (alertManager == null) // { // alertManager = new AlertManager(site.Url); // } // alertManager.ExecuteDelayedMessages(alert); // } //} ////3. Handling Delayed alerts based on weekdays and all the stuff //else if (!alert.ImmidiateAlways) //{ // //Based on week days // //if ((alert.ImmediateBusinessDays.Contains(web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow.DayOfWeek)) && (alert.BusinessStartHour <= web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow).Hour)) && (alert.BusinessendtHour > web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow).Hour)) // if ((Utilities.ContainsDay(alert.ImmediateBusinessDays, Convert.ToInt32(web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow).DayOfWeek))) && (alert.BusinessStartHour <= web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow).Hour) && (alert.BusinessendtHour > web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow).Hour)) // { // if (alertManager == null) // { // alertManager = new AlertManager(site.Url); // } // alertManager.ExecuteDelayedMessages(alert); // } //} } } } catch { //Error occured while creating web application etc } } } catch { //Error occured while processing } } else { //Feature is not activated for the site collection } } } }
/// <summary> /// update the buffered alerts to latest if any modifications are done /// </summary> /// <param name="siteUrl"></param> internal static void SynchroniseAlerts(string siteUrl,string jobName) { try { //Create alert manager object AlertManager aManager = new AlertManager(siteUrl); if (!bufferedAlerts.ContainsKey(siteUrl)) { //Directly add all Alerts bufferedAlerts.Add(siteUrl, aManager.GetAllAlerts()); } else { //update old alerts DateTime lastExecutionDate = GetLastExecutionTimeForTimerJob(jobName); Dictionary<int, Alert> modifiedAlerts = aManager.GetAlertsChangesSince(lastExecutionDate); Dictionary<int, Alert> AllAlerts = bufferedAlerts[siteUrl]; foreach (int id in modifiedAlerts.Keys) { if (AllAlerts.ContainsKey(id)) { if (modifiedAlerts[id] != null) { //Alert is updated AllAlerts[id] = modifiedAlerts[id]; } else { //Remove alert it has been deleted from the alert settings AllAlerts.Remove(id); } continue; } if (modifiedAlerts[id] != null) { // New alert is added AllAlerts.Add(id, modifiedAlerts[id]); } } } } catch { } }
private void CreateDelayedAlert(Alert alert, AlertEventType eventType, SPItemEventProperties properties, AlertManager alertManager) { //, SPWeb web try { if (!alert.SendAsSingleMessage) { Notifications notificationSender = new Notifications(); //Need to get the Alert instances MailTemplateUsageObject mtObject = alert.GetMailTemplateUsageObjectForEventType(eventType); //string subject = mtObject.Template.Subject; //string body = mtObject.Template.Body + "<br>" + "<br>" + FinalBody; string subject = notificationSender.ReplacePlaceHolders(mtObject.Template.Subject, properties.ListItem); string body = notificationSender.ReplacePlaceHolders(mtObject.Template.Body, properties.ListItem) + "<br>" + "<br>" + FinalBody; string parentItemId = Convert.ToString(properties.ListItem.ID); DelayedAlert dAlert = new DelayedAlert(subject, body, alert.Id, parentItemId, eventType); alertManager.AddDelayedAlert(dAlert); } else { Notifications notificationSender = new Notifications(); MailTemplateUsageObject mtObject = alert.GetMailTemplateUsageObjectForEventType(eventType); string subject = properties.ListTitle; string body = notificationSender.ReplacePlaceHolders(mtObject.Template.Subject, properties.ListItem) + "<br>" + "<br>" +notificationSender.ReplacePlaceHolders(mtObject.Template.Body,properties.ListItem) + "<br>" + "<br>" + FinalBody; string parentItemId = Convert.ToString(properties.ListItem.ID); DelayedAlert dAlert = new DelayedAlert(subject, body, alert.Id, parentItemId, eventType); alertManager.AddDelayedAlert(dAlert); } } catch { } }
private void CreateDelayedAlert(Alert alert, AlertEventType eventType, SPItemEventProperties properties, AlertManager alertManager) { //, SPWeb web try { if (!alert.SendAsSingleMessage) { Notifications notificationSender = new Notifications(); //Need to get the Alert instances MailTemplateUsageObject mtObject = alert.GetMailTemplateUsageObjectForEventType(eventType); //string subject = mtObject.Template.Subject; //string body = mtObject.Template.Body + "<br>" + "<br>" + FinalBody; string subject = notificationSender.ReplacePlaceHolders(mtObject.Template.Subject, properties.ListItem); string body = notificationSender.ReplacePlaceHolders(mtObject.Template.Body, properties.ListItem) + "<br>" + "<br>" + FinalBody; string parentItemId = Convert.ToString(properties.ListItem.ID); DelayedAlert dAlert = new DelayedAlert(subject, body, alert.Id, parentItemId, eventType); alertManager.AddDelayedAlert(dAlert); } else { Notifications notificationSender = new Notifications(); MailTemplateUsageObject mtObject = alert.GetMailTemplateUsageObjectForEventType(eventType); string subject = properties.ListTitle; string body = notificationSender.ReplacePlaceHolders(mtObject.Template.Subject, properties.ListItem) + "<br>" + "<br>" + notificationSender.ReplacePlaceHolders(mtObject.Template.Body, properties.ListItem) + "<br>" + "<br>" + FinalBody; string parentItemId = Convert.ToString(properties.ListItem.ID); DelayedAlert dAlert = new DelayedAlert(subject, body, alert.Id, parentItemId, eventType); alertManager.AddDelayedAlert(dAlert); } } catch { } }
private void ExecuteReceivedEvent(AlertEventType eventType, SPItemEventProperties properties) { LogManager.write("Entered in to ExecuteReceivedEvent with event type" + eventType); try { using (SPWeb web = properties.OpenWeb()) { //TODO we have to check is feature activated for this site or not AlertManager alertManager = new AlertManager(web.Site.Url); MailTemplateManager mailTemplateManager = new MailTemplateManager(web.Site.Url); IList<Alert> alerts = alertManager.GetAlertForList(properties.ListItem ,eventType, mailTemplateManager); Notifications notifications = new Notifications(); foreach (Alert alert in alerts) { if (eventType != AlertEventType.DateColumn) { if (alert.IsValid(properties.ListItem, eventType, properties)) { try { if (alert.SendType == SendType.ImmediateAlways) { notifications.SendMail(alert, eventType, properties.ListItem, FinalBody); } else if (alert.SendType == SendType.ImmediateBusinessDays) { if (alert.ImmediateBusinessDays.Contains((WeekDays)DateTime.UtcNow.DayOfWeek)) { if (alert.BusinessStartHour <= Convert.ToInt32(DateTime.UtcNow.Hour) && alert.BusinessendtHour >= Convert.ToInt32(DateTime.UtcNow.Hour)) { notifications.SendMail(alert, eventType, properties.ListItem, FinalBody); } else { return; } } } else { CreateDelayedAlert(alert, eventType, properties, alertManager); } } catch { } } } } } } catch (System.Exception Ex) { LogManager.write("Error occured white excuting event receiver" + Ex.Message); } }