using System; using System.Globalization; using System.IO; using System.Linq; using System.Threading; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.UpdateResponsiveTaloytioCss { public class UpdaterImpl:ICondoUpdater { private readonly Guid RESPONSIVE_LAYOUT_FEATURE_ID = new Guid("b45babd2-5e1c-4d9d-ac53-5ae87d7ea69d"); 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) { try { using (var site = new SPSite(url)) { var web = site.RootWeb; if (!web.Exists) { this.warn("Web site '{0}' doesn't exist. It will be ignored", url); return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language); if (!web.Site.Features.Any(f => f.DefinitionId.Equals(RESPONSIVE_LAYOUT_FEATURE_ID))) { this.info("ResponsiveLayout feature is not activated on PMC '{0}', ignore", web.Url); return; } //web.Site.Features.Add(RESPONSIVE_LAYOUT_FEATURE_ID, true); //updateFile(web, "/css/taloyhtio.css"); updateFile(web, "/js/taloyhtio.js"); } } catch (Exception ex) { this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace); } } private bool updateFile(SPWeb web, string path) { string fileUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, "/Style Library/taloyhtio_responsive" + path); var file = web.GetFile(fileUrl); if (!file.Exists) { this.warn("File '{0}' not found", fileUrl); return true; } string filePath = SPUtility.GetCurrentGenericSetupPath("Template/Features/ResponsiveLayout_Taloyhtio.ResponsiveLayout/ResponsiveDesignFiles" + path); if (!File.Exists(filePath)) { this.warn("File '{0}' not found", filePath); return true; } if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None) { file.UndoCheckOut(); } file.CheckOut(); var folder = file.ParentFolder; using (var fs = File.OpenRead(filePath)) { folder.Files.Add(file.Name, fs, true); } file.CheckIn("Responsive files update"); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Responsive files update"); } if (file.Item.ParentList.EnableModeration) { file.Approve("Responsive files update"); } return false; } 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 }); } } } }