using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using SPSolutions.SharePoint.Alerts; using Taloyhtio.CustomAlertHandler.CodeFiles; namespace FixCondoAlert { class Program { static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: FixCondoAlert.exe "); return; } string condoUrl = args[0]; fixList(condoUrl, "/Lists/Ilmoitustaulu"); fixList(condoUrl + "/asukkaille", "/Documents"); fixList(condoUrl + "/osakkaille", "/Documents"); fixList(condoUrl + "/Osakkaille", "/Lists/Osakastiedotteet"); fixList(condoUrl + "/hallitukselle", "/Documents"); fixList(condoUrl + "/Hallitukselle", "/Lists/Hallituksen%20tiedotteet"); fixList(condoUrl + "/Hallitukselle", "/Lists/Kalenteri"); fixList(condoUrl + "/Hallitukselle", "/Lists/Keskustelupalsta"); } private static void fixList(string webUrl, string listUrl) { 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) { log("List can't be found"); return; } string msg; bool? alreadyActivated = AlertTemplateConfigurator.HasEnabledFromField(list, out msg); if (alreadyActivated != null && alreadyActivated.Value) { log("Custom alert handler is already activated"); return; } // get first alert from list (normally in all lists affected by alert creation tool there is only 1 alert) var alert = 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) { log("Alert's alert template is null"); return; } if (!isCustom(alertTemplate)) { log("Alert template '{0}' is not custom", alertTemplate.DisplayName); return; } // assign alert template list.AlertTemplate = alertTemplate; list.Update(); log("List's alert template was set to '{0}'", alertTemplate.DisplayName); if (!AlertTemplateConfigurator.ActivateCustomAlertHandlerOnList(list, true, out msg)) { log("There was problem when activating custom alert handler: '{0}'", msg); return; } log("Custom alert handler was successfilly activated"); } } } private static 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 static bool isCustom(SPAlertTemplate t) { return (((t.Properties["Custom"] != null) && (t.Properties["Custom"] is string)) && (((string)t.Properties["Custom"]) == SPAlertTemplateUtil.CustomAlertTemplateIdentifier)); } private static void log(string msg, params object[] args) { Console.WriteLine(msg, args); } } }