142 lines
5.5 KiB
C#
142 lines
5.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Navigation;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using Microsoft.SharePoint.Publishing.Navigation;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.CondoRenameFrontPage
|
|
{
|
|
public class UpdaterImpl:ICondoUpdater
|
|
{
|
|
private const string PageTitle = "Etusivu";
|
|
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.changeGlobalNavigation(url, PageTitle);
|
|
}
|
|
|
|
private void changeGlobalNavigation(string url, string pageTitle)
|
|
{
|
|
if (string.IsNullOrEmpty(url)) return;
|
|
try
|
|
{
|
|
SPSecurity.RunWithElevatedPrivileges(() =>
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
|
|
using (var condoWeb = site.OpenWeb())
|
|
{
|
|
if (!condoWeb.Exists)
|
|
{
|
|
return;
|
|
}
|
|
bool allowUnsafeUpdates = condoWeb.AllowUnsafeUpdates;
|
|
condoWeb.AllowUnsafeUpdates = true;
|
|
var pCondoWeb = PublishingWeb.GetPublishingWeb(condoWeb);
|
|
var condoGlobalNavigation = pCondoWeb.Navigation.GlobalNavigationNodes;
|
|
|
|
var condoNodePage = condoGlobalNavigation.Cast<SPNavigationNode>().FirstOrDefault(n => string.Compare(condoWeb.Title, n.Title, true) == 0);
|
|
|
|
if (condoNodePage != null)
|
|
{
|
|
condoNodePage.Delete();
|
|
this.info("'{0}' Condo name navigation item is removed", condoWeb.Title);
|
|
}
|
|
else
|
|
{
|
|
this.info("'{0}' Condo name navigation item is not found", condoWeb.Title);
|
|
}
|
|
|
|
pCondoWeb = PublishingWeb.GetPublishingWeb(condoWeb);
|
|
condoGlobalNavigation = pCondoWeb.Navigation.GlobalNavigationNodes;
|
|
|
|
var condoEtusivuNode = condoGlobalNavigation.Cast<SPNavigationNode>().FirstOrDefault(p => p.Title.Equals(pageTitle));
|
|
if (condoEtusivuNode == null)
|
|
{
|
|
var condoNode = SPNavigationSiteMapNode.CreateSPNavigationNode(pageTitle, condoWeb.Url, NodeTypes.Heading, condoGlobalNavigation);
|
|
var dt = DateTime.Now;
|
|
if (condoNode != null) {
|
|
if (condoNode.Properties != null)
|
|
{
|
|
condoNode.Properties["CreatedDate"] = dt;
|
|
condoNode.Properties["LastModifiedDate"] = dt;
|
|
condoNode.Properties["Description"] = "";
|
|
condoNode.Properties["Target"] = "";
|
|
condoNode.Properties["Taloyhtio"] = "1";
|
|
}
|
|
condoNode.Update();
|
|
condoNode.MoveToFirst(condoGlobalNavigation);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.warn("'{0}' navigation node already exists", pageTitle);
|
|
//condoEtusivuNode.Delete();
|
|
}
|
|
condoWeb.Update();
|
|
condoWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when rename front page on {0} error: {1} {2}", url, x.Message,
|
|
x.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 });
|
|
}
|
|
}
|
|
}
|
|
}
|