224 lines
8.5 KiB
C#
224 lines
8.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.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.UpdateHallitukselleNewsItemView
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
public event EventHandler<LogEventArgs> OnNotify;
|
|
|
|
private const string XSL_FILE = "template.xsl";
|
|
private string xslStyleSheet = null;
|
|
|
|
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)
|
|
{
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
var resourceName = "CondoUpdate.ResponsiveLayout.UpdateHallitukselleNewsItemView.template.xslt";
|
|
using (var stream = assembly.GetManifestResourceStream(resourceName))
|
|
{
|
|
using (var reader = new StreamReader(stream))
|
|
{
|
|
xslStyleSheet = reader.ReadToEnd();
|
|
}
|
|
}
|
|
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var condoWeb = site.OpenWeb())
|
|
{
|
|
if (!condoWeb.Exists || string.Compare(condoWeb.Url, url, true) != 0)
|
|
{
|
|
this.warn("Site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(condoWeb))
|
|
{
|
|
this.warn("Site '{0}' is not publishing web. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
SPWeb boardWeb;
|
|
try
|
|
{
|
|
boardWeb = condoWeb.Webs.FirstOrDefault(w => w.Title.StartsWith("Hallitukselle"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
error("error occured when trying to get Hallitukselle web error: {0} {1}", ex.Message, ex.StackTrace);
|
|
return;
|
|
}
|
|
|
|
if (boardWeb == null)
|
|
{
|
|
this.warn("Site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(boardWeb))
|
|
{
|
|
this.warn("Site '{0}' is not publishing web. It will be ignored", url);
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(boardWeb);
|
|
|
|
xslStyleSheet = xslStyleSheet.Replace("~Site", boardWeb.ServerRelativeUrl);
|
|
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
var pages = SPUtility.GetLocalizedString("$Resources:List_Pages_UrlName", "cmscore", boardWeb.RegionalSettings.LocaleId);
|
|
var f = pweb.DefaultPage;
|
|
var file = boardWeb.GetFolder(pages).Files.Cast<SPFile>().FirstOrDefault(u => string.Compare(u.Name, "uutinen.aspx", true) == 0);
|
|
if (file == null)
|
|
{
|
|
this.error("Uutinen page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
webPartManager = boardWeb.GetLimitedWebPartManager(file.Url, PersonalizationScope.Shared);
|
|
if (webPartManager == null)
|
|
{
|
|
this.error("Web part manager is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
bool changed = false;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
if (wp.GetType().ToString().Equals("Microsoft.SharePoint.WebPartPages.DataFormWebPart"))
|
|
{
|
|
Regex regex = new Regex("{(?<Id>([a-f0-9-]+))}", RegexOptions.IgnoreCase);
|
|
Match match = regex.Match(((DataFormWebPart)wp).Xsl);
|
|
if (match.Success)
|
|
{
|
|
string listId = match.Groups["Id"].Value;
|
|
xslStyleSheet = xslStyleSheet.Replace("{ListId}", listId);
|
|
((DataFormWebPart)wp).Xsl = xslStyleSheet;
|
|
}
|
|
else
|
|
{
|
|
this.notify(LogLevel.Error, "unable to find list id");
|
|
//this.notify(LogLevel.Warn, ((DataFormWebPart)wp).Xsl);
|
|
}
|
|
webPartManager.SaveChanges(wp);
|
|
changed = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
file.Update();
|
|
file.CheckIn("Updated uutinen page");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Updated uutinen page");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("Updated uutinen page");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message, ex.StackTrace);
|
|
var file = pweb.DefaultPage;
|
|
if (file.CheckOutType != SPFile.SPCheckOutType.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (webPartManager != null)
|
|
{
|
|
if (webPartManager.Web != null)
|
|
{
|
|
webPartManager.Web.Dispose();
|
|
}
|
|
webPartManager.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#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
|
|
|
|
}
|
|
}
|