208 lines
7.6 KiB
C#
208 lines
7.6 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
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.ChangeDataFormWPsOnBoardMembers
|
|
{
|
|
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)
|
|
{
|
|
//this.info("Opening Condo '{0}'...", url);
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var condoWeb = site.OpenWeb())
|
|
{
|
|
var boardMembersWeb = condoWeb.Webs.Cast<SPWeb>().FirstOrDefault(w => w.Title == "Hallitukselle");
|
|
if (boardMembersWeb != null)
|
|
{
|
|
this.fixWebPart(boardMembersWeb);
|
|
}
|
|
else
|
|
{
|
|
this.warn("Board members site is not found for condo '{0}'", url);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void fixWebPart(SPWeb boardMembersWeb)
|
|
{
|
|
if (!boardMembersWeb.Exists)
|
|
{
|
|
this.warn("Site '{0}' doesn't exist. It will be ignored", boardMembersWeb.Url);
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(boardMembersWeb))
|
|
{
|
|
this.warn("Site '{0}' is not publishing web. It will be ignored", boardMembersWeb.Url);
|
|
return;
|
|
}
|
|
var boardMembersPWeb = PublishingWeb.GetPublishingWeb(boardMembersWeb);
|
|
//this.info("Condo '{0}' was sucessfully opened", url);
|
|
|
|
var list = boardMembersPWeb.PagesList;
|
|
foreach (SPListItem item in list.Items)
|
|
{
|
|
string fileUrl = item.File.Url.ToLower();
|
|
if (fileUrl.EndsWith("/default.aspx") || fileUrl.EndsWith("/uutinen.aspx"))
|
|
{
|
|
this.fixWebPart(boardMembersPWeb, item.File);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void fixWebPart(PublishingWeb boardMembersPWeb, SPFile file)
|
|
{
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
//this.info("Get start page");
|
|
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);
|
|
Microsoft.SharePoint.WebPartPages.DataFormWebPart webPart = null;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
if (wp.GetType().FullName == "Microsoft.SharePoint.WebPartPages.DataFormWebPart")
|
|
{
|
|
webPart = wp as Microsoft.SharePoint.WebPartPages.DataFormWebPart;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (webPart == null)
|
|
{
|
|
this.warn("DataFormWebPart not found. It won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
//this.info("DataFormWebPart '{0}' found on the start page", webPartTitle);
|
|
|
|
//if (!webPart.Xsl.Contains("href=\"~SiteCollection/Lists/Hallitustiedotteet/AllItems.aspx\""))
|
|
var re = new Regex("href=\".+?/Lists/Hallitustiedotteet/AllItems.aspx\"");
|
|
var m = re.Match(webPart.Xsl);
|
|
if (!m.Success)
|
|
{
|
|
this.warn("Link to list view not found in web part's xsl");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
//this.info("DataSourcesString property contains SelectCommand");
|
|
webPart.Xsl = re.Replace(webPart.Xsl, string.Format("href=\"{0}\"", SPUrlUtility.CombineUrl(boardMembersPWeb.Web.ServerRelativeUrl, "/_layouts/15/taloyhtio/condoautomation/boardnews.aspx")));
|
|
webPartManager.SaveChanges(webPart);
|
|
//this.info("DataSourcesString property of web part '{0}' was sucessfully updated", webPartTitle);
|
|
|
|
//this.info("Checkin file");
|
|
file.Update();
|
|
file.CheckIn("Change arkisto link");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Change arkisto link");
|
|
}
|
|
//this.info("Web part was sucessfully updated");
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", boardMembersPWeb.Web.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 });
|
|
}
|
|
}
|
|
}
|
|
}
|