95 lines
2.9 KiB
C#
95 lines
2.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Microsoft.SharePoint;
|
|
using Taloyhtio.CondoUpdate.Common;
|
|
|
|
namespace CondoUpdate.TargetResponsibleUserFieldToGroup
|
|
{
|
|
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(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))
|
|
{
|
|
var web = site.RootWeb;
|
|
string listTitle = "Sivustot";
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title, listTitle, true) == 0);
|
|
if (list == null)
|
|
{
|
|
warn("List '{0}' not found", listTitle);
|
|
return;
|
|
}
|
|
|
|
string fieldTitle = "Isännöitsijä";
|
|
var field = list.Fields.Cast<SPField>().FirstOrDefault(f => f.Title == fieldTitle) as SPFieldUser;
|
|
if (field == null)
|
|
{
|
|
warn("Field '{0}' not found", fieldTitle);
|
|
return;
|
|
}
|
|
|
|
string groupName = string.Format("{0} - Isännöitsijät", site.RootWeb.Title);
|
|
var group = site.RootWeb.SiteGroups.Cast<SPGroup>().FirstOrDefault(g => string.Compare(g.Name, groupName, true) == 0);
|
|
if (group == null)
|
|
{
|
|
warn("Group '{0}' not found", groupName);
|
|
return;
|
|
}
|
|
|
|
field.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;
|
|
field.SelectionGroup = group.ID;
|
|
field.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 });
|
|
}
|
|
}
|
|
}
|
|
}
|