using System; using System.Globalization; using System.Linq; using System.Net; using System.Threading; using Microsoft.SharePoint; using Microsoft.SharePoint.Navigation; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Publishing.Navigation; using Microsoft.SharePoint.Utilities; using Taloyhtio.CondoUpdate.Common; namespace CondoUpdate.ResponsiveLayout.ConfigureJohdonSalkkuWebParts { public class UpdaterImpl : ICondoUpdater { private const string TargetPageUrl = "johdonsalkku.aspx"; private const string TargetTitle = "Taloyhtio.Info"; 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 { SPSecurity.RunWithElevatedPrivileges(() => { 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; } Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)pmcWeb.Language); if (!PublishingWeb.IsPublishingWeb(pmcWeb)) { this.warn("Site '{0}' is not publishing web. It will be ignored", url); return; } string handlerUrl = SPUrlUtility.CombineUrl(pmcWeb.Url, "_layouts/15/Taloyhtio/CondoAutomation/PmcJohdonSalkkuWebPartUpdateHandler.ashx"); var request = (HttpWebRequest)WebRequest.Create(handlerUrl); request.Credentials = CredentialCache.DefaultNetworkCredentials; var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { this.error("Error occured during webparts update"); } } } }); } catch (Exception ex) { this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace); } } private void addGlobalNavNode(PublishingWeb pweb, SPWeb pmcWeb) { try { var globalNavigation = pweb.Navigation.GlobalNavigationNodes; bool alreadyExists = globalNavigation.Cast().Any(spnode => spnode.Title.Equals(TargetTitle)); if (alreadyExists) { this.warn(string.Format("{0} menu item already exists. Site won't be updated", TargetTitle)); return; } var pageExists = pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages").Files.Cast().Any(f => f.Name.ToLower() == "sisäisetasiat.aspx"); if (!pageExists) { this.error("Unable to find guide pages. Site won't be updated"); return; } var file = pmcWeb.GetFolder((int)pmcWeb.Language == 1035 ? "Sivut" : "Pages") .Files.Cast() .First(f => f.Name.ToLower() == TargetPageUrl); var node = SPNavigationSiteMapNode.CreateSPNavigationNode(TargetTitle, file == null ? pmcWeb.ServerRelativeUrl : file.Url, NodeTypes.AuthoredLinkPlain, globalNavigation); var dt = DateTime.Now; node.Properties["CreatedDate"] = dt; node.Properties["LastModifiedDate"] = dt; node.Properties["Description"] = ""; node.Update(); node.MoveToLast(globalNavigation); } catch (Exception ex) { this.error("Error occured when add global menu item to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, 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 }); } } } }