using System; using System.Globalization; using System.Linq; using System.Threading; using Microsoft.SharePoint; using Microsoft.SharePoint.Navigation; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Publishing.Navigation; using Taloyhtio.CondoUpdate.Common; using Microsoft.SharePoint.Utilities; namespace CondoUpdate.ResponsiveLayout.AddPelsuPage { public class UpdaterImpl : ICondoUpdater { private const string PAGE_URL = "pelsu.aspx"; private const string PAGE_FILENAME = "Pelastussuunnitelma.aspx"; private const string PAGE_TITLE = "Pelastussuunnitelma"; private const string LIST_TITLE = "Lisäpalvelut"; private const string FIELD_URL = "Url"; private readonly Guid WEB_FEATURE_ID = new Guid("3cc89ee5-ef67-418d-a656-7c3c6925714f"); 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)) { using (var web = site.OpenWeb()) { if (!web.Exists) { return; } if (!PublishingWeb.IsPublishingWeb(web)) { return; } var pweb = PublishingWeb.GetPublishingWeb(web); Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language); #region old (before mantis #1030) //var pages = SPUtility.GetLocalizedString("$Resources:List_Pages_UrlName", "cmscore", web.RegionalSettings.LocaleId); //var pageExists = web.GetFolder(pages).Files.Cast().Any(f => string.Compare(f.Name, PAGE_URL, true) == 0); //if (pageExists) //{ // this.info("found publishing page '{0}' on condo site '{1}'.", PAGE_URL, url); // var globalNavigation = pweb.Navigation.GlobalNavigationNodes; // var node = globalNavigation.Cast().FirstOrDefault(n => string.Compare(n.Title, PAGE_TITLE, true) == 0); // if (node != null) // { // this.info(string.Format("delete '{0}' menu item on site '{1}", PAGE_TITLE, url)); // globalNavigation.Delete(node); // } //} //else //{ // web.Features.Add(WEB_FEATURE_ID, true); //} //var list = web.Lists.Cast().FirstOrDefault(l => l.Title.Equals(LIST_TITLE)); //if (list == null) //{ // this.error("list '{0}' does not exist on site '{1}'.", LIST_TITLE, web.Url); // return; //} //if (list.Items.Cast().Any(i => string.Compare(i.Title, PAGE_TITLE, true) == 0)) //{ // this.warn("lisapalvelut already contains '{0}' on site '{1}'", PAGE_TITLE, url); // return; //} //var item = list.Items.Add(); //item[SPBuiltInFieldId.Title] = PAGE_TITLE; //item[FIELD_URL] = string.Format("{0}/{1}/{2}", web.ServerRelativeUrl, pages, PAGE_URL); //item.Update(); //list.Update(); #endregion var page = pweb.GetPublishingPages().FirstOrDefault(p => string.Compare(p.Name, PAGE_FILENAME, true) == 0); if (page != null) { this.info("found publishing page '{0}' on condo site '{1}'.", PAGE_FILENAME, url); page.IncludeInGlobalNavigation = false; page.IncludeInCurrentNavigation = false; } var globalNavigation = pweb.Navigation.GlobalNavigationNodes; //this.info("=== listing nodes start ==="); //globalNavigation.Cast().ToList().ForEach(n => { this.info("name '{0}', url '{1}'", n.Title, n.Url); }); var node = globalNavigation.Cast().FirstOrDefault(n => string.Compare(n.Title, PAGE_TITLE, true) == 0); if (node != null) { this.info(string.Format("found '{0}' menu item on site '{1}", PAGE_TITLE, url)); string pageUrl = node.Url; var list = web.Lists.Cast().FirstOrDefault(l => l.Title.Equals(LIST_TITLE)); if (list == null) { this.error("list '{0}' does not exist on site '{1}'.", LIST_TITLE, web.Url); return; } try { //globalNavigation.Delete(node); node.Delete(); this.info("deleted node"); } catch (Exception e) { this.warn("unable to delete node: {0}", e.Message); } if (list.Items.Cast().Any(i => string.Compare(i.Title, PAGE_TITLE, true) == 0)) { this.warn("lisapalvelut already contains '{0}' on site '{1}'", PAGE_TITLE, url); return; } var item = list.Items.Add(); item[SPBuiltInFieldId.Title] = PAGE_TITLE; item[FIELD_URL] = pageUrl; item.Update(); list.Update(); } } } } catch (Exception ex) { this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace); } } #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 } }