252 lines
8.9 KiB
C#
252 lines
8.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using System.Xml;
|
|
using Microsoft.Office.Server.WebControls.FieldTypes;
|
|
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.CentralizedNewsForOwnersPMC
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string LIST_TITLE = "Osakastiedotteet";
|
|
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;
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
|
|
this.ensureListExists(web);
|
|
this.addWebPart(web);
|
|
}
|
|
}
|
|
|
|
private void addWebPart(SPWeb web)
|
|
{
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == LIST_TITLE);
|
|
if (list == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(web))
|
|
{
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
|
|
// var list = pweb.PagesList;
|
|
// foreach (SPListItem item in list.Items)
|
|
// {
|
|
// string fileUrl = item.File.Url.ToLower();
|
|
// if (fileUrl.EndsWith("/default.aspx"))
|
|
// {
|
|
// this.moveWebParts(pweb, item.File);
|
|
// }
|
|
// }
|
|
|
|
var file = pweb.DefaultPage;
|
|
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);
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts == null)
|
|
{
|
|
this.error("Web parts are null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
if (wp.Title == "Isännöitsijältä osakkaille")
|
|
{
|
|
this.info("Web part is already added");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
}
|
|
|
|
WebPart hallituksilleWebPart = null;
|
|
var webPartsRightZone = new List<WebPart>();
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
if (wp.Title == "Isännöitsijältä hallituksille")
|
|
{
|
|
hallituksilleWebPart = wp;
|
|
}
|
|
else
|
|
{
|
|
string zoneId = webPartManager.GetZoneID(wp);
|
|
if (zoneId == "RightColumnZone")
|
|
{
|
|
webPartsRightZone.Add(wp);
|
|
}
|
|
}
|
|
}
|
|
|
|
// at first add "Isännöitsijältä hallituksille"
|
|
int index = 0;
|
|
if (hallituksilleWebPart != null)
|
|
{
|
|
webPartManager.MoveWebPart(hallituksilleWebPart, "RightColumnZone", index);
|
|
webPartManager.SaveChanges(hallituksilleWebPart);
|
|
index++;
|
|
}
|
|
// then "Isännöitsijältä osakkaille"
|
|
var xsltListViewWP = new XsltListViewWebPart();
|
|
xsltListViewWP.ListName = list.ID.ToString("B").ToUpper();
|
|
xsltListViewWP.ViewGuid = string.Empty;
|
|
xsltListViewWP.Title = "Isännöitsijältä osakkaille";
|
|
webPartManager.AddWebPart(xsltListViewWP, "RightColumnZone", index);
|
|
index++;
|
|
|
|
foreach (var wp in webPartsRightZone)
|
|
{
|
|
webPartManager.MoveWebPart(wp, "RightColumnZone", index);
|
|
webPartManager.SaveChanges(wp);
|
|
index++;
|
|
}
|
|
|
|
//this.info("DataSourcesString property of web part '{0}' was sucessfully updated", webPartTitle);
|
|
|
|
//this.info("Checkin file");
|
|
file.Update();
|
|
file.CheckIn("Add Isännöitsijältä osakkaille web part");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Add Isännöitsijältä osakkaille web part");
|
|
}
|
|
//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);
|
|
if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (webPartManager != null)
|
|
{
|
|
if (webPartManager.Web != null)
|
|
{
|
|
webPartManager.Web.Dispose();
|
|
}
|
|
webPartManager.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ensureListExists(SPWeb web)
|
|
{
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == LIST_TITLE);
|
|
if (list != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
web.Lists.Add(LIST_TITLE, string.Empty, SPListTemplateType.Announcements);
|
|
list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == LIST_TITLE);
|
|
string createFieldAsXml = GetCreateFieldAsXml();
|
|
list.Fields.AddFieldAsXml(createFieldAsXml);
|
|
list.Update();
|
|
}
|
|
|
|
internal static string GetCreateFieldAsXml()
|
|
{
|
|
XmlElement xmlElement = (new XmlDocument()).CreateElement("Field");
|
|
xmlElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d");
|
|
xmlElement.SetAttribute("Type", "TargetTo");
|
|
xmlElement.SetAttribute("Name", "TargetTo");
|
|
xmlElement.SetAttribute("DisplayName", /*StringResourceManager.GetString("ProfileStore_PublishedLinks_TargetToField")*/ "Kohdekäyttäjäryhmät");
|
|
xmlElement.SetAttribute("Required", "FALSE");
|
|
return xmlElement.OuterXml;
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|