204 lines
7.8 KiB
C#
204 lines
7.8 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 Microsoft.SharePoint.Utilities;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.TilaaIlmoitaGlobalNavigation
|
|
{
|
|
public class UpdaterImpl:ICondoUpdater
|
|
{
|
|
private const string NewPageTitle = "Tilaa & Ilmoita";
|
|
private const string OldPageTitle = "Isännöitsijän palsta";
|
|
private readonly Guid _webFeatureId = new Guid("28612bce-9bf5-423f-94d4-481fea2b5b40");
|
|
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)
|
|
{
|
|
if (string.IsNullOrEmpty(url)) return;
|
|
ChangePmcGlobalNavigation(url);
|
|
ChangeCondoGlobalNavigation(url);
|
|
EnableTilaaIlmoitaList(url);
|
|
}
|
|
|
|
private void ChangePmcGlobalNavigation(string url)
|
|
{
|
|
try
|
|
{
|
|
ChangeGlobalNavigation(url, false);
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when update pmc global navigation on {0} error: {1} {2}", url, x.Message, x.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void ChangeCondoGlobalNavigation(string url)
|
|
{
|
|
try
|
|
{
|
|
ChangeGlobalNavigation(url, true);
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when update condo global navigation on {0} error: {1} {2}", url, x.Message, x.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void ChangeGlobalNavigation(string url, bool isCondo)
|
|
{
|
|
this.warn("=== start tilaa and ilmoita plugin on {1} web: {0}", url, isCondo ? "condo" : "pmc");
|
|
using (var site = new SPSite(url))
|
|
using (var web = isCondo ? site.OpenWeb() : site.RootWeb)
|
|
{
|
|
try
|
|
{
|
|
if (web == null || !web.Exists)
|
|
{
|
|
this.warn("{1} web does not exist: {0}", url, isCondo ? "condo" : "pmc");
|
|
return;
|
|
}
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
var globalNavigation = pweb.Navigation.GlobalNavigationNodes;
|
|
|
|
var palstaWeb = web.Webs.Cast<SPWeb>().FirstOrDefault(w => w.Title.StartsWith(OldPageTitle));
|
|
if (palstaWeb != null && palstaWeb.Exists)
|
|
{
|
|
this.warn("'{0}' web found", OldPageTitle);
|
|
var publishingPalstaWeb = PublishingWeb.GetPublishingWeb(palstaWeb);
|
|
publishingPalstaWeb.IncludeInGlobalNavigation = false;
|
|
publishingPalstaWeb.Update();
|
|
this.warn("'{0}' web excluded", OldPageTitle);
|
|
}
|
|
else
|
|
{
|
|
this.warn("'{0}' web not found", OldPageTitle);
|
|
}
|
|
|
|
// if it still exists...
|
|
var palstaNode = globalNavigation.Cast<SPNavigationNode>().FirstOrDefault(n => n.Title.StartsWith(OldPageTitle));
|
|
if (palstaNode != null)
|
|
{
|
|
try
|
|
{
|
|
this.warn("trying to delete global navigation node on {1} web: {0}", url, isCondo ? "condo" : "pmc");
|
|
globalNavigation.Delete(palstaNode);
|
|
this.warn("no exceptions");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
this.warn("got {0} exception with message: {1}", e.ToString(), e.Message);
|
|
}
|
|
}
|
|
|
|
var tilaaIlmoitaNode = globalNavigation.Cast<SPNavigationNode>().FirstOrDefault(p => p.Title.Equals(NewPageTitle));
|
|
if (tilaaIlmoitaNode == null)
|
|
{
|
|
this.warn("'{0}' navigation node does not exist, creating new one", NewPageTitle);
|
|
var node = SPNavigationSiteMapNode.CreateSPNavigationNode(NewPageTitle, SPUrlUtility.CombineUrl(site.RootWeb.Url, "lomakkeet"), NodeTypes.Heading, globalNavigation);
|
|
var dt = DateTime.Now;
|
|
node.Properties["CreatedDate"] = dt;
|
|
node.Properties["LastModifiedDate"] = dt;
|
|
node.Properties["Description"] = "";
|
|
node.Properties["Target"] = "";
|
|
node.Properties["Taloyhtio"] = "1";
|
|
node.Update();
|
|
node.MoveToLast(globalNavigation);
|
|
}
|
|
else
|
|
{
|
|
this.warn("'{0}' navigation node already exists", NewPageTitle);
|
|
}
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured while working with lomakkeet: {0} {1}", x.Message, x.StackTrace);
|
|
}
|
|
}
|
|
this.warn("=== end tilaa and ilmoita plugin on {1} web: {0}\n\n", url, isCondo ? "condo" : "pmc");
|
|
}
|
|
|
|
private void EnableTilaaIlmoitaList(string url)
|
|
{
|
|
try
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
|
|
using (var web = site.RootWeb)
|
|
{
|
|
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);
|
|
if (!web.Features.Any(f => f.DefinitionId.Equals(_webFeatureId)))
|
|
{
|
|
web.Features.Add(_webFeatureId, true);
|
|
}
|
|
else
|
|
{
|
|
this.warn("Feature already activated on PMC '{0}', ignore", url);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when enable Tilaa & Ilmoita list on {0} error: {1} {2}", url, ex.Message,
|
|
ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|