192 lines
7.2 KiB
C#
192 lines
7.2 KiB
C#
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.UpdateCompanyItemView
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
public event EventHandler<LogEventArgs> OnNotify;
|
|
|
|
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.UpdateCompanyItemView.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 companyWeb = site.OpenWeb())
|
|
{
|
|
if (!companyWeb.Exists || string.Compare(companyWeb.Url, url, true) != 0)
|
|
{
|
|
this.warn("Site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(companyWeb))
|
|
{
|
|
this.warn("Site '{0}' is not publishing web. It will be ignored", url);
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(companyWeb);
|
|
|
|
xslStyleSheet = xslStyleSheet.Replace("~Site", companyWeb.ServerRelativeUrl);
|
|
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
var pages = SPUtility.GetLocalizedString("$Resources:List_Pages_UrlName", "cmscore", companyWeb.RegionalSettings.LocaleId);
|
|
var f = pweb.DefaultPage;
|
|
//var file = pweb.GetPublishingPage("Uutinen.aspx");
|
|
var file = companyWeb.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 = companyWeb.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;
|
|
}
|
|
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
|
|
}
|
|
}
|