using System; using System.Linq; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using SPSolutions; using SPSolutions.SharePoint.Alerts; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ChangeImmediateAlertsToSummaryAlerts { public class UpdaterImpl : ICondoUpdater { 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(url); } 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) { // condo fixList(url + "/asukkaille", "/Documents"); fixList(url + "/osakkaille", "/Documents"); fixList(url + "/hallitukselle", "/Documents"); // company // fixList(url, "/Documents"); // fixList(url, "/Lists/Asiakirjojen%20lhetys%20tilitoimistolle"); // fixList(url, "/Tuntilistat%20ja%20verokortit"); } private void fixList(string webUrl, string listUrl, bool reportErrorIfListNotExists = true) { //log("Check list '{0}' in web '{1}'", listUrl, webUrl); using (var site = new SPSite(webUrl)) { using (var web = site.OpenWeb()) { SPList list = null; try { string url = SPUrlUtility.CombineUrl(webUrl, listUrl); list = web.GetList(url); } catch { } if (list == null) { if (reportErrorIfListNotExists) { error("List '{0}' can't be found", listUrl); } return; } // get first alert from list (normally in all lists affected by alert creation tool there is only 1 alert) var alert = this.getFirstListAlert(web, list); if (alert == null) { //log("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}')", listUrl); return; } if (!isCustom(alertTemplate)) { error("Alert template '{0}' is not custom (list '{1}')", alertTemplate.DisplayName, listUrl); return; } if (this.isDailySummaryAlert(alert)) { // it is already summary alert return; } EnsureAlertProperty(alert, AlertManagerConstants.MAKE_DAILY_SUMMARY_FROM_IMMEDIATE_PROPERTY_BAG_KEY, true); alert.Update(); } } } private bool isDailySummaryAlert(SPAlert alert) { if (alert == null || alert.Properties == null || !alert.Properties.ContainsKey(AlertManagerConstants.MAKE_DAILY_SUMMARY_FROM_IMMEDIATE_PROPERTY_BAG_KEY)) { return false; } bool result = false; if (!bool.TryParse(alert.Properties[AlertManagerConstants.MAKE_DAILY_SUMMARY_FROM_IMMEDIATE_PROPERTY_BAG_KEY], out result)) { return false; } return result; } // copy of SPSolutions.SharePoint.Alerts.SPAlertUtil.EnsureAlertProperty // public static void EnsureAlertProperty(SPAlert alert, string propertyName, string propertyValue) // { // if (alert.Properties.ContainsKey(propertyName)) // { // alert.Properties[propertyName] = propertyValue; // return; // } // alert.Properties.Add(propertyName, propertyValue); // } public static void EnsureAlertProperty(SPAlert alert, string propertyName, object propertyValue) { SPAlertUtil.EnsureAlertProperty(alert, propertyName, propertyValue.ToString()); } private SPAlert getFirstListAlert(SPWeb web, SPList list) { 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 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 }); } } } }