199 lines
8.0 KiB
C#
199 lines
8.0 KiB
C#
using System;
|
||
using System.Globalization;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Reflection;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Web.UI.WebControls.WebParts;
|
||
using Microsoft.SharePoint;
|
||
using Microsoft.SharePoint.Publishing;
|
||
using Microsoft.SharePoint.Utilities;
|
||
using Microsoft.SharePoint.WebPartPages;
|
||
using Taloyhtio.CondoUpdate.Common;
|
||
using Taloyhtio.WhatsNewWebpart;
|
||
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
||
|
||
namespace CondoUpdate.ResponsiveLayout.CondoSiteConfigureWebParts
|
||
{
|
||
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)
|
||
{
|
||
configureWebParts(url);
|
||
}
|
||
|
||
private void configureWebParts(string url)
|
||
{
|
||
PublishingWeb pweb = null;
|
||
try
|
||
{
|
||
SPSecurity.RunWithElevatedPrivileges(() =>
|
||
{
|
||
using (var site = new SPSite(url))
|
||
{
|
||
using (var web = site.OpenWeb())
|
||
{
|
||
if (web == null)
|
||
{
|
||
return;
|
||
}
|
||
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language);
|
||
bool condoAllowUnsafeUpdates = web.AllowUnsafeUpdates;
|
||
web.AllowUnsafeUpdates = true;
|
||
|
||
pweb = PublishingWeb.GetPublishingWeb(web);
|
||
if (pweb == null || pweb.DefaultPage == null ||
|
||
string.IsNullOrEmpty(pweb.DefaultPage.ServerRelativeUrl))
|
||
{
|
||
this.warn("Publishing web of '{0}' is null. It will be ignored", url);
|
||
return;
|
||
}
|
||
|
||
var file = pweb.DefaultPage;
|
||
if (file == null)
|
||
{
|
||
this.error("Start page is null. Site won't be updated");
|
||
return;
|
||
}
|
||
|
||
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
||
{
|
||
file.CheckOut();
|
||
}
|
||
else
|
||
{
|
||
file.UndoCheckOut();
|
||
file.CheckOut();
|
||
}
|
||
|
||
using (var manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
|
||
{
|
||
if (manager == null)
|
||
{
|
||
file.UndoCheckOut();
|
||
this.error("Web part manager is null. Site won't be updated");
|
||
return;
|
||
}
|
||
|
||
if (manager.WebParts != null)
|
||
{
|
||
if (!manager.WebParts.Cast<WebPart>()
|
||
.Any(wp => wp.Title.ToLower().Equals("uusimmat päivitykset")) &&
|
||
!manager.WebParts.Cast<WebPart>()
|
||
.Any(wp => wp.Title.ToLower().Equals("ilmoitustaulu")) &&
|
||
!manager.WebParts.Cast<WebPart>()
|
||
.Any(wp => wp.Title.ToLower().Equals("isännöitsijä tiedottaa – asukastiedotteet"))
|
||
)
|
||
{
|
||
file.UndoCheckOut();
|
||
this.error("Web parts are already configured. Site won't be updated");
|
||
return;
|
||
}
|
||
foreach (WebPart wp in manager.WebParts)
|
||
{
|
||
if (wp.Title.ToLower().Equals("uusimmat päivitykset"))
|
||
{
|
||
wp.Title = "Uutta palvelussa";
|
||
var whatsNew = wp as WhatsNewWebpart;
|
||
if (whatsNew != null)
|
||
{
|
||
whatsNew.MaxItems = 15;
|
||
whatsNew.ShowTime = false;
|
||
}
|
||
manager.SaveChanges(wp);
|
||
}
|
||
|
||
if (wp.Title.ToLower().Equals("ilmoitustaulu"))
|
||
{
|
||
wp.Title = "Talon Ilmoitustaulu";
|
||
manager.SaveChanges(wp);
|
||
}
|
||
|
||
if (wp.Title.ToLower().Equals("isännöitsijä tiedottaa – asukastiedotteet"))
|
||
{
|
||
wp.Title = "Isännöitsijä tiedottaa";
|
||
manager.SaveChanges(wp);
|
||
}
|
||
|
||
}
|
||
}
|
||
file.Update();
|
||
file.CheckIn("Updated Condo site webparts");
|
||
if (file.Item.ParentList.EnableMinorVersions)
|
||
{
|
||
file.Publish("Updated Condo site webparts");
|
||
}
|
||
if (file.Item.ParentList.EnableModeration)
|
||
{
|
||
file.Approve("Updated Condo site webparts");
|
||
}
|
||
}
|
||
web.AllowUnsafeUpdates = condoAllowUnsafeUpdates;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (pweb != null)
|
||
{
|
||
this.error("Error occured when update web part on '{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();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
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 });
|
||
}
|
||
}
|
||
}
|
||
}
|