Taylohtio/CondoUpdate/CondoUpdate.ActivateIncomin.../UpdaterImpl.cs

119 lines
4.1 KiB
C#

using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.ActivateIncomingEmails.Owners
{
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(SPUrlUtility.CombineUrl(url, "hallitukselle"), "");
// this.updateImpl(SPUrlUtility.CombineUrl(url, "asukkaille"), "_asukas");
this.updateImpl(SPUrlUtility.CombineUrl(url, "osakkaille"), "_osakas");
}
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 suffix)
{
using (var site = new SPSite(url))
{
using (var web = site.OpenWeb())
{
if (!web.Exists || string.Compare(web.Url, url, true) != 0)
{
return;
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language);
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.DefaultViewUrl.ToLower().Contains("/documents/"));
if (list == null)
{
this.error("Documents doclib not found");
return;
}
if (!string.IsNullOrEmpty(list.EmailAlias))
{
this.warn("List '{0}' already has email enabled '{1}'", list.DefaultViewUrl, list.EmailAlias);
return;
}
var condoWeb = web.ParentWeb;
string emailAlias = condoWeb.Url.Substring(condoWeb.Url.LastIndexOf("/") + 1);
emailAlias = emailAlias.ToLower().Replace("ä", "a").Replace("ö", "o").Replace("å", "a");
if (string.IsNullOrEmpty(emailAlias))
{
this.error("Email alias is null or empty");
return;
}
if (!string.IsNullOrEmpty(suffix))
{
emailAlias += suffix;
}
list.EmailAlias = emailAlias;
var rootFolder = list.RootFolder;
rootFolder.Properties["vti_emailattachmentfolders"] = "subject";
rootFolder.Properties["vti_emailoverwrite"] = 1;
rootFolder.Properties["vti_emailsaveoriginal"] = 0;
rootFolder.Properties["vti_emailsavemeetings"] = 0;
rootFolder.Properties["vti_emailusesecurity"] = 0;
rootFolder.Update();
//list.ResetContentTypes();
ReflectionHelper.CallMethod(list, "ResetContentTypes");
list.Update();
}
}
}
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 });
}
}
}
}