using System; using System.Globalization; 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 = System.Web.UI.WebControls.WebParts.WebPart; using Taloyhtio.WhatsNewWebpart; namespace CondoUpdate.ResponsiveLayout.ChangeFronPageWebPartsAc { public class UpdaterImpl:ICondoUpdater { public event EventHandler 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 web = site.RootWeb) { if (web == null) { return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language); 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) { //this.info("Check web part '{0}' (zone '{1}', zone index '{2}')", wp.Title, webPartManager.GetZoneID(wp), wp.ZoneIndex); if (wp.Title.ToLower().Equals("omat asiakkaat") && (wp.ZoneIndex != 0 || !webPartManager.GetZoneID(wp).ToLower().Equals("leftcolumnzone"))) { //this.info("1"); webPartManager.MoveWebPart(wp, "LeftColumnZone", 0); webPartManager.SaveChanges(wp); } if (wp.Title.ToLower().Equals("omat tehtävät") && (wp.ZoneIndex != 1 || !webPartManager.GetZoneID(wp).ToLower().Equals("leftcolumnzone"))) { //this.info("2"); webPartManager.MoveWebPart(wp, "LeftColumnZone", 1); webPartManager.SaveChanges(wp); } if (wp.Title.ToLower().Equals("toimiston sisäiset") && (wp.ZoneIndex != 2 || !webPartManager.GetZoneID(wp).ToLower().Equals("leftcolumnzone"))) { //this.info("3"); webPartManager.MoveWebPart(wp, "LeftColumnZone", 2); webPartManager.SaveChanges(wp); } if (wp.Title.ToLower().Equals("asiakastiedotteet") || wp.Title.ToLower().Equals("kirjanpitäjältä asiakkaille")) { //this.info("4"); wp.Title = "Kirjanpitäjältä asiakkaille"; if (wp.ZoneIndex != 1 || !webPartManager.GetZoneID(wp).ToLower().Equals("rightcolumnzone")) { webPartManager.MoveWebPart(wp, "RightColumnZone", 1); } webPartManager.SaveChanges(wp); } if (wp.Title.ToLower().Equals("uusimmat päivitykset")) { bool needToSave = false; if (wp.ZoneIndex != 0 || !webPartManager.GetZoneID(wp).ToLower().Equals("rightcolumnzone")) { //this.info("5"); webPartManager.MoveWebPart(wp, "RightColumnZone", 0); needToSave = true; } var whatsNew = wp as WhatsNewWebpart; if (whatsNew != null) { whatsNew.MaxItems = 15; whatsNew.ShowTime = false; needToSave = true; } if (needToSave) { webPartManager.SaveChanges(wp); } } if (wp.Title.ToLower().Equals("työkalut") && (wp.ZoneIndex != 2 || !webPartManager.GetZoneID(wp).ToLower().Equals("rightcolumnzone"))) { //this.info("6"); webPartManager.MoveWebPart(wp, "RightColumnZone", 2); webPartManager.SaveChanges(wp); } } } file.Update(); file.CheckIn("Updated ac site webparts order"); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Updated ac site webparts order"); } if (file.Item.ParentList.EnableModeration) { file.Approve("Updated ac 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 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 }); } } } }