203 lines
7.7 KiB
C#
203 lines
7.7 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 WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
|
|
namespace CondoUpdate.ChangeDataFormWPsOnCompany
|
|
{
|
|
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 company site '{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())
|
|
{
|
|
this.fixWebPart(condoWeb, url, "Tilitoimisto tiedottaa - Asiakastiedotteet");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void fixWebPart(SPWeb web, string url, string webPartTitle)
|
|
{
|
|
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, webPartTitle);
|
|
}
|
|
|
|
private void fixWebPart(PublishingWeb pweb, string webPartTitle)
|
|
{
|
|
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);
|
|
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" && wp.Title == webPartTitle)
|
|
{
|
|
webPart = wp as Microsoft.SharePoint.WebPartPages.DataFormWebPart;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (webPart == null)
|
|
{
|
|
this.warn("DataFormWebPart with title '{0}' not found. It won't be updated", webPartTitle);
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
//this.info("DataFormWebPart '{0}' found on the start page", webPartTitle);
|
|
|
|
var re = new Regex(".+SelectCommand=\"(?<command>.+?)\" .+");
|
|
var m = re.Match(webPart.DataSourcesString);
|
|
if (!m.Success)
|
|
{
|
|
this.warn("SelectCommand is not found in DataSourcesString. Web part won't be updated:\n{0}", webPartTitle, webPart.DataSourcesString);
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
//this.info("DataSourcesString property contains SelectCommand");
|
|
string str =
|
|
webPart.DataSourcesString.Replace(string.Format(" SelectCommand=\"{0}\" ", m.Groups["command"].Value),
|
|
" SelectCommand=\"<View><Query><Where><Or><Gt><FieldRef Name="Expires" /><Value Type="DateTime"><Today /></Value></Gt><IsNull> <FieldRef Name="Expires" /></IsNull></Or></Where><OrderBy><FieldRef Name="Created" Ascending="FALSE" /></OrderBy></Query></View>\" ");
|
|
webPart.DataSourcesString = str;
|
|
webPartManager.SaveChanges(webPart);
|
|
//this.info("DataSourcesString property of web part '{0}' was sucessfully updated", webPartTitle);
|
|
|
|
//this.info("Checkin file");
|
|
file.Update();
|
|
file.CheckIn("DataFormWebPart is updated");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("DataFormWebPart is updated");
|
|
}
|
|
//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}", 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 });
|
|
}
|
|
}
|
|
}
|
|
}
|