Taylohtio/CondoUpdate/CondoUpdate.RenameTaloyhtio.../UpdaterImpl.cs

116 lines
3.6 KiB
C#

using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Navigation;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.RenameTaloyhtioToOsuriaInGlobalNav
{
public class UpdaterImpl : ICondoUpdater
{
// private const string TargetPageUrl = "johdonsalkku.aspx";
// private const string TargetTitle = "Taloyhtio.Info";
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)
{
try
{
using (var site = new SPSite(url))
{
using (var pmcWeb = site.RootWeb)
{
if (!pmcWeb.Exists)
{
return;
}
if (!PublishingWeb.IsPublishingWeb(pmcWeb))
{
return;
}
var pweb = PublishingWeb.GetPublishingWeb(pmcWeb);
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) pmcWeb.Language);
renameGlobalNavNode(pweb, pmcWeb);
}
}
}
catch (Exception ex)
{
this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message,
ex.StackTrace);
}
}
private void renameGlobalNavNode(PublishingWeb pweb, SPWeb pmcWeb)
{
try
{
var globalNavigation = pweb.Navigation.GlobalNavigationNodes;
var node = globalNavigation.Cast<SPNavigationNode>().FirstOrDefault(spnode => string.Compare(spnode.Title, "Taloyhtio.Info", true) == 0);
if (node == null)
{
return;
}
node.Title = "Osuria";
node.Update();
}
catch (Exception ex)
{
this.error("Error occured when change global menu item to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message, ex.StackTrace);
}
}
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 });
}
}
}
}