using System; using System.Globalization; using System.Linq; using System.Threading; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.Portal; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.WebPartPages; using Taloyhtio.CondoUpdate.Common; using WebPart = Microsoft.SharePoint.WebPartPages.WebPart; namespace CondoUpdate.DisableReputationOnDiscussionBoard { public class UpdaterImpl : ICondoUpdater { public event EventHandler 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")); } 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)) { using (var web = site.OpenWeb()) { if (!web.Exists || string.Compare(web.Url, url, true) != 0) { return; } var pweb = PublishingWeb.GetPublishingWeb(web); Thread.CurrentThread.CurrentUICulture = new CultureInfo((int)web.Language); var list = web.Lists.Cast().FirstOrDefault(l => l.DefaultViewUrl.ToLower().Contains("/keskustelupalsta/")); if (list == null) { this.error("Discussion board doclib not found"); return; } var type = typeof(RatingsSettingsPage15).Assembly.GetType("Microsoft.SharePoint.Portal.ReputationHelper"); try { var file = pweb.DefaultPage; if (file == null) { this.error("Start page is null. Site won't be updated"); return; } if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None) { file.CheckOut(); } else { // if file was checked out by another user, need undo check out and check in again in order to avoid errors // when add web part file.UndoCheckOut(); file.CheckOut(); } ReflectionHelper.CallStaticMethod(type, "DisableReputation", list); list.Update(); file.CheckIn("Reputation was disabled for Discussion board list"); file.Update(); file.Publish("Reputation was disabled for Discussion board list"); } catch (Exception x) { this.error("Error occured:\n{0}\n{1}", x.Message, x.StackTrace); var file = pweb.DefaultPage; if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None) { file.UndoCheckOut(); } } } } } 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 }); } } } }