using System.IO; using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.SharePoint.Utilities; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ResponsiveLayout.ReactivateControlPanel { 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) { using (var site = new SPSite(url)) { var web = site.RootWeb; Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language); var file = web.GetFile(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, "/_catalogs/masterpage/TaloyhtioSimpleLayout.aspx")); if (!file.Exists) { this.warn("File '/_catalogs/masterpage/TaloyhtioSimpleLayout.aspx' not found"); return; } if (!File.Exists(SPUtility.GetCurrentGenericSetupPath("Template/Features/ControlPanel_Taloyhtio.ControlPanel.Layouts/Taloyhtio.ControlPanel.Layouts/TaloyhtioSimpleLayout.aspx"))) { this.warn("File 'Template/Features/ControlPanel_Taloyhtio.ControlPanel.Layouts/Taloyhtio.ControlPanel.Layouts/TaloyhtioSimpleLayout.aspx' not found"); return; } if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None) { file.UndoCheckOut(); } file.CheckOut(); var folder = file.ParentFolder; using (var fs = File.OpenRead(SPUtility.GetCurrentGenericSetupPath("Template/Features/ControlPanel_Taloyhtio.ControlPanel.Layouts/Taloyhtio.ControlPanel.Layouts/TaloyhtioSimpleLayout.aspx"))) { folder.Files.Add(file.Name, fs, true); } file.CheckIn("Responsive layout"); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Responsive layout"); } if (file.Item.ParentList.EnableModeration) { file.Approve("Responsive layout"); } } } #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 } }