using System; using System.Globalization; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Web.UI.WebControls.WebParts; using System.Xml; using Microsoft.SharePoint; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.WebPartPages; using Taloyhtio.CondoUpdate.Common; using WebPart = System.Web.UI.WebControls.WebParts.WebPart; namespace CondoUpdate.CentralizedNewsForOwnersOrderWebParts { 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(SPUrlUtility.CombineUrl(url, "Osakkaille")); } 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 web = site.OpenWeb()) { if (!web.Exists) { return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language); if (!PublishingWeb.IsPublishingWeb(web)) { return; } var psite = new PublishingSite(site); var pweb = PublishingWeb.GetPublishingWeb(web); var list = pweb.PagesList; foreach (SPListItem item in list.Items) { string fileUrl = item.File.Url.ToLower(); if (fileUrl.EndsWith("/default.aspx")) { this.moveWebParts(pweb, item.File); } } } } } private void moveWebParts(PublishingWeb pweb, SPFile file) { SPLimitedWebPartManager webPartManager = null; try { //this.info("Get start page"); if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None) { //this.info("Checkout start page"); 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 //this.info("Start page is already checked out. Undo previous check out and check out it again"); file.UndoCheckOut(); file.CheckOut(); } //this.info("Get web part manager"); webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); if (webPartManager == null) { this.error("Web part manager is null. Site won't be updated"); file.UndoCheckOut(); return; } //this.info("Search for DataFormWebPart with title '{0}' on the start page", webPartTitle); WebPart osakastiedotteetWebPart = null; WebPart tiedostotWebPart = null; WebPart osakkailleWebPart = null; var webParts = webPartManager.WebParts; if (webParts != null) { foreach (WebPart wp in webParts) { if (wp.Title == "Osakastiedotteet") { osakastiedotteetWebPart = wp; } else if (wp.Title == "Tiedostot") { tiedostotWebPart = wp; } else if (wp.Title == "Isännöitsijältä osakkaille") { osakkailleWebPart = wp; } } } if (osakastiedotteetWebPart != null) { webPartManager.MoveWebPart(osakastiedotteetWebPart, "LeftColumnZone", 0); webPartManager.SaveChanges(osakastiedotteetWebPart); } if (tiedostotWebPart != null) { webPartManager.MoveWebPart(tiedostotWebPart, "RightColumnZone", 0); webPartManager.SaveChanges(tiedostotWebPart); } if (osakkailleWebPart != null) { webPartManager.MoveWebPart(osakkailleWebPart, "LeftColumnZone", 1); webPartManager.SaveChanges(osakkailleWebPart); } //this.info("DataSourcesString property of web part '{0}' was sucessfully updated", webPartTitle); //this.info("Checkin file"); file.Update(); file.CheckIn("Reorder web parts"); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Reorder web parts"); } //this.info("Web part was sucessfully updated"); } catch (Exception x) { this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", pweb.Web.Url, x.Message, x.StackTrace); if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None) { file.UndoCheckOut(); } } finally { if (webPartManager != null) { if (webPartManager.Web != null) { webPartManager.Web.Dispose(); } webPartManager.Dispose(); } } } 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 }); } } } }