218 lines
7.5 KiB
C#
218 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using System.Xml;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
|
|
namespace CondoUpdate.AddImagesCarouselWebPart
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private readonly Guid FEATURE_ID = new Guid("165f7eb3-f26c-4233-895d-234cc5245dd9");
|
|
private const string PB_KEY = "CondoUpdate.AddImagesCarouselWebPart_B610046E_A3E7_4d86_9883_2948E91629D1";
|
|
private const string FOLDER_URL_CAROUSEL = "etusivukuvat";
|
|
|
|
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 condoWeb = site.OpenWeb())
|
|
{
|
|
if (!condoWeb.Exists || string.Compare(condoWeb.Url, url, true) != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(condoWeb))
|
|
{
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(condoWeb);
|
|
|
|
this.ensureFeatureActivated(site);
|
|
this.addWebPart(pweb);
|
|
this.createImagesCarouselFolderInPublishingImages(condoWeb);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void addWebPart(PublishingWeb pweb)
|
|
{
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
var file = pweb.DefaultPage;
|
|
if (file == null)
|
|
{
|
|
this.error("Start page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
|
|
{
|
|
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
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
|
|
if (webPartManager == null)
|
|
{
|
|
this.error("Web part manager is null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
if (wp.GetType().FullName == "Taloyhtio.CondoAutomation.ImageCarouselWebPart")
|
|
{
|
|
this.warn("ImageCarouselWebPart already exist on start page. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
using (var fs = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText("Taloyhtio.CondoAutomation.ImageCarouselWebpart.webpart"))))
|
|
{
|
|
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", 0);
|
|
webPartManager.SaveChanges(wp);
|
|
}
|
|
}
|
|
|
|
file.CheckIn("Images carousel web part is added");
|
|
file.Update();
|
|
file.Publish("Images carousel web part is added");
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when add web part to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, x.Message, x.StackTrace);
|
|
var file = pweb.DefaultPage;
|
|
if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (webPartManager != null)
|
|
{
|
|
if (webPartManager.Web != null)
|
|
{
|
|
webPartManager.Web.Dispose();
|
|
}
|
|
webPartManager.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ensureFeatureActivated(SPSite site)
|
|
{
|
|
var rootWeb = site.RootWeb;
|
|
if (!site.Features.Cast<SPFeature>().Any(f => f.DefinitionId == FEATURE_ID) ||
|
|
!rootWeb.AllProperties.ContainsKey(PB_KEY))
|
|
{
|
|
site.Features.Add(FEATURE_ID, true);
|
|
rootWeb.AllProperties[PB_KEY] = "1";
|
|
rootWeb.Update();
|
|
}
|
|
}
|
|
|
|
private void createImagesCarouselFolderInPublishingImages(SPWeb web)
|
|
{
|
|
try
|
|
{
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == "Kuvat");
|
|
if (list == null)
|
|
{
|
|
return;
|
|
}
|
|
var folders = list.RootFolder.SubFolders;
|
|
if (!folders.Cast<SPFolder>().Any(f => f.Name == FOLDER_URL_CAROUSEL))
|
|
{
|
|
folders.Add(FOLDER_URL_CAROUSEL);
|
|
}
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when try to create '{0}' folder in 'Kuvat' list:\n{1}\n{2}", FOLDER_URL_CAROUSEL,
|
|
x.Message, x.StackTrace);
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|