using Microsoft.SharePoint; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.WebPartPages; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WebPart = System.Web.UI.WebControls.WebParts.WebPart; using Taloyhtio.CondoUpdate.Common; using System.Web.UI.WebControls.WebParts; namespace CondoUpdate.ResponsiveLayout.RemoveContactsWebpartFrontPage { 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 Condo '{0}':\n{1}\n{2}", url, x.Message, x.StackTrace); } } private void updateImpl(string url) { using (var site = new SPSite(url)) { using (var condoWeb = site.OpenWeb()) { if (!condoWeb.Exists || string.Compare(condoWeb.Url, url, true) != 0) { this.warn("Site '{0}' doesn't exist. It will be ignored", url); return; } if (!PublishingWeb.IsPublishingWeb(condoWeb)) { this.warn("Site '{0}' is not publishing web. It will be ignored", url); return; } var pweb = PublishingWeb.GetPublishingWeb(condoWeb); SPLimitedWebPartManager webPartManager = null; try { var file = pweb.DefaultPage; if (file == null) { this.error("Start page is null. Site won't be updated"); return; } if (file.CheckOutType == SPFile.SPCheckOutType.None) { file.CheckOut(); } else { file.UndoCheckOut(); file.CheckOut(); } webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); if (webPartManager == null) { this.error("Web part manager is null. Site won't be updated"); file.UndoCheckOut(); return; } bool changed = false; var webParts = webPartManager.WebParts; if (webParts != null) { foreach (WebPart wp in webParts) { if (wp.GetType().FullName == "Taloyhtio.ContactsUpdateTool.CodeFiles.ContactsWebPart") { webPartManager.DeleteWebPart(wp); changed = true; break; } } } if (!changed) { file.UndoCheckOut(); return; } file.Update(); file.CheckIn("Removed old contacts webpart"); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Removed old contacts webpart"); } if (file.Item.ParentList.EnableModeration) { file.Approve("Removed old contacts webpart"); } } catch (Exception ex) { this.error("Error occured when update web part on '{0}' site:\n{1}\n{2}", pweb.Web.Url, ex.Message, ex.StackTrace); var file = pweb.DefaultPage; if (file.CheckOutType != SPFile.SPCheckOutType.None) { file.UndoCheckOut(); } } finally { if (webPartManager != null) { if (webPartManager.Web != null) { webPartManager.Web.Dispose(); } webPartManager.Dispose(); } } } } } #region notifier 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 }); } } #endregion } }