284 lines
11 KiB
C#
284 lines
11 KiB
C#
using Microsoft.SharePoint;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
using Microsoft.SharePoint.Publishing;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using Microsoft.SharePoint.Publishing.Navigation;
|
|
using Microsoft.SharePoint.Navigation;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using System.IO;
|
|
using System.Xml;
|
|
|
|
namespace CondoUpdate.ResponsiveLayout.TyokalutListAndWebPartTallier
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string LIST_TITLE = "Työkalut";
|
|
private const string TargetTitle = "Työkalut";
|
|
private const string FIELD_TITLE = "Otsikko";
|
|
private readonly Guid WEB_FEATURE_ID = new Guid("10fdc4c1-d046-4923-9aac-a7f26a8312df");
|
|
private readonly Dictionary<string, string> links = new Dictionary<string, string>()
|
|
{
|
|
{"Lähetä isoja tiedostoja", "https://mobimus.wetransfer.com/"},
|
|
{"Yhteystietojen hallinta", "/_layouts/15/Taloyhtio/ContactsUpdateTool/Contacts.aspx"},
|
|
{"Lomakeohjaukset", "~SiteCollection/lomakkeet/Lists/Lomakeohjaukset/AllItems.aspx"},
|
|
{"Saapuneet lomakkeet", "~SiteCollection/~Pages/Ilmoituksetjatilaukset.aspx"},
|
|
{"Suosittele Taloyhtio.Infoa", "http://www.taloyhtio.info/suosittele/"},
|
|
{"hallitusta", "/isakatemia/Sivut/jasentenvaihtuessa.aspx"},
|
|
{"Lisää/poista taloyhtio", "/isakatemia/Sivut/uusiyhtio.aspx"}
|
|
};
|
|
|
|
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))
|
|
{
|
|
using (var pmcWeb = site.RootWeb)
|
|
{
|
|
if (!pmcWeb.Exists)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!PublishingWeb.IsPublishingWeb(pmcWeb))
|
|
{
|
|
return;
|
|
}
|
|
var pweb = PublishingWeb.GetPublishingWeb(pmcWeb);
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)pmcWeb.Language);
|
|
this.enableToolsList(url);
|
|
this.fillPredefinedLinks(pmcWeb);
|
|
this.addWebPart(pweb);
|
|
this.addGlobalNavNode(pweb, pmcWeb);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void enableToolsList(string url)
|
|
{
|
|
try
|
|
{
|
|
using (var site = new SPSite(url))
|
|
{
|
|
|
|
using (var web = site.RootWeb)
|
|
{
|
|
if (!web.Exists)
|
|
{
|
|
this.warn("Web site '{0}' doesn't exist. It will be ignored", url);
|
|
return;
|
|
}
|
|
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
if (!web.Features.Any(f => f.DefinitionId.Equals(WEB_FEATURE_ID)))
|
|
{
|
|
web.Features.Add(WEB_FEATURE_ID, true);
|
|
}
|
|
else
|
|
{
|
|
this.warn("Feature already activated on PMC '{0}', ignore", url);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when enable Tools list on {0} error: {1} {2}", url, ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void fillPredefinedLinks(SPWeb web)
|
|
{
|
|
var pages = SPUtility.GetLocalizedString("$Resources:List_Pages_UrlName", "cmscore", web.RegionalSettings.LocaleId);
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title.Equals(LIST_TITLE));
|
|
if (list == null)
|
|
{
|
|
this.error("unable to find '{0}' list on site '{1}'", LIST_TITLE, web.Url);
|
|
return;
|
|
}
|
|
foreach (var pair in links)
|
|
{
|
|
string title = pair.Key;
|
|
string url = pair.Value.Replace("~SiteCollection", web.ServerRelativeUrl).Replace("~Pages", pages);
|
|
|
|
var item = list.AddItem();
|
|
item[FIELD_TITLE] = title;
|
|
item[SPBuiltInFieldId.URL] = url;
|
|
item.Update();
|
|
}
|
|
list.Update();
|
|
}
|
|
|
|
private void addGlobalNavNode(PublishingWeb pweb, SPWeb pmcWeb)
|
|
{
|
|
try
|
|
{
|
|
var globalNavigation = pweb.Navigation.GlobalNavigationNodes;
|
|
bool alreadyExists = globalNavigation.Cast<SPNavigationNode>().Any(spnode => spnode.Title.Equals(TargetTitle));
|
|
if (alreadyExists) return;
|
|
var list =
|
|
pmcWeb.Lists.Cast<SPList>()
|
|
.FirstOrDefault(
|
|
l => string.Equals(l.Title, TargetTitle, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
var node = SPNavigationSiteMapNode.CreateSPNavigationNode(TargetTitle,
|
|
list == null ? pmcWeb.ServerRelativeUrl : list.DefaultViewUrl,
|
|
NodeTypes.AuthoredLinkPlain, globalNavigation);
|
|
|
|
var dt = DateTime.Now;
|
|
node.Properties["CreatedDate"] = dt;
|
|
node.Properties["LastModifiedDate"] = dt;
|
|
node.Properties["Description"] = "";
|
|
node.Update();
|
|
node.MoveToLast(globalNavigation);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.error("Error occured when add web part to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message, ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private void addWebPart(PublishingWeb pweb)
|
|
{
|
|
try
|
|
{
|
|
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
|
|
{
|
|
// 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();
|
|
}
|
|
using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
|
|
{
|
|
if (webPartManager == null)
|
|
{
|
|
this.error("Web part manager is null. Site won't be updated");
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
bool alreadyExists = false;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
if (webParts.Cast<WebPart>().Any(wp => wp.GetType().FullName == "Taloyhtio.CondoAutomation.ToolsWebPart"))
|
|
{
|
|
this.warn("ToolsWebPart already exist on start page. Site won't be updated");
|
|
alreadyExists = true;
|
|
}
|
|
}
|
|
|
|
if (alreadyExists)
|
|
{
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
|
|
|
|
using (var fs = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText("ToolsWebPart.webpart"))))
|
|
{
|
|
using (var reader = new XmlTextReader(fs))
|
|
{
|
|
string error;
|
|
WebPart wp = webPartManager.ImportWebPart(reader, out error);
|
|
if (wp == null)
|
|
{
|
|
this.error("Imported web part is null. Site '{0}' won't be updated. Error message: {1}", pweb.Web.Url, error);
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
webPartManager.AddWebPart(wp, "RightColumnZone", 2);
|
|
webPartManager.SaveChanges(wp);
|
|
}
|
|
}
|
|
|
|
file.CheckIn("ToolsWebPart is added");
|
|
file.Update();
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("ToolsWebPart is added");
|
|
}
|
|
if (file.Item.ParentList.EnableModeration)
|
|
{
|
|
file.Approve("ToolsWebPart is added");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when add web part to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, x.Message, x.StackTrace);
|
|
var file = pweb.DefaultPage;
|
|
if (file.CheckOutType != SPFile.SPCheckOutType.None)
|
|
{
|
|
file.UndoCheckOut();
|
|
}
|
|
}
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|