214 lines
8.3 KiB
C#
214 lines
8.3 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 Microsoft.SharePoint.WebPartPages;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = Microsoft.SharePoint.WebPartPages.WebPart;
|
|
|
|
namespace Taloyhtio.CondoUpdate.ResponsiveLayout.HallitukselleFrontPageChangeWebPartsOrder
|
|
{
|
|
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)
|
|
{
|
|
try
|
|
{
|
|
SPSecurity.RunWithElevatedPrivileges(() =>
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var condo = site.OpenWeb())
|
|
{
|
|
if (condo == null)
|
|
{
|
|
return;
|
|
}
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)condo.Language);
|
|
|
|
var web = condo.Webs.Cast<SPWeb>().FirstOrDefault(w => w.Title.StartsWith("Hallitukselle"));
|
|
if (web == null)
|
|
{
|
|
return;
|
|
}
|
|
bool condoAllowUnsafeUpdates = web.AllowUnsafeUpdates;
|
|
web.AllowUnsafeUpdates = true;
|
|
|
|
try
|
|
{
|
|
|
|
var 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;
|
|
}
|
|
|
|
updatWebParts(pweb);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured during updating of Condo :\n{0}\n{1}", ex.Message,
|
|
ex.StackTrace);
|
|
}
|
|
finally
|
|
{
|
|
web.AllowUnsafeUpdates = condoAllowUnsafeUpdates;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured during updating of Condo :\n{0}\n{1}", ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void updatWebParts(PublishingWeb pweb)
|
|
{
|
|
try
|
|
{
|
|
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();
|
|
}
|
|
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
using (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.Title.ToLower().Equals("hallitustiedotteet") && (wp.ZoneIndex > 0 ||
|
|
!webPartManager.GetZoneID(wp).ToLower().Equals("leftcolumnzone")))
|
|
{
|
|
webPartManager.MoveWebPart(wp, "LeftColumnZone", 0);
|
|
webPartManager.SaveChanges(wp);
|
|
}
|
|
if (wp.Title.ToLower().Equals("isännöitsijältä hallituksille") && (wp.ZoneIndex != 1 ||
|
|
!webPartManager.GetZoneID(wp).ToLower().Equals("leftcolumnzone")))
|
|
{
|
|
webPartManager.MoveWebPart(wp, "LeftColumnZone", 1);
|
|
webPartManager.SaveChanges(wp);
|
|
}
|
|
if (wp.Title.ToLower().Equals("hallituskeskustelu") && (wp.ZoneIndex > 0 ||
|
|
!webPartManager.GetZoneID(wp).ToLower().Equals("rightcolumnzone")))
|
|
{
|
|
webPartManager.MoveWebPart(wp, "RightColumnZone", 0);
|
|
webPartManager.SaveChanges(wp);
|
|
}
|
|
if (wp.Title.ToLower().Equals("kalenteri") && (wp.ZoneIndex != 1 ||
|
|
!webPartManager.GetZoneID(wp).ToLower().Equals("rightcolumnzone")))
|
|
{
|
|
webPartManager.MoveWebPart(wp, "RightColumnZone", 1);
|
|
webPartManager.SaveChanges(wp);
|
|
}
|
|
if ((wp.Title.ToLower().Equals("tiedostot") || wp.Title.ToLower().Equals("asiakirjat")) && (wp.ZoneIndex != 2 ||
|
|
!webPartManager.GetZoneID(wp).ToLower().Equals("rightcolumnzone")))
|
|
{
|
|
webPartManager.MoveWebPart(wp, "RightColumnZone", 2);
|
|
webPartManager.SaveChanges(wp);
|
|
}
|
|
}
|
|
}
|
|
|
|
file.Update();
|
|
file.CheckIn("Updated condo site webparts order");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Updated condo site webparts order");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("Updated condo site webparts order");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|