Taylohtio/CondoUpdate/CondoUpdate.RemoveFormFilling/UpdaterImpl.cs

226 lines
8.2 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Taloyhtio.CondoUpdate.Common;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
namespace CondoUpdate.RemoveFormFilling
{
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)
{
using (var site = new SPSite(url))
{
var web = site.RootWeb.Webs.Cast<SPWeb>().FirstOrDefault(w => w.Url.ToLower().Contains("/lomakkeet"));
if (web == null)
{
this.warn("Akatemia site not found");
return;
}
this.updatePages(web);
this.updateListForm(web);
}
}
private void updateListForm(SPWeb web)
{
SPLimitedWebPartManager webPartManager = null;
try
{
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == "Kunnossapito- ja muutostyöilmoitus");
if (list == null)
{
this.warn("List 'Kunnossapito- ja muutostyöilmoitus' not found");
return;
}
var rootFolder = list.RootFolder;
string newFormUrl = SPUrlUtility.CombineUrl(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, rootFolder.Url),
"NewForm.aspx");
var newForm = web.GetFile(newFormUrl);
webPartManager = newForm.GetLimitedWebPartManager(PersonalizationScope.Shared);
var webParts = webPartManager.WebParts;
if (webParts != null)
{
foreach (WebPart wp in webParts)
{
if (wp.GetType().FullName == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart")
{
var contentEditor = (ContentEditorWebPart) wp;
if (contentEditor.Content != null && contentEditor.Content.InnerText != null &&
contentEditor.Content.InnerText.Contains("/clientdatahandler/initform.js"))
{
webPartManager.DeleteWebPart(contentEditor);
break;
}
}
}
}
}
finally
{
if (webPartManager != null)
{
if (webPartManager.Web != null)
{
webPartManager.Web.Dispose();
}
webPartManager.Dispose();
}
}
}
private void updatePages(SPWeb web)
{
var pweb = PublishingWeb.GetPublishingWeb(web);
var list = pweb.PagesList;
foreach (SPListItem item in list.Items)
{
string fileUrl = item.File.Url.ToLower();
// if (fileUrl.EndsWith("/autopaikka.aspx") ||
// fileUrl.EndsWith("/avaintilaus.aspx") ||
// fileUrl.EndsWith("/hairioilmoitus.aspx") ||
// fileUrl.EndsWith("/isannoitsijantodistuksentilaus.aspx") ||
// fileUrl.EndsWith("/muuttoilmoitus.aspx") ||
// fileUrl.EndsWith("/sahkomittarilukema.aspx") ||
// fileUrl.EndsWith("/saunavuoro.aspx") ||
// fileUrl.EndsWith("/talonkirjaote.aspx") ||
// fileUrl.EndsWith("/vesimittarilukema.aspx") ||
// fileUrl.EndsWith("/vikailmoitus.aspx"))
if (!fileUrl.EndsWith("/default.aspx") &&
!fileUrl.EndsWith("/kiitos.aspx"))
{
this.removeWebPart(item.File);
}
}
}
private void removeWebPart(SPFile file)
{
SPLimitedWebPartManager webPartManager = null;
try
{
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.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();
}
webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
if (webPartManager == null)
{
this.error("Web part manager is null. Site won't be updated");
file.UndoCheckOut();
return;
}
var webParts = webPartManager.WebParts;
if (webParts != null)
{
foreach (WebPart wp in webParts)
{
if (wp.GetType().FullName == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart")
{
var contentEditor = (ContentEditorWebPart) wp;
if (contentEditor.Content != null && contentEditor.Content.InnerText != null &&
contentEditor.Content.InnerText.Contains("/clientdatahandler/initform.js"))
{
webPartManager.DeleteWebPart(contentEditor);
break;
}
}
}
}
file.CheckIn("Content editor web part is removed");
file.Update();
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("Content editor web part is removed");
}
}
catch (Exception x)
{
this.error("Error occured when add web part to the '{0}' site:\n{1}\n{2}", file.Url, x.Message, x.StackTrace);
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 });
}
}
}
}