182 lines
5.6 KiB
C#
182 lines
5.6 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.WebPartPages;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.RenameAsumisenOppaatWebPart
|
|
{
|
|
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 '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void updateImpl(string url)
|
|
{
|
|
try
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
if (!web.Exists)
|
|
{
|
|
this.warn("Web site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
|
|
this.renameWebpart(web);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when update '{0}' site:\n{1}\n{2}", url, ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void renameWebpart(SPWeb web)
|
|
{
|
|
if (!PublishingWeb.IsPublishingWeb(web))
|
|
{
|
|
//log("Target web is not a publishing web");
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
|
|
// ensure feature is installed?
|
|
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
//log("Get file");
|
|
var file = pweb.DefaultPage;
|
|
if (file == null)
|
|
{
|
|
//log("Start page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
///log("File found");
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
|
|
if (webPartManager == null)
|
|
{
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
bool shouldSavePage = false;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webParts)
|
|
{
|
|
//this.info("{0}: {1}", wp.GetType().FullName, wp.Title);
|
|
if (wp.GetType().FullName == "Taloyhtio.CondoAutomation.AsumisenOppaatWebPart")
|
|
{
|
|
if (wp.Title != "Asumisen oppaat")
|
|
{
|
|
shouldSavePage = true;
|
|
wp.Title = "Asumisen oppaat";
|
|
webPartManager.SaveChanges(wp);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!shouldSavePage)
|
|
{
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
file.CheckIn("AsumisenWebPart is renamed");
|
|
file.Update();
|
|
file.Publish("AsumisenWebPart is renamed");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|