Taylohtio/CondoUpdate/CondoUpdate.ChangeLatestUpd.../UpdaterImpl.cs

191 lines
6.3 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;
using Taloyhtio.CondoUpdate.Common;
using Taloyhtio.WhatsNewWebpart;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
namespace CondoUpdate.ChangeLatestUpdatesWebPart
{
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 site '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace);
}
}
private void updateImpl(string url)
{
using (var site = new SPSite(url))
{
using (var condoWeb = site.OpenWeb())
{
this.fixWebPart(condoWeb, url);
}
}
}
private void fixWebPart(SPWeb web, string url)
{
if (!web.Exists || string.Compare(web.Url, url, true) != 0)
{
this.warn("Site '{0}' doesn't exist. It will be ignored", url);
return;
}
if (!PublishingWeb.IsPublishingWeb(web))
{
this.warn("Site '{0}' is not publishing web. It will be ignored", url);
return;
}
var pweb = PublishingWeb.GetPublishingWeb(web);
//this.info("Condo '{0}' was sucessfully opened", url);
this.fixWebPart(pweb);
}
private void fixWebPart(PublishingWeb pweb)
{
SPLimitedWebPartManager webPartManager = null;
try
{
//this.info("Get start page");
var file = pweb.DefaultPage;
if (file == null)
{
this.error("Start page is null. Site won't be updated");
return;
}
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
//this.info("Checkout start page");
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
//this.info("Start page is already checked out. Undo previous check out and check out it again");
file.UndoCheckOut();
file.CheckOut();
}
//this.info("Get web part manager");
webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
if (webPartManager == null)
{
this.error("Web part manager is null. Site won't be updated");
file.UndoCheckOut();
return;
}
//this.info("Search for DataFormWebPart with title '{0}' on the start page", webPartTitle);
WhatsNewWebpart webPart = null;
var webParts = webPartManager.WebParts;
if (webParts != null)
{
foreach (WebPart wp in webParts)
{
if (wp.GetType().FullName == "Taloyhtio.WhatsNewWebpart.WhatsNewWebpart")
{
webPart = wp as Taloyhtio.WhatsNewWebpart.WhatsNewWebpart;
break;
}
}
}
if (webPart == null)
{
this.warn("WhatsNewWebpart not found. It won't be updated");
file.UndoCheckOut();
return;
}
webPart.SortBy = WhatsNewWebpart.SortMode.Created;
webPart.ShowDate = false;
webPart.ShowTime = false;
webPart.ShowCreatedDate = true;
webPart.ShowCreatedTime = true;
webPartManager.SaveChanges(webPart);
file.Update();
file.CheckIn("Show created date and time instead of modified date and time in WhatsNewWebpart.");
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("Show created date and time instead of modified date and time in WhatsNewWebpart.");
}
}
catch (Exception x)
{
this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", pweb.Web.Url, x.Message, x.StackTrace);
var file = pweb.DefaultPage;
if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
file.UndoCheckOut();
}
}
finally
{
if (webPartManager != null)
{
if (webPartManager.Web != null)
{
webPartManager.Web.Dispose();
}
webPartManager.Dispose();
}
}
}
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 });
}
}
}
}