211 lines
8.6 KiB
C#
211 lines
8.6 KiB
C#
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.OsakkailleConfigureWebParts
|
|
{
|
|
public class UpdaterImpl:ICondoUpdater
|
|
{
|
|
public event EventHandler<LogEventArgs> 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)
|
|
{
|
|
configureWebParts(url);
|
|
ChangeGlobalNavigation(url);
|
|
}
|
|
|
|
private void ChangeGlobalNavigation(string url)
|
|
{
|
|
try
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
if (web == null) return;
|
|
|
|
if (!web.Exists)
|
|
{
|
|
return;
|
|
}
|
|
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
|
|
web.AllowUnsafeUpdates = true;
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language);
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
var globalNavigation = pweb.Navigation.GlobalNavigationNodes;
|
|
|
|
var osakkailleNode =
|
|
globalNavigation.Cast<SPNavigationNode>()
|
|
.FirstOrDefault(p => p.Title.StartsWith("Osakkaille"));
|
|
var asukkailleNode =
|
|
globalNavigation.Cast<SPNavigationNode>()
|
|
.FirstOrDefault(p => p.Title.StartsWith("Asukkaille"));
|
|
if (osakkailleNode == null)
|
|
{
|
|
var node = SPNavigationSiteMapNode.CreateSPNavigationNode("Osakkaille",
|
|
SPUrlUtility.CombineUrl(web.Url, "Osakkaille"), 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();
|
|
if (asukkailleNode != null)
|
|
node.Move(globalNavigation, asukkailleNode);
|
|
}
|
|
else
|
|
{
|
|
this.warn("'{0}' navigation node already exists", "Osakkaille");
|
|
if (asukkailleNode != null)
|
|
osakkailleNode.Move(globalNavigation, asukkailleNode);
|
|
}
|
|
|
|
web.Update();
|
|
web.AllowUnsafeUpdates = allowUnsafeUpdates;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when update condo global navigation on {0} error: {1} {2}", url, x.Message,
|
|
x.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void configureWebParts(string url)
|
|
{
|
|
try
|
|
{
|
|
SPSecurity.RunWithElevatedPrivileges(() =>
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
if (web == null)
|
|
{
|
|
return;
|
|
}
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language);
|
|
bool condoAllowUnsafeUpdates = web.AllowUnsafeUpdates;
|
|
web.AllowUnsafeUpdates = true;
|
|
SPWeb landlordWeb;
|
|
try
|
|
{
|
|
landlordWeb = web.Webs.FirstOrDefault(w => w.Title.StartsWith("Osakkaille"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error("error occured when tring to get Osakkaille web error: {0} {1}", ex.Message,
|
|
ex.StackTrace);
|
|
return;
|
|
}
|
|
|
|
if (landlordWeb == null)
|
|
{
|
|
this.warn("Site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
if (!PublishingWeb.IsPublishingWeb(landlordWeb))
|
|
{
|
|
this.warn("Site '{0}' is not publishing web. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
bool allowUnsafeUpdates = landlordWeb.AllowUnsafeUpdates;
|
|
try
|
|
{
|
|
landlordWeb.AllowUnsafeUpdates = true;
|
|
var pweb = PublishingWeb.GetPublishingWeb(landlordWeb);
|
|
if (pweb == null || pweb.DefaultPage == null ||
|
|
string.IsNullOrEmpty(pweb.DefaultPage.ServerRelativeUrl))
|
|
{
|
|
this.warn("Publishing web of '{0}' is null. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
string handlerUrl = SPUrlUtility.CombineUrl(landlordWeb.Url,
|
|
"_layouts/15/Taloyhtio/CondoAutomation/LandlordsWebPartUpdateHandler.ashx");
|
|
string urlWithQuery = string.Format("{0}?condo={1}", handlerUrl, landlordWeb.ServerRelativeUrl);
|
|
var request = (HttpWebRequest)WebRequest.Create(urlWithQuery);
|
|
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 during updating of Condo :\n{0}\n{1}", ex.Message,
|
|
ex.StackTrace);
|
|
}
|
|
finally
|
|
{
|
|
landlordWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
|
|
web.AllowUnsafeUpdates = condoAllowUnsafeUpdates;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured during updating of Condo :\n{0}\n{1}", ex.Message, ex.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 });
|
|
}
|
|
}
|
|
}
|
|
}
|