179 lines
7.0 KiB
C#
179 lines
7.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.AddSisaisetAsiatGlobalMenuItemTallier
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string TargetPageUrl = "sisäisetasiat.aspx";
|
|
private const string TargetTitle = "Sisäiset asiat";
|
|
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 acWeb = site.RootWeb)
|
|
{
|
|
if (!acWeb.Exists)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(acWeb))
|
|
{
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(acWeb);
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)acWeb.Language);
|
|
addGlobalNavNode(pweb, acWeb);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message,
|
|
ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void addGlobalNavNode(PublishingWeb pweb, SPWeb pmcWeb)
|
|
{
|
|
try
|
|
{
|
|
// var globalNavigation = pweb.Navigation.GlobalNavigationNodes;
|
|
// var existingNode = globalNavigation.Cast<SPNavigationNode>().FirstOrDefault(spnode => spnode.Title.Equals(TargetTitle));
|
|
// if (existingNode != null)
|
|
// {
|
|
// this.warn(string.Format("{0} menu item already exists. Site won't be updated", TargetTitle));
|
|
// return;
|
|
// }
|
|
// var pageExists = pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages").Files.Cast<SPFile>().Any(f => f.Name.ToLower() == "sisäisetasiat.aspx");
|
|
// if (!pageExists)
|
|
// {
|
|
// this.error("Unable to find guide pages. Site won't be updated");
|
|
// return;
|
|
// }
|
|
// var file =
|
|
// pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages")
|
|
// .Files.Cast<SPFile>()
|
|
// .First(f => f.Name.ToLower() == TargetPageUrl);
|
|
//
|
|
// var node = SPNavigationSiteMapNode.CreateSPNavigationNode(TargetTitle,
|
|
// file == null ? pmcWeb.ServerRelativeUrl : file.Url,
|
|
// NodeTypes.AuthoredLinkPlain, globalNavigation);
|
|
//
|
|
// var dt = DateTime.Now;
|
|
// node.Properties["CreatedDate"] = dt;
|
|
// node.Properties["LastModifiedDate"] = dt;
|
|
// node.Properties["Description"] = "";
|
|
// node.Update();
|
|
// node.MoveToLast(globalNavigation);
|
|
// var file = pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages").Files.Cast<SPFile>().FirstOrDefault(f => f.Name.ToLower() == "sisäisetasiat.aspx");
|
|
// if (file == null)
|
|
// {
|
|
// this.error("Unable to find guide pages. Site won't be updated");
|
|
// return;
|
|
// }
|
|
var page = pweb.GetPublishingPages().FirstOrDefault(p => string.Compare(p.Name, "sisäisetasiat.aspx", true) == 0);
|
|
if (page == null)
|
|
{
|
|
this.error("Unable to find guide pages. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
var file = pweb.PagesList.RootFolder.Files.Cast<SPFile>().FirstOrDefault(f => string.Compare(f.Name, page.Name, true) == 0);
|
|
if (file == null)
|
|
{
|
|
this.error("cannot access file '{0}', global navigation will not be updated", page.Name);
|
|
return;
|
|
}
|
|
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
page.IncludeInGlobalNavigation = true;
|
|
page.Update();
|
|
|
|
file.Update();
|
|
file.CheckIn("included in global navigation");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("included in global navigation");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("included in global navigation");
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when add global menu item to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
#region notify
|
|
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
|
|
}
|
|
}
|