161 lines
4.6 KiB
C#
161 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Xml.Serialization;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using SPSolutions.Serialization;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.RemoveAlerts
|
|
{
|
|
public class SiteInfo
|
|
{
|
|
public string Url { get; set; }
|
|
public string Prefix { get; set; }
|
|
}
|
|
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
public event EventHandler<LogEventArgs> 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)
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
if (!web.Exists)
|
|
{
|
|
this.warn("Web site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language);
|
|
|
|
this.removeAlerts(web);
|
|
this.removeAlertsFromPMC(site, web);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void removeAlerts(SPWeb web)
|
|
{
|
|
var alertIds = web.Alerts.Cast<SPAlert>().Select(a => a.ID).ToList();
|
|
foreach (Guid alertId in alertIds)
|
|
{
|
|
web.Alerts.Delete(alertId);
|
|
}
|
|
|
|
foreach (SPWeb subWeb in web.Webs)
|
|
{
|
|
this.removeAlerts(subWeb);
|
|
}
|
|
}
|
|
|
|
private void removeAlertsFromPMC(SPSite site, SPWeb condo)
|
|
{
|
|
var rootWeb = site.RootWeb;
|
|
var alertIds = new List<Guid>();
|
|
var alerts = rootWeb.Alerts.Cast<SPAlert>().ToList();
|
|
foreach (var alert in alerts)
|
|
{
|
|
var alertGroups = getAlertTargetGroups(alert);
|
|
if (alertGroups.Count == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (alertGroups.Any(g => g.ToLower().StartsWith(condo.Title.ToLower())))
|
|
{
|
|
alertIds.Add(alert.ID);
|
|
}
|
|
}
|
|
|
|
foreach (Guid alertId in alertIds)
|
|
{
|
|
rootWeb.Alerts.Delete(alertId);
|
|
}
|
|
}
|
|
|
|
private List<string> getAlertTargetGroups(SPAlert alert)
|
|
{
|
|
try
|
|
{
|
|
if (alert == null)
|
|
{
|
|
return new List<string>();
|
|
}
|
|
var list = new List<string>();
|
|
if (alert.Properties.ContainsKey("AlertGroups") && (alert.Properties["AlertGroups"] != null))
|
|
{
|
|
string str = alert.Properties["AlertGroups"];
|
|
if (!string.IsNullOrEmpty(str))
|
|
{
|
|
var groupNames = SerializationUtil.Deserialize<string[]>(str);
|
|
if ((groupNames != null) && (groupNames.Length > 0))
|
|
{
|
|
list.AddRange(groupNames);
|
|
}
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
return new List<string>();
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|