Taylohtio/CondoUpdate/CondoUpdate.DoclibSort/UpdaterImpl.cs

183 lines
8.1 KiB
C#

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Taloyhtio.CondoUpdate.Common;
using System.Web.UI.WebControls.WebParts;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;
namespace CondoUpdate.DoclibSort
{
public class UpdaterImpl : ICondoUpdater
{
private const string WEBPART_TITLE = "Tiedostot";
private const string WEBPART_TITLE_ALT = "Asiakirjat";
private const string WEBPART_TITLE_ENG = "Documents";
private const string SORT_BY_MODIFIED = "<FieldRef Name=\"Modified\" Ascending=\"FALSE\"/>";
private const string SORT_BY_FILENAME = "<FieldRef Name=\"FileLeafRef\" Ascending=\"TRUE\"/>";
private string[] webTitles = { "Asukkaille", "Osakkaille", "Hallitukselle" };
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)
{
try
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (var site = new SPSite(url))
{
using (var web = site.OpenWeb())
{
if (web == null)
{
return;
}
foreach (var subsite in webTitles)
{
try
{
var subWeb = web.Webs.FirstOrDefault(w => w.Title.StartsWith(subsite));
if (subWeb == null)
{
this.warn("subite '{0}' doesn't exist. It will be ignored", subsite);
continue;
}
if (!PublishingWeb.IsPublishingWeb(subWeb))
{
this.warn("site '{0}' is not publishing web. It will be ignored", subWeb.Url);
continue;
}
var pweb = PublishingWeb.GetPublishingWeb(subWeb);
var file = pweb.DefaultPage;
if (file == null)
{
this.error("Start page is null. Site won't be updated");
continue;
}
if (file.CheckOutType != SPFile.SPCheckOutType.None)
{
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();
continue;
}
bool changed = false;
var webParts = webPartManager.WebParts;
if (webParts != null)
{
foreach (WebPart wp in webParts)
{
if (wp.Title.Equals(WEBPART_TITLE) || wp.Title.Equals(WEBPART_TITLE_ALT) || wp.Title.Equals(WEBPART_TITLE_ENG))
{
var view = ((XsltListViewWebPart)wp).View;
string xml = view.GetViewXml();
if (xml.Contains(SORT_BY_MODIFIED))
{
xml = xml.Replace(SORT_BY_MODIFIED, SORT_BY_FILENAME);
view.SetViewXml(xml);
view.Update();
webPartManager.SaveChanges(wp);
changed = true;
}
else
{
this.warn("sort by modified date not found. ignore");
}
break;
}
}
}
if (!changed)
{
file.UndoCheckOut();
return;
}
file.Update();
file.CheckIn("set sort order for documents webpart");
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("set sort order for documents webpart");
}
if (file.Item.ParentList.EnableModeration)
{
file.Approve("set sort order for documents webpart");
}
}
}
catch (Exception ex)
{
error("error occured when trying to get Hallitukselle web error: {0} {1}", ex.Message, ex.StackTrace);
return;
}
}
}
}
});
}
catch (Exception ex)
{
this.error("Error occured during updating of Condo :\n{0}\n{1}", ex.Message, ex.StackTrace);
}
}
#region notify
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 });
}
}
#endregion
}
}