using Microsoft.SharePoint; using Microsoft.SharePoint.Navigation; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Publishing.Navigation; using Microsoft.SharePoint.Utilities; using System; using System.Globalization; using System.Linq; using System.Threading; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ResponsiveLayout.TilaaIlmoitaGlobalNavigationTallier { public class UpdaterImpl : ICondoUpdater { private const string NewPageTitle = "Tilaa & Ilmoita"; private const string OldPageTitle = "Tilitoimiston palsta"; private readonly Guid _webFeatureId = new Guid("2aebd2c9-b135-4681-aa59-de83aea68d1b"); 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 Company '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace); } } private void updateImpl(string url) { if (string.IsNullOrEmpty(url)) return; ChangeAcGlobalNavigation(url); ChangeCompanyGlobalNavigation(url); EnableTilaaIlmoitaList(url); } private void ChangeAcGlobalNavigation(string url) { try { ChangeGlobalNavigation(url, false); } catch (Exception x) { this.error("Error occured when update ac global navigation on {0} error: {1} {2}", url, x.Message, x.StackTrace); } } private void ChangeCompanyGlobalNavigation(string url) { try { ChangeGlobalNavigation(url, true); } catch (Exception x) { this.error("Error occured when update company global navigation on {0} error: {1} {2}", url, x.Message, x.StackTrace); } } private void ChangeGlobalNavigation(string url, bool isCompany) { this.warn("=== start tilaa and ilmoita plugin on {1} web: {0}", url, isCompany ? "company" : "ac"); using (var site = new SPSite(url)) using (var web = isCompany ? site.OpenWeb() : site.RootWeb) { try { if (web == null || !web.Exists) { this.warn("{1} web does not exist: {0}", url, isCompany ? "company" : "ac"); return; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language); var pweb = PublishingWeb.GetPublishingWeb(web); var globalNavigation = pweb.Navigation.GlobalNavigationNodes; var palstaWeb = web.Webs.Cast().FirstOrDefault(w => w.Title.StartsWith(OldPageTitle)); if (palstaWeb != null && palstaWeb.Exists) { this.warn("'{0}' web found", OldPageTitle); var publishingPalstaWeb = PublishingWeb.GetPublishingWeb(palstaWeb); publishingPalstaWeb.IncludeInGlobalNavigation = false; publishingPalstaWeb.Update(); this.warn("'{0}' web excluded", OldPageTitle); } else { this.warn("'{0}' web not found", OldPageTitle); } // if it still exists... var palstaNode = globalNavigation.Cast().FirstOrDefault(n => n.Title.StartsWith(OldPageTitle)); if (palstaNode != null) { try { this.warn("trying to delete global navigation node on {1} web: {0}", url, isCompany ? "condo" : "pmc"); globalNavigation.Delete(palstaNode); this.warn("no exceptions"); } catch (Exception e) { this.warn("got {0} exception with message: {1}", e.ToString(), e.Message); } } var tilaaIlmoitaNode = globalNavigation.Cast().FirstOrDefault(p => p.Title.Equals(NewPageTitle)); if (tilaaIlmoitaNode == null) { this.warn("'{0}' navigation node does not exist, creating new one", NewPageTitle); var node = SPNavigationSiteMapNode.CreateSPNavigationNode(NewPageTitle, SPUrlUtility.CombineUrl(site.RootWeb.Url, "lomakkeet"), NodeTypes.Heading, globalNavigation); var dt = DateTime.Now; node.Properties["CreatedDate"] = dt; node.Properties["LastModifiedDate"] = dt; node.Properties["Description"] = ""; node.Properties["Target"] = ""; node.Properties["Taloyhtio"] = "1"; node.Update(); node.MoveToLast(globalNavigation); } else { this.warn("'{0}' navigation node already exists", NewPageTitle); } } catch (Exception x) { this.error("Error occured while working with lomakkeet: {0} {1}", x.Message, x.StackTrace); } } this.warn("=== end tilaa and ilmoita plugin on {1} web: {0}\n\n", url, isCompany ? "condo" : "pmc"); } private void EnableTilaaIlmoitaList(string url) { try { using (var site = new SPSite(url)) { using (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.Features.Any(f => f.DefinitionId.Equals(_webFeatureId))) { web.Features.Add(_webFeatureId, true); } else { this.warn("Feature already activated on PMC '{0}', ignore", url); } } } } catch (Exception ex) { this.error("Error occured when enable Tilaa & Ilmoita list on {0} error: {1} {2}", url, ex.Message, ex.StackTrace); } } #region notify 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 } }