309 lines
11 KiB
C#
309 lines
11 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using System.Xml;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Administration;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = Microsoft.SharePoint.WebPartPages.WebPart;
|
|
|
|
namespace CondoUpdate.AddAsuParemminPage
|
|
{
|
|
public class UpdaterImpl:ICondoUpdater
|
|
{
|
|
private readonly Guid WEB_FEATURE_ID = new Guid("8a5a94a6-5ac7-4088-b068-4f1dc9cf20f3");
|
|
private const string COMMON_PAGE_URL = "CommonIframePage.aspx";
|
|
private const string ASU_PAGE_URL = "AsuParemmin.aspx";
|
|
private const string ASU_PAGE_TITLE = "Asu paremmin";
|
|
private const string LISAPALVELUT_LIST_TITLE = "Lisäpalvelut";
|
|
|
|
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)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new CultureInfo((int)web.Language);
|
|
|
|
// activate feature first
|
|
this.ensureFeatureActivated(web);
|
|
|
|
this.renamePage(web);
|
|
|
|
this.configureWebPart(web);
|
|
|
|
this.addPageToLisapalvelut(web);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void addPageToLisapalvelut(SPWeb web)
|
|
{
|
|
// use fba url for link
|
|
using (var fbaSite = new SPSite(web.Site.ID, SPUrlZone.Extranet))
|
|
{
|
|
using (var fbaWeb = fbaSite.OpenWeb(web.ID))
|
|
{
|
|
var pweb = PublishingWeb.GetPublishingWeb(fbaWeb);
|
|
var page = pweb.GetPublishingPages().FirstOrDefault(p => string.Compare(p.Name, ASU_PAGE_URL, true) == 0);
|
|
if (page != null)
|
|
{
|
|
var list = fbaWeb.Lists.Cast<SPList>().FirstOrDefault(l => l.Title.Equals(LISAPALVELUT_LIST_TITLE));
|
|
if (list == null)
|
|
{
|
|
warn("list '{0}' does not exist on site '{1}'.", LISAPALVELUT_LIST_TITLE, fbaWeb.Url);
|
|
return;
|
|
}
|
|
|
|
if (list.Items.Cast<SPListItem>().Any(i => string.Compare(i.Title, ASU_PAGE_TITLE, true) == 0))
|
|
{
|
|
warn("lisapalvelut already contains '{0}' on site '{1}'", ASU_PAGE_TITLE, fbaWeb.Url);
|
|
return;
|
|
}
|
|
|
|
var item = list.Items.Add();
|
|
item[SPBuiltInFieldId.Title] = ASU_PAGE_TITLE;
|
|
item["Url"] = SPUrlUtility.CombineUrl(fbaWeb.Url, page.Url);
|
|
item.Update();
|
|
//list.Update();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void configureWebPart(SPWeb web)
|
|
{
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
SPFile file = null;
|
|
try
|
|
{
|
|
//this.info("Get start page");
|
|
var item = pweb.PagesList.Items.Cast<SPListItem>().FirstOrDefault(
|
|
i =>
|
|
{
|
|
return (i.File != null && string.Compare(i.File.Name, ASU_PAGE_URL, true) == 0);
|
|
});
|
|
if (item == null)
|
|
{
|
|
this.error("Page '{0}' not found on web '{1}'", ASU_PAGE_URL, pweb.Url);
|
|
return;
|
|
}
|
|
file = item.File;
|
|
if (file == null)
|
|
{
|
|
this.error("Page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
|
|
{
|
|
//this.info("Checkout start page");
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
// if file was checked out by another user, need undo check out and check in again in order to avoid errors
|
|
// when add web part
|
|
//this.info("Start page is already checked out. Undo previous check out and check out it again");
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
//this.info("Get web part manager");
|
|
webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
|
|
if (webPartManager == null)
|
|
{
|
|
this.error("Web part manager is null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
PageViewerWebPart pageViewerWebPart = null;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
if (wp.GetType().FullName == "Microsoft.SharePoint.WebPartPages.PageViewerWebPart")
|
|
{
|
|
pageViewerWebPart = wp as PageViewerWebPart;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (pageViewerWebPart != null)
|
|
{
|
|
pageViewerWebPart.ContentLink = "https://www.kiinteistomedia.fi/tuotelinkki?tuote=106";
|
|
webPartManager.SaveChanges(pageViewerWebPart);
|
|
}
|
|
|
|
//this.info("Checkin file");
|
|
file.Update();
|
|
file.CheckIn("Update web part");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Update web part");
|
|
}
|
|
//this.info("Web part was sucessfully updated");
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", pweb.Web.Url,
|
|
x.Message, x.StackTrace);
|
|
if (file != null && file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (webPartManager != null)
|
|
{
|
|
if (webPartManager.Web != null)
|
|
{
|
|
webPartManager.Web.Dispose();
|
|
}
|
|
webPartManager.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void renamePage(SPWeb web)
|
|
{
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
SPFile file = null;
|
|
try
|
|
{
|
|
//this.info("Get start page");
|
|
var item = pweb.PagesList.Items.Cast<SPListItem>().FirstOrDefault(
|
|
i =>
|
|
{
|
|
return (i.File != null && string.Compare(i.File.Name, COMMON_PAGE_URL, true) == 0);
|
|
});
|
|
if (item == null)
|
|
{
|
|
this.error("Page '{0}' not found on web '{1}'", COMMON_PAGE_URL, pweb.Url);
|
|
return;
|
|
}
|
|
file = item.File;
|
|
if (file == null)
|
|
{
|
|
this.error("Page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
|
|
{
|
|
//this.info("Checkout start page");
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
// if file was checked out by another user, need undo check out and check in again in order to avoid errors
|
|
// when add web part
|
|
//this.info("Start page is already checked out. Undo previous check out and check out it again");
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
item[SPBuiltInFieldId.FileLeafRef] = ASU_PAGE_URL;
|
|
item[SPBuiltInFieldId.Title] = ASU_PAGE_TITLE;
|
|
item.Update();
|
|
|
|
file = item.File;
|
|
//this.info("Checkin file");
|
|
//file.Update();
|
|
file.CheckIn("Update web part");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Update web part");
|
|
}
|
|
//this.info("Web part was sucessfully updated");
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when rename page on '{0}' site:\n{1}\n{2}", pweb.Web.Url,
|
|
x.Message, x.StackTrace);
|
|
if (file != null && file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ensureFeatureActivated(SPWeb web)
|
|
{
|
|
if (!web.Features.Cast<SPFeature>().Any(f => f.DefinitionId == WEB_FEATURE_ID))
|
|
{
|
|
web.Features.Add(WEB_FEATURE_ID);
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|