Taylohtio/CondoUpdate/CondoUpdate.ResponsiveLayou.../UpdaterImpl.cs

115 lines
4.0 KiB
C#

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.ResponsiveLayout.RemoveEmailAlertPages
{
class UpdaterImpl : ICondoUpdater
{
private const string menuText = "Tilaa sähköpostihälytys";
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)
{
this.info("Opening Condo '{0}'...", url);
using (var site = new SPSite(url))
{
using (var condoWeb = site.OpenWeb())
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)condoWeb.Language);
if (!condoWeb.Exists || string.Compare(condoWeb.Url, url, true) != 0)
{
this.warn("Condo site '{0}' doesn't exist. It will be ignored", url);
return;
}
if (!PublishingWeb.IsPublishingWeb(condoWeb))
{
this.warn("Condo site '{0}' is not publishing web. It will be ignored", url);
return;
}
var pweb = PublishingWeb.GetPublishingWeb(condoWeb);
this.info("Condo '{0}' was sucessfully opened", url);
#if USE_NAV
var globalNav = pweb.Navigation.GlobalNavigationNodes;
globalNav.Cast<SPNavigationNode>().Where(n => string.Compare(menuText, n.Title, true) == 0).ToList().ForEach((node) =>
{
var pageUrl = node.Url;
this.info("Got page '{0}'", pageUrl);
var subWeb = pweb.GetPublishingWebs().Cast<PublishingWeb>().FirstOrDefault(w => string.Compare(pageUrl, w.Web.ServerRelativeUrl, true) == 0);
if (subWeb != null)
{
this.info("Deleting subsite '{0}'", subWeb.Title);
subWeb.Delete();
}
});
#else
//pweb.GetPublishingWebs().Cast<PublishingWeb>().Where(w => string.Compare(menuText, w.Title, true) == 0).ToList().ForEach((subWeb) =>
pweb.GetPublishingWebs().Cast<PublishingWeb>().Where(w => w.Title.StartsWith(menuText)).ToList().ForEach((subWeb) =>
{
this.info("Deleting subsite '{0}' with url: {1}", subWeb.Title, subWeb.Web.ServerRelativeUrl);
subWeb.Delete();
});
#endif
pweb.Update();
}
}
}
#region notifier
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 });
}
}
#endregion
}
}