using Microsoft.SharePoint; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.WebPartPages; using System; using System.IO; using System.Reflection; using System.Text.RegularExpressions; using System.Web.UI.WebControls.WebParts; using Taloyhtio.CondoUpdate.Common; using WebPart = System.Web.UI.WebControls.WebParts.WebPart; namespace CondoUpdate.ResponsiveLayout.UpdateNewsFromPmcWebPart { public class UpdaterImpl : ICondoUpdater { public event EventHandler OnNotify; private const string TITLE_OLD = "Isännöitsijä tiedottaa - Asukastiedotteet"; private const string TITLE_OLD2 = "Isännöitsijätoimisto tiedottaa - Asukastiedotteet"; private const string TITLE_NEW = "Isännöitsijä tiedottaa"; private const string XSL_FILE = "template.xsl"; private string xslStyleSheet = null; 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) { var assembly = Assembly.GetExecutingAssembly(); var resourceName = "CondoUpdate.ResponsiveLayout.UpdateNewsFromPmcWebPart.template.xslt"; using (var stream = assembly.GetManifestResourceStream(resourceName)) { using (var reader = new StreamReader(stream)) { xslStyleSheet = reader.ReadToEnd(); } } using (var site = new SPSite(url)) { using (var condoWeb = site.OpenWeb()) { if (!condoWeb.Exists || string.Compare(condoWeb.Url, url, true) != 0) { this.warn("Site '{0}' doesn't exist. It will be ignored", url); return; } if (!PublishingWeb.IsPublishingWeb(condoWeb)) { this.warn("Site '{0}' is not publishing web. It will be ignored", url); return; } var pweb = PublishingWeb.GetPublishingWeb(condoWeb); xslStyleSheet = xslStyleSheet.Replace("~Site", condoWeb.ServerRelativeUrl); SPLimitedWebPartManager webPartManager = null; 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(); } webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); if (webPartManager == null) { this.error("Web part manager is null. Site won't be updated"); file.UndoCheckOut(); return; } bool changed = false; var webParts = webPartManager.WebParts; if (webParts != null) { foreach (WebPart wp in webParts) { if (wp.Title.Equals(TITLE_OLD) || wp.Title.Equals(TITLE_OLD2)) { wp.Title = TITLE_NEW; Regex regex = new Regex("NewsList=(?([a-f0-9-]+))\"", RegexOptions.IgnoreCase); Match match = regex.Match(((DataFormWebPart)wp).Xsl); if (match.Success) { string listId = match.Groups["Id"].Value; xslStyleSheet = xslStyleSheet.Replace("{ListId}", listId); ((DataFormWebPart)wp).Xsl = xslStyleSheet; } webPartManager.SaveChanges(wp); changed = true; break; } } } if (!changed) { file.UndoCheckOut(); return; } file.Update(); file.CheckIn("Updated 'news from pmc' webpart"); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Updated 'news from pmc' webpart"); } if (file.Item.ParentList.EnableModeration) { file.Approve("Updated 'news from pmc' webpart"); } } 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(); } } finally { if (webPartManager != null) { if (webPartManager.Web != null) { webPartManager.Web.Dispose(); } webPartManager.Dispose(); } } } } } #region notifier 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 }); } } #endregion } }