private void ExecuteTimerAlert(SPWeb web, SPList list, Alert alert) { try { if (web != null && list != null) { DateTime startTime = web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow).AddMinutes(-30.0); DateTime endTime = web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.UtcNow); TimerJobHelper.CaliculateExecutionTime(ref startTime, alert, false); TimerJobHelper.CaliculateExecutionTime(ref endTime, alert, false); int num = !alert.Repeat ? 1 : (alert.RepeatCount + 1); int num2 = 1; while (num2 <= num) { //We need to get all alerts which are fall SPQuery query = new SPQuery(); query.Query = string.Format("<Where>" + "<And>" + "<Gt>" + "<FieldRef Name=\"{0}\" />" + "<Value Type=\"DateTime\" IncludeTimeValue=\"TRUE\">{1}</Value>" + "</Gt>" + "<Leq>" + "<FieldRef Name=\"{0}\" />" + "<Value Type=\"DateTime\" IncludeTimeValue=\"TRUE\">{2}</Value>" + "</Leq>" + "</And>" + "</Where>", new object[] { alert.DateColumnName, SPUtility.CreateISO8601DateTimeFromSystemDateTime(startTime), SPUtility.CreateISO8601DateTimeFromSystemDateTime(endTime) }); SPListItemCollection items = list.GetItems(query); if (items.Count > 0) { foreach (SPListItem item in items) { if (alert.IsValid(item, AlertEventType.DateColumn, null)) { Notifications mailSender = new Notifications(); //mailSender.SendAlert(alert, ChangeTypes.DateColumn, item2, null); mailSender.SendMail(alert, AlertEventType.DateColumn, item); } else { //Some conditions are not passthrough } } } else { //No items returned as part of Query } if (num2 < num) { TimerJobHelper.CaliculateExecutionTime(ref startTime, alert, true); TimerJobHelper.CaliculateExecutionTime(ref endTime, alert, true); } num2++; } } } catch { //Error occured while executing timer alerts } }
public void SendDelayedMessage2(DelayedAlert delayedAlert, Alert alert, SPListItem item) { Notifications notificationSender = new Notifications(); try { MailTemplateUsageObject mtObject = alert.GetMailTemplateUsageObjectForEventType(delayedAlert.AlertType); toAddress = notificationSender.GetRecipientEmailAddresses(alert.ToAddress, item); ccAddress = notificationSender.GetRecipientEmailAddresses(alert.CcAddress, item); fromAddress = notificationSender.GetRecipientEmailAddresses(alert.FromAdderss, item); subject = delayedAlert.Subject; body += delayedAlert.Body; smtpSName = notificationSender.GetSMTPServer(item); //SendMail(smtpSName, // toAddress, // fromAddress, // ccAddress, // subject, // body, // null); } 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 { } }
internal void ExecuteDelayedMessages(Alert alert) { try { SPQuery query = new SPQuery(); query.Query = string.Format("<Where><Eq><FieldRef Name=\"{0}\" LookupId=\"TRUE\"/><Value Type=\"Lookup\">{1}</Value></Eq></Where>", "Alert", alert.Id); SPListItemCollection items = this.delayedAlertList.GetItems(query); if (items.Count > 0) { foreach (SPListItem item in items) { try { try { if (alert.SendAsSingleMessage) { DelayedAlert delayedAlert = new DelayedAlert(item); Notifications notificationSender = new Notifications(); SendDelayedMessage2(delayedAlert, alert,item); } else { DelayedAlert delayedAlert = new DelayedAlert(item); Notifications notificationSender = new Notifications(); notificationSender.SendDelayedMessage(delayedAlert, alert,item); } } catch { } continue; } finally { try { //Delete the delayed alert after completion } catch { } } } if (alert.SendAsSingleMessage) { SendMail(smtpSName, toAddress, fromAddress,ccAddress,subject,body,null); } } else { //No delayed alert found in the Delayed alert list } } 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); } }