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

211 lines
8.1 KiB
C#

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.ResponsiveLayout.DiscussionBoardLikesTallier
{
public class UpdaterImpl : ICondoUpdater
{
private readonly Guid WEB_FEATURE_ID = new Guid("915c240e-a6cc-49b8-8b2c-0bff8b553ed3");
public event EventHandler<LogEventArgs> OnNotify;
private const string LIST_TITLE = "Keskustelu";
private const string LIST_TITLE2 = "Keskustelu tilitoimistosi kanssa"; // for /tevilex
private const string LIST_TITLE3 = "Huomioitavaa ja kommentteja"; // for /tilitoimistoverso
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)
{
enableRatingFeature(url);
enableLikesCompanySite(url);
}
private void enableRatingFeature(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.Site.Features.Any(f => f.DefinitionId.Equals(WEB_FEATURE_ID)))
{
web.Site.Features.Add(WEB_FEATURE_ID, true);
}
else
{
this.warn("Feature already activated on AC '{0}', ignore", web.Url);
}
}
}
}
catch (Exception ex)
{
this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace);
}
}
private void enableLikesCompanySite(string url)
{
try
{
using (var site = new SPSite(url))
{
using (var web = site.OpenWeb())
{
if (web == null)
{
return;
}
bool oldAllowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
if (!PublishingWeb.IsPublishingWeb(web))
{
return;
}
PublishingWeb pweb = null;
try
{
if (web.Lists.Cast<SPList>().Any(l => string.Equals(l.Title, LIST_TITLE, StringComparison.InvariantCultureIgnoreCase)))
{
var discussionList = web.Lists.Cast<SPList>()
.First(l => string.Equals(l.Title, LIST_TITLE, StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(l.Title, LIST_TITLE2, StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(l.Title, LIST_TITLE3, StringComparison.InvariantCultureIgnoreCase));
if (discussionList != null)
{
pweb = PublishingWeb.GetPublishingWeb(web);
var file = pweb.DefaultPage;
if (file == null)
{
this.error("Start page is null. Site won't be updated");
return;
}
if (file.CheckOutType == SPFile.SPCheckOutType.None)
{
file.CheckOut();
}
else
{
file.UndoCheckOut();
file.CheckOut();
}
EnableRatingLikes(discussionList);
file.CheckIn("Add rating field to discussion board web part");
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("Add rating field to discussion board web part");
}
if (file.Item.ParentList.EnableModeration)
{
file.Approve("Add rating field to discussion board web part");
}
}
}
}
catch (Exception ex)
{
this.error("Error occured during enable likes of Company :\n{0}\n{1}", ex.Message, ex.StackTrace);
}
finally
{
if (pweb != null)
{
var file = pweb.DefaultPage;
if (file.CheckOutType != SPFile.SPCheckOutType.None)
{
file.UndoCheckOut();
}
}
web.AllowUnsafeUpdates = oldAllowUnsafeUpdates;
}
}
}
}
catch (Exception ex)
{
this.error("Error occured during enable likes of Condo :\n{0}\n{1}", ex.Message, ex.StackTrace);
}
}
private void EnableRatingLikes(SPList list)
{
try
{
Assembly theAssembly = Assembly.Load("Microsoft.SharePoint.Portal, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
Type reputationHelperType = theAssembly.GetType("Microsoft.SharePoint.Portal.ReputationHelper", false, true);
MethodInfo milf = reputationHelperType.GetMethod("EnableReputation", BindingFlags.Static | BindingFlags.NonPublic);
milf.Invoke(reputationHelperType, new object[] { list, "Likes", false });
list.Update();
}
catch (Exception ex)
{
this.error("Error occured during enable likes of Condo :\n{0}\n{1}", 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
}
}