245 lines
8.8 KiB
C#
245 lines
8.8 KiB
C#
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.SetDefaultPageLayoutPmc
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string CONDO_PREFIX = "as oy ";
|
|
private const string CONDO_WEB_TEMPLATE_NAME = "TaloyhtioCondoSite";
|
|
private const int CONDO_SITE_WEB_TEMPLATE_ID = 10101;
|
|
private const short CONDO_WEB_CONFIGURATION = 0;
|
|
|
|
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)
|
|
{
|
|
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);
|
|
|
|
this.addAvailablePageLayouts(site);
|
|
this.fixWebs(web);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void addAvailablePageLayouts(SPSite site)
|
|
{
|
|
// page templates
|
|
var pweb = PublishingWeb.GetPublishingWeb(site.RootWeb);
|
|
var pageLayouts = new List<PageLayout>(pweb.GetAvailablePageLayouts());
|
|
|
|
var psite = new PublishingSite(site);
|
|
var allPageLayouts = psite.GetPageLayouts(true);
|
|
|
|
if (!pageLayouts.Any(l => string.Compare(l.Name, "taloyhtio_responsive_layout.aspx", true) == 0))
|
|
{
|
|
var pl = allPageLayouts.FirstOrDefault(l => string.Compare(l.Name, "taloyhtio_responsive_layout.aspx", true) == 0);
|
|
if (pl != null)
|
|
{
|
|
pageLayouts.Add(pl);
|
|
}
|
|
}
|
|
|
|
if (!pageLayouts.Any(l => string.Compare(l.Name, "taloyhtio_responsive_layout_wide.aspx", true) == 0))
|
|
{
|
|
var pl = allPageLayouts.FirstOrDefault(l => string.Compare(l.Name, "taloyhtio_responsive_layout_wide.aspx", true) == 0);
|
|
if (pl != null)
|
|
{
|
|
pageLayouts.Add(pl);
|
|
}
|
|
}
|
|
|
|
pweb.SetAvailablePageLayouts(pageLayouts.ToArray(), true);
|
|
pweb.Update();
|
|
this.notify(LogLevel.Info, "applied page layouts");
|
|
}
|
|
|
|
private void fixWebs(SPWeb web)
|
|
{
|
|
this.fixWeb(web);
|
|
if (web.Webs.Count > 0)
|
|
{
|
|
this.notify(LogLevel.Info, "processing subwebs of '{0}'", web.ServerRelativeUrl);
|
|
}
|
|
foreach (SPWeb subWeb in web.Webs)
|
|
{
|
|
if (!IsCondo(subWeb))
|
|
{
|
|
this.fixWebs(subWeb);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void fixWeb(SPWeb web)
|
|
{
|
|
this.notify(LogLevel.Info, "start fixWeb for '{0}'", web.ServerRelativeUrl);
|
|
var pages = SPUtility.GetLocalizedString("$Resources:List_Pages_UrlName", "cmscore", web.RegionalSettings.LocaleId);
|
|
if (!web.GetFolder(pages).Exists)
|
|
{
|
|
this.error("site '{0}' has no pages ('{1}') library, site pages will not be updated", web.Url, pages);
|
|
return;
|
|
}
|
|
|
|
bool oldAllowUnsafe = web.AllowUnsafeUpdates;
|
|
web.AllowUnsafeUpdates = true;
|
|
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
var layout = pweb.GetAvailablePageLayouts().FirstOrDefault(l => string.Compare(l.Name, "taloyhtio_responsive_layout.aspx", true) == 0);
|
|
var layoutWide = pweb.GetAvailablePageLayouts().FirstOrDefault(l => string.Compare(l.Name, "taloyhtio_responsive_layout_wide.aspx", true) == 0);
|
|
this.notify(LogLevel.Info, "layout available: {0}", layout != null);
|
|
this.notify(LogLevel.Info, "wide layout available: {0}", layoutWide != null);
|
|
foreach (var page in pweb.GetPublishingPages())
|
|
{
|
|
this.notify(LogLevel.Info, "processing page '{0}'", page.Name);
|
|
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}', page layout will not be updated", page.Name);
|
|
continue;
|
|
}
|
|
|
|
if (page.Layout == null)
|
|
{
|
|
this.warn("page '{0}' has no layout! apply wide layout", page.Name);
|
|
}
|
|
else
|
|
{
|
|
this.notify(LogLevel.Info, "page '{0}' has layout '{1}'", page.Name, page.Layout.Name);
|
|
}
|
|
|
|
if (page.Layout != null && page.Layout.Name.ToLower().Equals("taloyhtiolayout.aspx"))
|
|
{
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
page.Layout = layout;
|
|
page.Update();
|
|
file.Update();
|
|
file.CheckIn("set page layout");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("set page layout");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("set page layout");
|
|
}
|
|
}
|
|
else if (page.Layout == null || page.Layout.Name.ToLower().Equals("taloyhtiolayoutwide.aspx"))
|
|
{
|
|
this.notify(LogLevel.Info, "file checkout type: {0}", file.CheckOutType.ToString());
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
page.Layout = layoutWide;
|
|
page.Update();
|
|
file.Update();
|
|
file.CheckIn("set page layout");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("set page layout");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("set page layout");
|
|
}
|
|
}
|
|
}
|
|
//pweb.SetDefaultPageLayout(layout, true);
|
|
//pweb.Update();
|
|
this.notify(LogLevel.Info, "end fixWeb for '{0}'", web.ServerRelativeUrl);
|
|
}
|
|
|
|
public static bool IsCondo(SPWeb web)
|
|
{
|
|
try
|
|
{
|
|
if (web == null)
|
|
{
|
|
return false;
|
|
}
|
|
return web.Title.ToLower().StartsWith(CONDO_PREFIX) ||
|
|
(string.Compare(web.WebTemplate, CONDO_WEB_TEMPLATE_NAME, true) == 0 && web.WebTemplateId == CONDO_SITE_WEB_TEMPLATE_ID) && web.Configuration == CONDO_WEB_CONFIGURATION;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#region notifier
|
|
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
|
|
}
|
|
}
|