308 lines
12 KiB
C#
308 lines
12 KiB
C#
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
using System.Xml.Serialization;
|
|
using SPSolutions.SharePoint.Alerts;
|
|
using Taloyhtio.CustomAlertHandler.CodeFiles;
|
|
|
|
namespace AlertTemplateUpdater
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
if (args.Length != 3)
|
|
{
|
|
Console.WriteLine("Usage: exe <web_url> <list_title> <command: [enum_all_alert_templates] [print_list_alert_template] [print_list_alerts] [update_list_alert_template]z>");
|
|
return;
|
|
}
|
|
|
|
string command = args[2];
|
|
if (command == "enum_all_alert_templates")
|
|
{
|
|
enumAllAlertTemplates();
|
|
return;
|
|
}
|
|
|
|
if (command == "print_list_alert_template")
|
|
{
|
|
printListAlertTemplate(args[0], args[1]);
|
|
return;
|
|
}
|
|
|
|
if (command == "print_list_alerts")
|
|
{
|
|
printListAlerts(args[0], args[1]);
|
|
return;
|
|
}
|
|
|
|
if (command == "update_list_alert_template")
|
|
{
|
|
updateListAlertTemplate(args[0], args[1]);
|
|
return;
|
|
}
|
|
}
|
|
|
|
private static void printListAlerts(string webUrl, string listTitle)
|
|
{
|
|
using (var site = new SPSite(webUrl))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
var list = web.Lists.Cast<SPList>().First(l => string.Compare(l.Title, listTitle, true) == 0);
|
|
if (list == null)
|
|
{
|
|
Console.WriteLine("List '{0}' not found", listTitle);
|
|
return;
|
|
}
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendFormat("List '{0}' has the following alerts:{1}", list.Title, Environment.NewLine);
|
|
|
|
var alerts = web.Alerts.Cast<SPAlert>();
|
|
var listAlerts = alerts.Where(a => a.ListID == list.ID).ToList();
|
|
if (!listAlerts.IsNullOrEmpty())
|
|
{
|
|
listAlerts.ForEach(a => checkAlert(a, sb));
|
|
}
|
|
else
|
|
{
|
|
sb.AppendFormat("(no alerts){0}", Environment.NewLine);
|
|
}
|
|
|
|
Console.WriteLine(sb.ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void updateListAlertTemplate(string webUrl, string listTitle)
|
|
{
|
|
using (var site = new SPSite(webUrl))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == listTitle);
|
|
if (list == null)
|
|
{
|
|
Console.WriteLine("List '{0}' not found", listTitle);
|
|
return;
|
|
}
|
|
|
|
var alertTemplate = list.AlertTemplate;
|
|
|
|
Console.WriteLine("Alert template before:");
|
|
Console.WriteLine(alertTemplate.Xml);
|
|
Console.WriteLine("/////////////////////////////////////");
|
|
|
|
// alertTemplate.Properties["NotificationHandlerAssembly"] =
|
|
// "Taloyhtio.CustomAlertHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ecbf947198042521";
|
|
// alertTemplate.Properties["NotificationHandlerClassName"] =
|
|
// "Taloyhtio.CustomAlertHandler.CodeFiles.AlertHandlerWithFromField";
|
|
// alertTemplate.Properties["NotificationHandlerProperties"] = "";
|
|
|
|
|
|
|
|
alertTemplate.Xml = AlertTemplateConfigurator.UpdateXml(alertTemplate.Xml, typeof(AlertHandlerWithFromField));
|
|
alertTemplate.Update();
|
|
|
|
Console.WriteLine("Alert template was updated");
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ensureElement(XElement parent, string childName, string childValue)
|
|
{
|
|
if (parent.Elements(childName).Count() == 0)
|
|
{
|
|
parent.Add(new XElement(childName));
|
|
}
|
|
var child = parent.Element(childName);
|
|
child.SetValue(childValue);
|
|
}
|
|
|
|
private static void printListAlertTemplate(string webUrl, string listTitle)
|
|
{
|
|
using (var site = new SPSite(webUrl))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == listTitle);
|
|
if (list == null)
|
|
{
|
|
Console.WriteLine("List '{0}' not found", listTitle);
|
|
return;
|
|
}
|
|
|
|
Console.WriteLine("List id: {0}", list.ID);
|
|
var at = list.AlertTemplate;
|
|
Console.WriteLine(at.Xml);
|
|
Console.WriteLine();
|
|
Console.WriteLine("Properties of list alert template:\n");
|
|
foreach (DictionaryEntry e in at.Properties)
|
|
{
|
|
if (string.Compare((string)e.Key, "NotificationMessage", true) == 0)
|
|
{
|
|
Console.WriteLine("\t{0}:\t(skipped)", e.Key);
|
|
//Console.WriteLine("\t{0}:\t{1}", e.Key, e.Value);
|
|
}
|
|
else if (string.Compare((string)e.Key, "Taloyhtio_AffectedLists", true) == 0)
|
|
{
|
|
var lists = e.Value as List<Guid>;
|
|
if (lists == null)
|
|
{
|
|
Console.WriteLine("\t{0}:\t(null)", e.Key);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("\t{0}:\t{1}", e.Key, string.Join(",", lists.Select(g => g.ToString()).ToArray()));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("\t{0}:\t{1}", e.Key, e.Value);
|
|
}
|
|
}
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("Existing alerts:\n");
|
|
foreach (SPAlert alert in list.ParentWeb.Alerts)
|
|
{
|
|
if (alert.ListID == list.ID)
|
|
{
|
|
Console.WriteLine("Title: {0}\tTemplate: {1}", alert.Title, alert.AlertTemplateName);
|
|
var t = alert.AlertTemplate;
|
|
Console.WriteLine("Tempalte XML : \n");
|
|
Console.WriteLine(t.Xml + "\n\n");
|
|
Console.WriteLine("Properties of attached alert's alert template:\n");
|
|
foreach (DictionaryEntry e in t.Properties)
|
|
{
|
|
if (string.Compare((string)e.Key, "NotificationMessage", true) == 0)
|
|
{
|
|
Console.WriteLine("\t{0}:\t(skipped)", e.Key);
|
|
}
|
|
else if (string.Compare((string)e.Key, "Taloyhtio_AffectedLists", true) == 0)
|
|
{
|
|
var lists = e.Value as List<Guid>;
|
|
if (lists == null)
|
|
{
|
|
Console.WriteLine("\t{0}:\t(null)", e.Key);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("\t{0}:\t{1}", e.Key, string.Join(",", lists.Select(g => g.ToString()).ToArray()));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("\t{0}:\t{1}", e.Key, e.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void enumAllAlertTemplates()
|
|
{
|
|
var items = SPAlertTemplateUtil.GetSPAlertTemplatesInContext();
|
|
foreach (var item in items)
|
|
{
|
|
if (AlertTemplateConfigurator.IsCustom(item))
|
|
{
|
|
Console.WriteLine("/////////////////////////////////////");
|
|
Console.WriteLine("Template friendly name: {0}{1}", getTemplateFriendlyName(item), Environment.NewLine);
|
|
Console.WriteLine(item.Xml);
|
|
Console.WriteLine("Properties:");
|
|
foreach (DictionaryEntry p in item.Properties)
|
|
{
|
|
Console.WriteLine("{0}:\t{1}", p.Key, p.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void checkAlert(SPAlert alert, StringBuilder sb)
|
|
{
|
|
var properties = alert.Properties;
|
|
sb.AppendFormat("\t\tAlert {0}{1}", alert.Title, Environment.NewLine);
|
|
sb.AppendFormat("\t\t\tTemplate name: {0}{1}", alert.AlertTemplate.Name, Environment.NewLine);
|
|
sb.AppendFormat("\t\t\tTemplate friendly name: {0}{1}", getTemplateFriendlyName(alert.AlertTemplate), Environment.NewLine);
|
|
sb.AppendFormat("\t\t\tUser: {0}{1}", alert.User, Environment.NewLine);
|
|
sb.AppendFormat("\t\t\tGroups: {0}{1}", getGroups(properties), Environment.NewLine);
|
|
}
|
|
|
|
private static string getTemplateFriendlyName(SPAlertTemplate t)
|
|
{
|
|
if (t == null)
|
|
{
|
|
return "<empty>";
|
|
}
|
|
var properties = t.Properties;
|
|
if (properties == null)
|
|
{
|
|
return "<empty>";
|
|
}
|
|
if (!properties.ContainsKey("FriendlyName"))
|
|
{
|
|
return "<empty>";
|
|
}
|
|
var s = properties["FriendlyName"] as string;
|
|
return string.IsNullOrEmpty(s) ? "<empty>" : s;
|
|
}
|
|
|
|
private static string getGroups(SPPropertyBag properties)
|
|
{
|
|
if (properties == null)
|
|
{
|
|
return "<null>";
|
|
}
|
|
|
|
if (!properties.ContainsKey("alertgroups"))
|
|
{
|
|
return "<null>";
|
|
}
|
|
|
|
var list = Deserialize<List<string>>(properties["alertgroups"]);
|
|
return printGroups(list);
|
|
}
|
|
|
|
private static string printGroups(List<string> groups)
|
|
{
|
|
if (groups == null || groups.Count == 0)
|
|
{
|
|
return "<empty>";
|
|
}
|
|
return string.Join(", ", groups.ToArray());
|
|
}
|
|
|
|
private static T Deserialize<T>(string s) where T : class
|
|
{
|
|
if (string.IsNullOrEmpty(s))
|
|
{
|
|
throw new ArgumentNullException("s");
|
|
}
|
|
var serializer = new XmlSerializer(typeof(T));
|
|
var stringReader = new StringReader(s);
|
|
return (T)serializer.Deserialize(stringReader);
|
|
}
|
|
}
|
|
|
|
public static class EnumerableExtensions
|
|
{
|
|
public static bool IsNullOrEmpty<T>(this IEnumerable<T> items)
|
|
{
|
|
// see http://haacked.com/archive/2010/06/10/checking-for-empty-enumerations.aspx
|
|
//return (items == null || items.Count() == 0);
|
|
return (items == null || !items.Any());
|
|
}
|
|
}
|
|
}
|