Taylohtio/CondoUpdate/CondoUpdate.AddonsTargeting/UpdaterImpl.cs

140 lines
4.3 KiB
C#

using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.AddonsTargeting
{
public class UpdaterImpl : ICondoUpdater
{
public const string LIST_TITLE = "Lisäpalvelut";
public const string FIELD_TARGET_GROUPS = "Kohderyhmät";
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))
{
bool updateRootWeb = false;
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["updateRootWeb"]))
{
updateRootWeb = bool.Parse(ConfigurationManager.AppSettings["updateRootWeb"]);
}
if (updateRootWeb)
{
this.fixAddonsList(site.RootWeb);
}
else
{
using (var web = site.OpenWeb())
{
this.fixAddonsList(web);
}
}
}
}
private void fixAddonsList(SPWeb web)
{
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == LIST_TITLE);
if (list == null)
{
// list is not created yet => condo was not updated yet
return;
}
var field = list.Fields.Cast<SPField>().FirstOrDefault(f => f.Title == FIELD_TARGET_GROUPS);
if (field != null)
{
// field already exists
return;
}
string groupsFieldId = list.Fields.Add(FIELD_TARGET_GROUPS, SPFieldType.User, false);
list.Update();
var groupsField = list.Fields.Cast<SPField>().FirstOrDefault(i => i.InternalName == groupsFieldId || i.StaticName == groupsFieldId) as SPFieldUser;
if (groupsField == null)
{
throw new Exception(string.Format("Field '{0}' not found in the list '{1}'", FIELD_TARGET_GROUPS, LIST_TITLE));
}
groupsField.SelectionMode = SPFieldUserSelectionMode.PeopleAndGroups;
groupsField.AllowMultipleValues = true;
groupsField.Update();
list.Update();
var view = list.DefaultView;
Action<string> addFieldToView = fieldTitle =>
{
if (string.IsNullOrEmpty(fieldTitle))
{
return;
}
var f = list.Fields.Cast<SPField>().FirstOrDefault(i => i.Title == fieldTitle || i.StaticName == fieldTitle);
if (f == null)
{
return;
}
view.ViewFields.Add(f);
};
addFieldToView(FIELD_TARGET_GROUPS);
view.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 });
}
}
}
}