using System; using System.Globalization; using System.Linq; using System.Threading; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using SPSolutions.SharePoint.Alerts; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ChangeDiscussionBoardAlertTemplates { public class UpdaterImpl : ICondoUpdater { private const string ALERT_TEMPLATE_BODY_PROPERTY_NAME = "NotificationMessage"; public event EventHandler OnNotify; public void Update(object args) { string url = args as string; if (string.IsNullOrEmpty(url)) { this.warn("Url is empty"); return; } try { this.updateImpl(SPUrlUtility.CombineUrl(url, "hallitukselle")); } catch(Exception x) { this.error("Error occured during updating of Condo '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace); } } private void updateImpl(string url) { using (var site = new SPSite(url)) { using (var web = site.OpenWeb()) { if (!web.Exists || string.Compare(web.Url, url, true) != 0) { return; } var list = web.Lists.Cast().FirstOrDefault(l => l.Title == "Keskustelupalsta"); if (list == null) { this.warn("Keskustelupalsta list not found"); return; } this.ensureText(list); } } } private void ensureText(SPList l) { try { //var alertTemplate = l.AlertTemplate; var alert = getFirstListAlert(l); if (alert == null) { //error("There are no alerts in the list"); return; } // get alert template from this list var alertTemplate = alert.AlertTemplate; if (alertTemplate == null) { //error("Alert's alert template is null (list '{0}')", l.DefaultViewUrl); return; } if (!isCustom(alertTemplate)) { //error("Alert template '{0}' is not custom (list '{1}')", alertTemplate.DisplayName, l.DefaultViewUrl); return; } if (alertTemplate.Properties == null || !alertTemplate.Properties.ContainsKey(ALERT_TEMPLATE_BODY_PROPERTY_NAME)) { //error("Alert template '{0}' doesn't have {1} property (list '{2}')", alertTemplate.DisplayName, ALERT_TEMPLATE_BODY_PROPERTY_NAME, l.DefaultViewUrl); return; } string body = alertTemplate.Properties[ALERT_TEMPLATE_BODY_PROPERTY_NAME] as string; if (string.IsNullOrEmpty(body)) { //error("{0} property is empty (list '{1}')", ALERT_TEMPLATE_BODY_PROPERTY_NAME, l.DefaultViewUrl); return; } if (body.Contains("{ParentDiscussionTitle}")) { //info("Body already contains ParentDiscussionTitle placeholder"); return; } if (!body.Contains("<p><strong>{Title}</strong></p>")) { //error("Body doesn't contain Title placeholder"); return; } alertTemplate.Properties[ALERT_TEMPLATE_BODY_PROPERTY_NAME] = body.Replace("<p><strong>{Title}</strong></p>", "<p><strong>{ParentDiscussionTitle}</strong></p>"); alertTemplate.Update(); } catch (Exception x) { this.error("Error occured when try to update alert template for list '{0}': {1}\n{2}", l.DefaultViewUrl, x.Message, x.StackTrace); } } private static SPAlert getFirstListAlert(SPList list) { var web = list.ParentWeb; var alerts = web.Alerts.Cast(); return alerts.FirstOrDefault(a => a.ListID == list.ID); } // This is copy of Alert template's AlertTemplateManager.IsCustom property implementation private static bool isCustom(SPAlertTemplate t) { return (((t.Properties["Custom"] != null) && (t.Properties["Custom"] is string)) && (((string)t.Properties["Custom"]) == SPAlertTemplateUtil.CustomAlertTemplateIdentifier)); } private void info(string msg, params object[] args) { this.notify(LogLevel.Info, msg, args); } private void warn(string msg, params object[] args) { this.notify(LogLevel.Warn, msg, args); } private void error(string msg, params object[] args) { this.notify(LogLevel.Error, msg, args); } private void notify(LogLevel level, string msg, params object[] args) { this.notify(level, string.Format(msg, args)); } private void notify(LogLevel level, string msg) { if (this.OnNotify != null) { this.OnNotify(this, new LogEventArgs { LogLevel = level, Message = msg }); } } } }