using System; using System.Linq; using Microsoft.SharePoint; using Microsoft.SharePoint.Navigation; using Microsoft.SharePoint.Publishing; using Taloyhtio.CondoUpdate.Common; using Microsoft.SharePoint.Publishing.Navigation; namespace CondoUpdate.ResponsiveLayout.ChangeHomeLinkInGlobalNavigationAc { public class UpdaterImpl:ICondoUpdater { private const string PageTitle = "Etusivu"; 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) { this.changeGlobalNavigation(url, PageTitle); } private void changeGlobalNavigation(string url, string pageTitle) { if (string.IsNullOrEmpty(url)) return; try { SPSecurity.RunWithElevatedPrivileges(() => { using (var site = new SPSite(url)) { using (var web = site.RootWeb) { if (!web.Exists) { return; } bool allowUnsafeUpdatesPmc = web.AllowUnsafeUpdates; web.AllowUnsafeUpdates = true; var pweb = PublishingWeb.GetPublishingWeb(web); // var defaultPage = pweb.DefaultPage; // if (defaultPage != null) // { // var page = pweb.GetPublishingPages().FirstOrDefault(p => p.Url.ToLower().EndsWith(defaultPage.Url.ToLower())); // if (page != null) // { // page.IncludeInGlobalNavigation = true; // } // } var globalNavigation = pweb.Navigation.GlobalNavigationNodes; var nodePage = globalNavigation.Cast().FirstOrDefault(n => (n.Title.ToLower().EndsWith(" oy") || n.Title.ToLower().EndsWith(" ky")) && n.IsVisible && (n.Properties != null && n.Properties["NodeType"] != null && n.Properties["NodeType"] is string && (n.Properties["NodeType"] as string == "AuthoredLink" || (n.Properties["NodeType"] as string).StartsWith("AuthoredLink")))); if (nodePage != null) { nodePage.Properties["NodeType"] = "Heading"; nodePage.Update(); pweb = PublishingWeb.GetPublishingWeb(web); globalNavigation = pweb.Navigation.GlobalNavigationNodes; nodePage = globalNavigation.Cast().FirstOrDefault(n => (n.Title.ToLower().EndsWith(" oy") || n.Title.ToLower().EndsWith(" ky")) && n.IsVisible); if (nodePage != null) { nodePage.Delete(); } else { this.warn("'{0}' Pmc name navigation item is not found", pageTitle); } } else { this.warn("'{0}' Pmc name navigation item is not found", pageTitle); } pweb = PublishingWeb.GetPublishingWeb(web); globalNavigation = pweb.Navigation.GlobalNavigationNodes; var etusivuNode = globalNavigation.Cast().FirstOrDefault(p => p.Title.Equals(pageTitle) && p.IsVisible == true); if (etusivuNode == null) { var node = SPNavigationSiteMapNode.CreateSPNavigationNode(pageTitle, web.Url, NodeTypes.Heading, globalNavigation); var dt = DateTime.Now; if (node != null) { node.Properties["CreatedDate"] = dt; node.Properties["LastModifiedDate"] = dt; node.Properties["Description"] = ""; node.Properties["Target"] = ""; node.Properties["Taloyhtio"] = "1"; node.Update(); node.MoveToFirst(globalNavigation); } } else { this.warn("'{0}' navigation node already exists", pageTitle); //etusivuNode.Delete(); } web.AllowUnsafeUpdates = allowUnsafeUpdatesPmc; } } }); } catch (Exception x) { this.error("Error occured when rename front page on {0} error: {1} {2}", url, x.Message, x.StackTrace); } } 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 }); } } } }