167 lines
5.6 KiB
C#
167 lines
5.6 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.RemoveImagesCarouselWebPart
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
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 condoWeb = site.OpenWeb())
|
|
{
|
|
if (!condoWeb.Exists)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(condoWeb))
|
|
{
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(condoWeb);
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)condoWeb.Language);
|
|
this.removeWebPart(pweb);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when remove web part from the '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void removeWebPart(PublishingWeb pweb)
|
|
{
|
|
try
|
|
{
|
|
if (pweb.DefaultPage == null)
|
|
{
|
|
this.error("Unable to find guide pages. Site won't be updated");
|
|
return;
|
|
}
|
|
var file = pweb.DefaultPage;
|
|
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.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();
|
|
}
|
|
using (var 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)
|
|
{
|
|
if (webParts.Cast<WebPart>().Any(wp => wp.Title.ToLower().Equals("kuvien karuselli")))
|
|
{
|
|
var webPart = webParts.Cast<WebPart>().First(wp => wp.Title.ToLower().Equals("kuvien karuselli"));
|
|
webPartManager.DeleteWebPart(webPart);
|
|
}
|
|
else
|
|
{
|
|
this.warn("Web part doesn't exist. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
}
|
|
|
|
file.Update();
|
|
file.CheckIn("kuvien karuselli web part is removed");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("kuvien karuselli web part is removed");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("kuvien karuselli web part is removed");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (pweb != null)
|
|
{
|
|
var file = pweb.DefaultPage;
|
|
if (file != null && file.CheckOutType != SPFile.SPCheckOutType.None)
|
|
{
|
|
this.error("Error occured when remove web part from the '{0}' site:\n{1}\n{2}", pweb.Url,
|
|
ex.Message, ex.StackTrace);
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|