Taylohtio/CondoUpdate/CondoUpdate.ResponsiveLayou.../UpdaterImpl.cs

108 lines
3.5 KiB
C#

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Hosting;
using System.Web.UI.WebControls.WebParts;
using Taloyhtio.CondoUpdate.Common;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
namespace CondoUpdate.ResponsiveLayout.UpdatePmcAsukastiedotteetWebPart
{
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)
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (var site = new SPSite(url))
{
using (var condoWeb = site.RootWeb)
{
if (!condoWeb.Exists)
{
this.warn("Site '{0}' doesn't exist. It will be ignored", url);
return;
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)condoWeb.Language);
if (!PublishingWeb.IsPublishingWeb(condoWeb))
{
this.warn("Site '{0}' is not publishing web. It will be ignored", url);
return;
}
string handlerUrl = SPUrlUtility.CombineUrl(condoWeb.Url, "_layouts/15/Taloyhtio/CondoAutomation/PmcWebPartUpdateHandler.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");
}
}
}
});
}
#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
}
}