211 lines
9.4 KiB
C#
211 lines
9.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.CompanySiteAddWebParts
|
|
{
|
|
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)
|
|
{
|
|
configureWebParts(url);
|
|
}
|
|
|
|
private void configureWebParts(string url)
|
|
{
|
|
PublishingWeb pweb = null;
|
|
try
|
|
{
|
|
SPSecurity.RunWithElevatedPrivileges(() =>
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
using (var web = site.OpenWeb())
|
|
{
|
|
if (web == null)
|
|
{
|
|
return;
|
|
}
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int) web.Language);
|
|
bool condoAllowUnsafeUpdates = web.AllowUnsafeUpdates;
|
|
web.AllowUnsafeUpdates = true;
|
|
|
|
pweb = PublishingWeb.GetPublishingWeb(web);
|
|
if (pweb == null || pweb.DefaultPage == null ||
|
|
string.IsNullOrEmpty(pweb.DefaultPage.ServerRelativeUrl))
|
|
{
|
|
this.warn("Publishing web of '{0}' is null. It will be ignored", url);
|
|
return;
|
|
}
|
|
|
|
var file = pweb.DefaultPage;
|
|
if (file == null)
|
|
{
|
|
this.error("Start page is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
if (file.CheckOutType == SPFile.SPCheckOutType.None)
|
|
{
|
|
file.CheckOut();
|
|
}
|
|
else
|
|
{
|
|
file.UndoCheckOut();
|
|
file.CheckOut();
|
|
}
|
|
|
|
using (var manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
|
|
{
|
|
if (manager == null)
|
|
{
|
|
file.UndoCheckOut();
|
|
this.error("Web part manager is null. Site won't be updated");
|
|
return;
|
|
}
|
|
|
|
if (manager.WebParts != null)
|
|
{
|
|
var tiedostotWebPart = manager.WebParts.Cast<WebPart>().FirstOrDefault(wp => wp.Title.ToLower().Equals("tiedostot"));
|
|
if (tiedostotWebPart != null)
|
|
{
|
|
manager.DeleteWebPart(tiedostotWebPart);
|
|
}
|
|
|
|
var tiedostotLahetusWebPart = manager.WebParts.Cast<WebPart>().FirstOrDefault(wp => wp.Title.ToLower().Equals("tiedostojen lähetys tilitoimistolle"));
|
|
if (tiedostotLahetusWebPart != null)
|
|
{
|
|
manager.DeleteWebPart(tiedostotLahetusWebPart);
|
|
}
|
|
|
|
// if (manager.WebParts.Cast<WebPart>().Any(wp => wp.Title.ToLower().Equals("tiedostot")) &&
|
|
// manager.WebParts.Cast<WebPart>().Any(wp => wp.Title.ToLower().Equals("tiedostojen lähetys tilitoimistolle")))
|
|
// {
|
|
// file.UndoCheckOut();
|
|
// this.error("Web parts are already added. Site won't be updated");
|
|
// return;
|
|
// }
|
|
|
|
var list1 = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == "Tiedostot");
|
|
if (list1 != null)
|
|
{
|
|
var webPart = new XsltListViewWebPart();
|
|
webPart.ListName = list1.ID.ToString("B");
|
|
//webPart.ViewGuid = string.Empty;
|
|
manager.AddWebPart(webPart, "RightColumnZone", 0);
|
|
|
|
webPart = manager.WebParts.Cast<WebPart>().FirstOrDefault(wp => wp.Title.ToLower().Equals("tiedostot")) as XsltListViewWebPart;
|
|
if (webPart != null)
|
|
{
|
|
webPart.ViewId = 0;
|
|
webPart.ViewGuid = string.Empty;
|
|
manager.SaveChanges(webPart);
|
|
}
|
|
}
|
|
|
|
var list2 = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == "Asiakirjojen lähetys tilitoimistolle");
|
|
if (list2 != null)
|
|
{
|
|
var webPart = new XsltListViewWebPart();
|
|
webPart.ListName = list2.ID.ToString("B");
|
|
//webPart.ViewGuid = string.Empty;
|
|
webPart.Title = "Tiedostojen lähetys tilitoimistolle";
|
|
//webPart.Title = "Palkanlaskenta-aineistojen lähetys tilitoimistolle"; // tevilex
|
|
//webPart.Title = "ASIAKKAALTA TILITOIMISTOLLE"; // tilitoimistoverso
|
|
manager.AddWebPart(webPart, "RightColumnZone", 1);
|
|
|
|
webPart = manager.WebParts.Cast<WebPart>().FirstOrDefault(wp => wp.Title.ToLower().Equals("tiedostojen lähetys tilitoimistolle")) as XsltListViewWebPart;
|
|
if (webPart != null)
|
|
{
|
|
webPart.ViewId = 0;
|
|
webPart.ViewGuid = string.Empty;
|
|
manager.SaveChanges(webPart);
|
|
}
|
|
}
|
|
}
|
|
file.Update();
|
|
file.CheckIn("Added company site webparts");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Added company site webparts");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("Added company site webparts");
|
|
}
|
|
}
|
|
web.AllowUnsafeUpdates = condoAllowUnsafeUpdates;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (pweb != null)
|
|
{
|
|
this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message,
|
|
ex.StackTrace);
|
|
|
|
var file = pweb.DefaultPage;
|
|
if (file.CheckOutType != SPFile.SPCheckOutType.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|