186 lines
6.2 KiB
C#
186 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using Microsoft.SharePoint;
|
|
using Microsoft.SharePoint.Administration;
|
|
using Microsoft.SharePoint.Utilities;
|
|
using SPSolutions.SharePoint.Alerts;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.AppendTextToAlertTemplates
|
|
{
|
|
public class UpdaterImpl : ICondoUpdater
|
|
{
|
|
private const string ALERT_TEMPLATE_BODY_PROPERTY_NAME = "NotificationMessage";
|
|
|
|
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)
|
|
{
|
|
/*string filePath = "textToAppend.txt";
|
|
if (!File.Exists(filePath))
|
|
{
|
|
this.error("File '{0}' not found", filePath);
|
|
return;
|
|
}
|
|
string textToAppend = File.ReadAllText(filePath);
|
|
if (string.IsNullOrEmpty(textToAppend))
|
|
{
|
|
this.error("Text to append is empty");
|
|
return;
|
|
}*/
|
|
|
|
using (var s = new SPSite(url))
|
|
{
|
|
var web = s.RootWeb;
|
|
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
|
|
this.checkWeb(web);
|
|
}
|
|
}
|
|
|
|
private void checkWeb(SPWeb web)
|
|
{
|
|
foreach (SPList l in web.Lists)
|
|
{
|
|
this.ensureText(l);
|
|
}
|
|
|
|
foreach (SPWeb subWeb in web.Webs)
|
|
{
|
|
this.checkWeb(subWeb);
|
|
}
|
|
}
|
|
|
|
private void ensureText(SPList l)
|
|
{
|
|
try
|
|
{
|
|
//var alertTemplate = l.AlertTemplate;
|
|
var alert = getFirstListAlert(l);
|
|
if (alert == null)
|
|
{
|
|
//log("There are no alerts in the list");
|
|
return;
|
|
}
|
|
|
|
// get alert template from this list
|
|
var alertTemplate = alert.AlertTemplate;
|
|
if (alertTemplate == null)
|
|
{
|
|
//error("Alert's alert template is null (list '{0}')", l.DefaultViewUrl);
|
|
return;
|
|
}
|
|
|
|
if (!isCustom(alertTemplate))
|
|
{
|
|
//error("Alert template '{0}' is not custom (list '{1}')", alertTemplate.DisplayName, l.DefaultViewUrl);
|
|
return;
|
|
}
|
|
|
|
if (alertTemplate.Properties == null || !alertTemplate.Properties.ContainsKey(ALERT_TEMPLATE_BODY_PROPERTY_NAME))
|
|
{
|
|
error("Alert template '{0}' doesn't have {1} property (list '{2}')", alertTemplate.DisplayName, ALERT_TEMPLATE_BODY_PROPERTY_NAME, l.DefaultViewUrl);
|
|
return;
|
|
}
|
|
|
|
string body = alertTemplate.Properties[ALERT_TEMPLATE_BODY_PROPERTY_NAME] as string;
|
|
if (string.IsNullOrEmpty(body))
|
|
{
|
|
error("{0} property is empty (list '{1}')", ALERT_TEMPLATE_BODY_PROPERTY_NAME, l.DefaultViewUrl);
|
|
return;
|
|
}
|
|
|
|
// if (body.Contains("Jos haluat muuttaa tietojasi tai perua sähköposti-ilmoitukset"))
|
|
// {
|
|
// return;
|
|
// }
|
|
//
|
|
// int idx = body.IndexOf("</BodyTemplate>");
|
|
// if (idx < 0)
|
|
// {
|
|
// return;
|
|
// }
|
|
//
|
|
// string sub1 = body.Substring(0, idx);
|
|
// string sub2 = body.Substring(idx);
|
|
//
|
|
// string text = "<p>Jos haluat muuttaa tietojasi tai perua sähköposti-ilmoitukset, <a href=\"{SiteAbsolutePublicUrl}/lopetatilaus/_layouts/15/Taloyhtio/OwnProfile/OwnProfile.aspx\">klikkaa tästä</a></p>";
|
|
|
|
if (!body.Contains("{SiteAbsolutePublicUrl}"))
|
|
{
|
|
return;
|
|
}
|
|
|
|
alertTemplate.Properties[ALERT_TEMPLATE_BODY_PROPERTY_NAME] = body.Replace("{SiteAbsolutePublicUrl}", "{WebAppAbsolutePublicUrl}");
|
|
alertTemplate.Update();
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when try to update alert template for list '{0}': {1}\n{2}", l.DefaultViewUrl, x.Message, x.StackTrace);
|
|
}
|
|
}
|
|
|
|
private static SPAlert getFirstListAlert(SPList list)
|
|
{
|
|
var web = list.ParentWeb;
|
|
var alerts = web.Alerts.Cast<SPAlert>();
|
|
return alerts.FirstOrDefault(a => a.ListID == list.ID);
|
|
}
|
|
|
|
// This is copy of Alert template's AlertTemplateManager.IsCustom property implementation
|
|
private static bool isCustom(SPAlertTemplate t)
|
|
{
|
|
return (((t.Properties["Custom"] != null) && (t.Properties["Custom"] is string)) && (((string)t.Properties["Custom"]) == SPAlertTemplateUtil.CustomAlertTemplateIdentifier));
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|