using System; using System.Globalization; using System.Linq; using System.Threading; using Microsoft.SharePoint; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Utilities; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ResponsiveLayout.EnableDocLibsPmc { public class UpdaterImpl : ICondoUpdater { //private const string ListTitle1 = "Asiakirja-arkisto"; private const string ListTitle2 = "Dokumentit"; private const string ListTitle3 = "Asiakirjat"; 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) { updatePmcDocLibs(url); } private void updatePmcDocLibs(string url) { try { using (var site = new SPSite(url)) { using (var pmcWeb = site.RootWeb) { if (!pmcWeb.Exists) { this.warn("Site '{0}' doesn't exist. It will be ignored", url); return; } if (!PublishingWeb.IsPublishingWeb(pmcWeb)) { this.warn("Web '{0}' is not publishing. It will be ignored", url); return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)pmcWeb.Language); bool allowUnsafeUpdates = pmcWeb.AllowUnsafeUpdates; pmcWeb.AllowUnsafeUpdates = true; try { var list = pmcWeb.Lists.Cast().FirstOrDefault(l => string.Equals(l.Title, ListTitle3, StringComparison.InvariantCultureIgnoreCase)); if (list != null) { if (!list.OnQuickLaunch) { list.OnQuickLaunch = true; list.Update(); } else { if (list.OnQuickLaunch) { this.warn("List '{0}' is included in Quick Launch. It will be ignored", ListTitle3); } } } var list2 = pmcWeb.Lists.Cast().FirstOrDefault(l => string.Equals(l.Title, ListTitle2, StringComparison.InvariantCultureIgnoreCase)); if (list2 != null && list2.OnQuickLaunch) { this.warn("List '{0}' is included in Quick Launch. It will be ignored", ListTitle2); } if (list2 != null) { list2.OnQuickLaunch = true; list2.Update(); } //updatePalstaDocLib(pmcWeb.Url); } catch (Exception ex) { error("error occured when tring to get web error: {0} {1}", ex.Message, ex.StackTrace); } finally { pmcWeb.AllowUnsafeUpdates = allowUnsafeUpdates; } } } } catch (Exception ex) { error("error occured when tring to get web error: {0} {1}", ex.Message, ex.StackTrace); } } /*private void updatePalstaDocLib(string url) { try { string palstaUrl = SPUrlUtility.CombineUrl(url, "lomakkeet"); using (var site = new SPSite(palstaUrl)) { using (var palstaWeb = site.OpenWeb()) { if (!palstaWeb.Exists) { this.warn("Site '{0}' doesn't exist. It will be ignored", url); return; } if (!PublishingWeb.IsPublishingWeb(palstaWeb)) { this.warn("Web '{0}' is not publishing. It will be ignored", url); return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)palstaWeb.Language); bool allowUnsafeUpdates = palstaWeb.AllowUnsafeUpdates; palstaWeb.AllowUnsafeUpdates = true; try { var list = palstaWeb.Lists.Cast().FirstOrDefault(l => string.Equals(l.Title, ListTitle3, StringComparison.InvariantCultureIgnoreCase)); if (list == null) return; if (list.OnQuickLaunch) { this.warn("List '{0}' is included in Quick Launch. It will be ignored", ListTitle3); return; } list.OnQuickLaunch = true; list.Update(); } catch (Exception ex) { error("error occured when tring to get lomakkeet web error: {0} {1}", ex.Message, ex.StackTrace); } finally { palstaWeb.AllowUnsafeUpdates = allowUnsafeUpdates; } } } } catch (Exception ex) { error("error occured when tring to get web error: {0} {1}", ex.Message, ex.StackTrace); } }*/ 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 }); } } } }