278 lines
12 KiB
C#
278 lines
12 KiB
C#
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
using System.Xml;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.MigrateAsumisenTietopankki2019
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string LIST_TITLE = "Lisäpalvelut";
|
|
private const string ASUMISEN_PLUGIN_TITLE = "Asumisen tietopankki";
|
|
private const string ASUMISEN_PAGE_OLD = "asumisentietopankki.aspx";
|
|
private const string ASUMISEN_PAGE_NEW = "_layouts/15/taloyhtio/Tietopankki.aspx";
|
|
private const string FIELD_URL = "Url";
|
|
private readonly Guid TIETOPANKKI_2019_FEATURE_ID = Guid.Parse("6892b8f4-fdd0-4971-a10b-ca9ce8e1d749");
|
|
private const string TIETOPANKKI_2019_FEATURE_TITLE = "AsumisenTietopankki2019";
|
|
|
|
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 web = site.OpenWeb())
|
|
{
|
|
if (!web.Exists)
|
|
{
|
|
this.error("Web '{0}' does not exist", web.Url);
|
|
return;
|
|
}
|
|
|
|
// get lisapalvelut list
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title, LIST_TITLE, true) == 0);
|
|
if (list == null)
|
|
{
|
|
this.error("List '{0}' not found", LIST_TITLE);
|
|
return;
|
|
}
|
|
this.info("got lisapalvelut list");
|
|
|
|
// get asumisen tietopankki link
|
|
var link = list.Items.Cast<SPListItem>().FirstOrDefault(i => string.Compare(i.Title, ASUMISEN_PLUGIN_TITLE, true) == 0);
|
|
if (link == null)
|
|
{
|
|
this.error("Link to '{0}' not found", ASUMISEN_PLUGIN_TITLE);
|
|
return;
|
|
}
|
|
this.info("got asumisen tietopankki link");
|
|
|
|
var oldPage = link[FIELD_URL];
|
|
this.info("old tietopankki url: '{0}'", oldPage);
|
|
var newPage = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, ASUMISEN_PAGE_NEW);
|
|
this.info("new tietopankki url: '{0}'", newPage);
|
|
|
|
var feature = site.Features.FirstOrDefault(f => f != null && f.Definition != null && f.Definition.Id.Equals(TIETOPANKKI_2019_FEATURE_ID));
|
|
if (feature == null)
|
|
{
|
|
this.info("Feature '{0}' is not installed on site '{1}'", ASUMISEN_PLUGIN_TITLE, site.Url);
|
|
site.Features.Add(TIETOPANKKI_2019_FEATURE_ID, true);
|
|
this.info("installed");
|
|
}
|
|
else
|
|
{
|
|
this.info("Feature '{0}' already installed on site '{1}'", ASUMISEN_PLUGIN_TITLE, site.Url);
|
|
}
|
|
|
|
// remove old webpart, add new webpart
|
|
if (!PublishingWeb.IsPublishingWeb(web))
|
|
{
|
|
this.error("Target web is not a publishing web");
|
|
return;
|
|
}
|
|
this.info("Web is publishing web");
|
|
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
var file = pweb.DefaultPage;
|
|
if (file == null)
|
|
{
|
|
this.error("Start page is null.");
|
|
return;
|
|
}
|
|
this.info("got default page");
|
|
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
|
|
if (webPartManager == null)
|
|
{
|
|
this.error("Unable to get webpart manager");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
this.info("got webpart manager");
|
|
|
|
bool changed = false;
|
|
//bool found = false;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
var oldWebpart = webParts.Cast<WebPart>().FirstOrDefault(wp => wp.GetType().FullName.Equals("Taloyhtio.CondoAutomation.AsumisenPankkiWebPart"));
|
|
var newWebpart = webParts.Cast<WebPart>().FirstOrDefault(wp => wp.GetType().FullName.Equals("Taloyhtio.AsumisenTietopankki.TietopankkiWebPart.TietopankkiWebPart"));
|
|
|
|
if (oldWebpart != null)
|
|
{
|
|
this.info("deleting old webpart");
|
|
webPartManager.DeleteWebPart(oldWebpart);
|
|
changed = true;
|
|
}
|
|
else
|
|
{
|
|
this.info("can't find old webpart");
|
|
}
|
|
|
|
if (newWebpart == null)
|
|
{
|
|
|
|
//foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webParts)
|
|
//{
|
|
// this.info("webpart {0}: {1}", wp.GetType().FullName, wp.Title);
|
|
// if (wp.GetType().FullName.Equals("Taloyhtio.CondoAutomation.AsumisenPankkiWebPart"))
|
|
// {
|
|
// this.info("deleting webpart...");
|
|
// webPartManager.DeleteWebPart(wp);
|
|
// changed = true;
|
|
// }
|
|
// if (wp.GetType().FullName.Equals("Taloyhtio.AsumisenTietopankki.TietopankkiWebPart.TietopankkiWebPart"))
|
|
// {
|
|
// found = true;
|
|
// }
|
|
//}
|
|
|
|
//if (!found)
|
|
//{
|
|
var path = string.Format(@"{0}\FEATURES\{1}", SPUtility.GetVersionedGenericSetupPath("TEMPLATE", 15), @"AsumisenTietopankki_AsumisenTietopankki2019\TietopankkiWebPart\TietopankkiWebPart.webpart");
|
|
this.info("Path to web part file: {0}", path);
|
|
using (var fs = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(path))))
|
|
{
|
|
using (var reader = new XmlTextReader(fs))
|
|
{
|
|
string error;
|
|
WebPart wp = webPartManager.ImportWebPart(reader, out error);
|
|
if (wp == null)
|
|
{
|
|
this.error("Imported web part is null. Site '{0}' won't be updated. Error message: {1}", pweb.Web.Url, error);
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
webPartManager.AddWebPart(wp, "RightColumnZone", 10);
|
|
webPartManager.SaveChanges(wp);
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
link[FIELD_URL] = newPage;
|
|
link.Update();
|
|
list.Update();
|
|
}
|
|
}
|
|
|
|
if (!changed)
|
|
{
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
file.CheckIn("Asumisen Tietopankki 2019 webpart is added");
|
|
file.Update();
|
|
file.Publish("Asumisen Tietopankki 2019 webpart is added");
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("An error occured while trying to add 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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
#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
|
|
|
|
}
|
|
}
|