400 lines
16 KiB
C#
400 lines
16 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = Microsoft.SharePoint.WebPartPages.WebPart;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.PmcDiscussionBoardWebPart
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string ListTitle = "Keskustelu";
|
|
private const string TargetPageUrl = "johdonsalkku.aspx";
|
|
private readonly Guid WEB_FEATURE_ID = new Guid("915c240e-a6cc-49b8-8b2c-0bff8b553ed3");
|
|
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)
|
|
{
|
|
enableRatingFeature(url);
|
|
|
|
try
|
|
{
|
|
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; }
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(pmcWeb)) { this.warn("Web '{0}' is not publishing. It will be ignored", url); return; }
|
|
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)pmcWeb.Language);
|
|
|
|
bool allowUnsafeUpdates = pmcWeb.AllowUnsafeUpdates;
|
|
pmcWeb.AllowUnsafeUpdates = true;
|
|
try
|
|
{
|
|
var list = pmcWeb.Lists.Cast<SPList>().FirstOrDefault(l => string.Equals(l.Title, ListTitle, StringComparison.InvariantCultureIgnoreCase));
|
|
if (list != null)
|
|
{
|
|
EnableRatingLikes(list);
|
|
SPView view = list.Views.Cast<SPView>().LastOrDefault(v => v.BaseViewID == "3");
|
|
if (view != null)
|
|
{
|
|
if (!view.ViewFields.Exists("LinkDiscussionTitle"))
|
|
view.ViewFields.Add("LinkDiscussionTitle");
|
|
|
|
if (!view.ViewFields.Exists("Author"))
|
|
view.ViewFields.Add("Author");
|
|
|
|
if (!view.ViewFields.Exists("ItemChildCount"))
|
|
view.ViewFields.Add("ItemChildCount");
|
|
|
|
if (!view.ViewFields.Exists("Created"))
|
|
view.ViewFields.Add("Created");
|
|
|
|
if (!view.ViewFields.Exists("Body"))
|
|
view.ViewFields.Add("Body");
|
|
|
|
if (!view.ViewFields.Exists("LastReplyBy"))
|
|
view.ViewFields.Add("LastReplyBy");
|
|
|
|
if (!view.ViewFields.Exists("DiscussionLastUpdated"))
|
|
view.ViewFields.Add("DiscussionLastUpdated");
|
|
|
|
if (!view.ViewFields.Exists("BestAnswerId"))
|
|
view.ViewFields.Add("BestAnswerId");
|
|
|
|
if (!view.ViewFields.Exists("IsFeatured"))
|
|
view.ViewFields.Add("IsFeatured");
|
|
|
|
if (!view.ViewFields.Exists("LikesCount"))
|
|
view.ViewFields.Add("LikesCount");
|
|
|
|
if (!view.ViewFields.Exists("Popularity"))
|
|
view.ViewFields.Add("Popularity");
|
|
|
|
if (!view.ViewFields.Exists("DescendantLikesCount"))
|
|
view.ViewFields.Add("DescendantLikesCount");
|
|
|
|
if (!view.ViewFields.Exists("DescendantRatingsCount"))
|
|
view.ViewFields.Add("DescendantRatingsCount");
|
|
|
|
if (!view.ViewFields.Exists("TopicLastRatedOrLikedBy"))
|
|
view.ViewFields.Add("TopicLastRatedOrLikedBy");
|
|
|
|
if (!view.ViewFields.Exists("LikedBy"))
|
|
view.ViewFields.Add("LikedBy");
|
|
|
|
view.Update();
|
|
list.Update();
|
|
}
|
|
ConfiguredWebPart(pmcWeb);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error("error occured when tring to get Hallitukselle web error: {0} {1}", ex.Message, ex.StackTrace);
|
|
}
|
|
finally
|
|
{
|
|
pmcWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error("error occured when tring to get Hallitukselle web error: {0} {1}", ex.Message,
|
|
ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
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 PMC '{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 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 Pmc :\n{0}\n{1}", ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void ConfiguredWebPart(SPWeb web)
|
|
{
|
|
PublishingWeb pweb = null;
|
|
SPFile file = null;
|
|
try
|
|
{
|
|
pweb = PublishingWeb.GetPublishingWeb(web);
|
|
if (pweb == null)
|
|
{
|
|
this.warn("Publishing web of '{0}' is null. It will be ignored", web.Url);
|
|
return;
|
|
}
|
|
file = web.GetFolder((int) web.Language == 1035 ? "Sivut" : "Pages")
|
|
.Files.Cast<SPFile>()
|
|
.First(f => f.Name.ToLower() == TargetPageUrl);
|
|
|
|
if (file == null)
|
|
{
|
|
this.error("Start page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
// if file was checked out by another user, need undo check out and check in again in order to avoid errors
|
|
// when add web part
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
|
|
{
|
|
if (webPartManager == null)
|
|
{
|
|
this.error("Web part manager is null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
if (web.Lists.Cast<SPList>().Any(l => string.Equals(l.Title, ListTitle, StringComparison.InvariantCultureIgnoreCase)))
|
|
{
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => string.Equals(l.Title, ListTitle, StringComparison.InvariantCultureIgnoreCase));
|
|
if (list != null)
|
|
{
|
|
SPView view = list.DefaultView;
|
|
|
|
bool alreadyExists = false;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
if (webParts.Cast<WebPart>().Any(wp => wp.Title.ToLower().Equals(ListTitle.ToLower())))
|
|
{
|
|
alreadyExists = true;
|
|
}
|
|
}
|
|
|
|
if (view != null)
|
|
{
|
|
if (!view.ViewFields.Exists("LinkDiscussionTitle"))
|
|
view.ViewFields.Add("LinkDiscussionTitle");
|
|
|
|
|
|
if (!view.ViewFields.Exists("Author"))
|
|
view.ViewFields.Add("Author");
|
|
|
|
|
|
if (!view.ViewFields.Exists("ItemChildCount"))
|
|
view.ViewFields.Add("ItemChildCount");
|
|
|
|
|
|
if (!view.ViewFields.Exists("Created"))
|
|
view.ViewFields.Add("Created");
|
|
|
|
|
|
if (!view.ViewFields.Exists("Body"))
|
|
view.ViewFields.Add("Body");
|
|
|
|
|
|
if (!view.ViewFields.Exists("LastReplyBy"))
|
|
view.ViewFields.Add("LastReplyBy");
|
|
|
|
|
|
if (!view.ViewFields.Exists("DiscussionLastUpdated"))
|
|
view.ViewFields.Add("DiscussionLastUpdated");
|
|
|
|
|
|
if (!view.ViewFields.Exists("BestAnswerId"))
|
|
view.ViewFields.Add("BestAnswerId");
|
|
|
|
|
|
if (!view.ViewFields.Exists("IsFeatured"))
|
|
view.ViewFields.Add("IsFeatured");
|
|
|
|
|
|
if (!view.ViewFields.Exists("LikesCount"))
|
|
{
|
|
view.ViewFields.Add("LikesCount");
|
|
}
|
|
|
|
if (!view.ViewFields.Exists("LikedBy"))
|
|
view.ViewFields.Add("LikedBy");
|
|
|
|
if (!view.ViewFields.Exists("Popularity"))
|
|
view.ViewFields.Add("Popularity");
|
|
|
|
|
|
if (!view.ViewFields.Exists("DescendantLikesCount"))
|
|
view.ViewFields.Add("DescendantLikesCount");
|
|
|
|
|
|
if (!view.ViewFields.Exists("DescendantRatingsCount"))
|
|
view.ViewFields.Add("DescendantRatingsCount");
|
|
|
|
|
|
if (!view.ViewFields.Exists("TopicLastRatedOrLikedBy"))
|
|
view.ViewFields.Add("TopicLastRatedOrLikedBy");
|
|
|
|
|
|
view.Update();
|
|
list.Update();
|
|
}
|
|
|
|
|
|
if (alreadyExists)
|
|
{
|
|
var webPart = webParts.Cast<WebPart>().First(wp => wp.Title.ToLower().Equals(ListTitle.ToLower()));
|
|
|
|
if (view != null)
|
|
{
|
|
var xslt = webPart as XsltListViewWebPart;
|
|
|
|
if (xslt != null)
|
|
{
|
|
xslt.ListId = list.ID;
|
|
xslt.ViewId = int.Parse(view.BaseViewID);
|
|
xslt.XmlDefinition = view.GetViewXml();
|
|
xslt.TitleUrl = list.DefaultViewUrl;
|
|
xslt.ClientRender = true;
|
|
xslt.ViewGuid = view.ID.ToString("B").ToUpper();
|
|
webPartManager.SaveChanges(xslt);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.warn("Subject view is null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.warn("web part doesn't exist is null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
}
|
|
|
|
file.Update();
|
|
file.CheckIn("PmcDiscussionBoardWebPart is configured");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("PmcDiscussionBoardWebPart is configured");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("PmcDiscussionBoardWebPart is configured");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when add web part to the '{0}' site:\n{1}\n{2}", web.Url, x.Message, x.StackTrace);
|
|
if (pweb != null)
|
|
{
|
|
if (file != null && file.CheckOutType != SPFile.SPCheckOutType.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|