using System; using System.Linq; using System.Text.RegularExpressions; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.WebPartPages; using Taloyhtio.CondoUpdate.Common; using WebPart = System.Web.UI.WebControls.WebParts.WebPart; namespace CondoUpdate.ChangeDiscussionBoardViewOnCompany { 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(url); } catch(Exception x) { this.error("Error occured during updating of company site '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace); } } private void updateImpl(string url) { //this.info("Opening Condo '{0}'...", url); using (var site = new SPSite(url)) { using (var web = site.OpenWeb()) { this.fixWebPart(web, url, "Keskustelu"); } } } private void fixWebPart(SPWeb web, string url, string webPartTitle) { if (!web.Exists || string.Compare(web.Url, url, true) != 0) { this.warn("Site '{0}' doesn't exist. It will be ignored", url); return; } if (!PublishingWeb.IsPublishingWeb(web)) { this.warn("Site '{0}' is not publishing web. It will be ignored", url); return; } var pweb = PublishingWeb.GetPublishingWeb(web); //this.info("Condo '{0}' was sucessfully opened", url); this.fixView(pweb); } private void fixView(PublishingWeb pweb) { try { //this.info("Get start page"); 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) { //this.info("Checkout start page"); 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 //this.info("Start page is already checked out. Undo previous check out and check out it again"); file.UndoCheckOut(); file.CheckOut(); } var list = pweb.Web.Lists.Cast().FirstOrDefault(l => l.Title == "Keskustelu" || l.Title == "Huomioitavaa ja kommentteja"); if (list != null) { // View element on the default.aspx creates hidden view with BaseViewID = 3 var view = list.Views.Cast().LastOrDefault(v => v.BaseViewID == "3" && v.Hidden); var ctId = new SPContentTypeId("0x012001"); if (view != null && view.ContentTypeId != ctId) { view.ContentTypeId = ctId; view.Update(); } else { this.warn("Hidden view of list '{0}' not found. View won't be changed", list.Title); } } else { this.warn("Lists 'Keskustelu' or 'Huomioitavaa ja kommentteja' not found. View won't be changed"); } //this.info("Checkin file"); file.Update(); file.CheckIn("Change discussion board view"); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Change discussion board view"); } //this.info("Web part was sucessfully updated"); } catch (Exception x) { this.error("Error occured when update discussion list view on '{0}' site:\n{1}\n{2}", pweb.Web.Url, 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 }); } } } }