210 lines
7.6 KiB
C#
210 lines
7.6 KiB
C#
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.AsumisenPankki
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private readonly Guid CONDO_AUTOMATION_ASUMISEN_OPPAAT_FEATURE_ID = new Guid("2b106f86-7017-4641-b8b9-85b7ea131e24");
|
|
private readonly Guid CONDO_AUTOMATION_ASUMISEN_PANKKI_FEATURE_ID = new Guid("84fb0469-2139-41aa-968d-29606bff961f");
|
|
private readonly Guid ASUMISEN_PANKKI_CLEANUP_FEATURE_ID = new Guid("e9d2a4ea-df84-4b55-aebc-b1ff492edc05");
|
|
private readonly Guid ASUMISEN_PANKKI_FEATURE_ID = new Guid("6d44d109-1726-4b8e-89ab-670222e7d81d");
|
|
|
|
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.OpenWeb())
|
|
{
|
|
if (!web.Exists)
|
|
{
|
|
this.warn("Condo site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
|
|
// at first remove Condo automation - Asumisen oppaat feature if it is activated. After that
|
|
// condo won't have Assumisen oppaat item in global navigation (see CondoAutomation > topnavigation.js)
|
|
if (web.Features.Any(f => f.DefinitionId.Equals(CONDO_AUTOMATION_ASUMISEN_OPPAAT_FEATURE_ID)))
|
|
{
|
|
web.Features.Remove(CONDO_AUTOMATION_ASUMISEN_OPPAAT_FEATURE_ID);
|
|
}
|
|
|
|
// then remove Asumisen oppaat web part if it exists
|
|
this.removeAsumisenOppaatWebpart(web);
|
|
|
|
// activate cleanup feature which will delete asumisentietopankki.aspx. As result it won't have multiple web parts
|
|
// after activation of Asumisen pankki feature which will recreate the page
|
|
web.Features.Add(ASUMISEN_PANKKI_CLEANUP_FEATURE_ID, true);
|
|
// add asumisentietopankki.aspx page
|
|
web.Features.Add(ASUMISEN_PANKKI_FEATURE_ID, true);
|
|
// add Asumisen pankki web part and Lisapalvelut plugin
|
|
web.Features.Add(CONDO_AUTOMATION_ASUMISEN_PANKKI_FEATURE_ID, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void removeAsumisenOppaatWebpart(SPWeb web)
|
|
{
|
|
//this.info("1");
|
|
if (!PublishingWeb.IsPublishingWeb(web))
|
|
{
|
|
//this.info("2");
|
|
//LogUtils.LogError("Target web is not a publishing web");
|
|
return;
|
|
}
|
|
//this.info("3");
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
//this.info("4");
|
|
var file = pweb.DefaultPage;
|
|
if (file == null)
|
|
{
|
|
//this.info("5");
|
|
//LogUtils.LogError("Start page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
//this.info("6");
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
//this.info("7");
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
//this.info("8");
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
//this.info("9");
|
|
webPartManager = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
|
|
if (webPartManager == null)
|
|
{
|
|
//this.info("10");
|
|
//LogUtils.LogError("Web part manager is null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
//this.info("11");
|
|
bool webPartFound = false;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
//this.info("12 " + wp.GetType().FullName);
|
|
if (wp.GetType().FullName == "Taloyhtio.CondoAutomation.AsumisenOppaatWebPart")
|
|
{
|
|
//this.info("13");
|
|
webPartFound = true;
|
|
webPartManager.DeleteWebPart(wp);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
////this.info("14");
|
|
if (!webPartFound)
|
|
{
|
|
//this.info("15");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
//this.info("16");
|
|
file.CheckIn("AsumisenOppaatWebPart is deleted");
|
|
file.Update();
|
|
file.Publish("AsumisenOppaatWebPart is deleted");
|
|
web.Update();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//this.info("17 " + ex.Message);
|
|
//LogUtils.LogError("An error occured while trying to delete web part to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message, ex.StackTrace);
|
|
var file = pweb.DefaultPage;
|
|
if (file.CheckOutType != SPFile.SPCheckOutType.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (webPartManager != null)
|
|
{
|
|
if (webPartManager.Web != null)
|
|
{
|
|
webPartManager.Web.Dispose();
|
|
}
|
|
webPartManager.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
#region notifier
|
|
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
|
|
}
|
|
}
|