using System; using System.IO; using System.Linq; using System.Text; using System.Web.UI.WebControls.WebParts; using System.Xml; using Microsoft.SharePoint; using Microsoft.SharePoint.Publishing; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.WebPartPages; using Taloyhtio.CondoUpdate.Common; using WebPart = System.Web.UI.WebControls.WebParts.WebPart; namespace CondoUpdate.AddFormFilling { 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)) { var web = site.RootWeb.Webs.Cast().FirstOrDefault(w => w.Url.ToLower().Contains("/lomakkeet")); if (web == null) { this.warn("Akatemia site not found"); return; } this.updatePages(web); this.updateListForm(web); } } private void updateListForm(SPWeb web) { SPLimitedWebPartManager webPartManager = null; try { var list = web.Lists.Cast().FirstOrDefault(l => l.Title == "Kunnossapito- ja muutostyöilmoitus"); if (list == null) { this.warn("List 'Kunnossapito- ja muutostyöilmoitus' not found"); return; } var rootFolder = list.RootFolder; string newFormUrl = SPUrlUtility.CombineUrl(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, rootFolder.Url), "NewForm.aspx"); var newForm = web.GetFile(newFormUrl); webPartManager = newForm.GetLimitedWebPartManager(PersonalizationScope.Shared); var webParts = webPartManager.WebParts; if (webParts != null) { foreach (WebPart wp in webParts) { if (wp.GetType().FullName == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart") { var contentEditor = (ContentEditorWebPart) wp; if (contentEditor.Content != null && contentEditor.Content.InnerText != null && contentEditor.Content.InnerText.Contains("/_layouts/taloyhtio")) { XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlElement = xmlDoc.CreateElement("MyElement"); xmlElement.InnerText = contentEditor.Content.InnerText.Replace("/_layouts/taloyhtio", "/_layouts/15/taloyhtio"); // we MUST set the Content property, not a property of Content. // For example, changing the InnerXml or InnerText property of // Content will not be saved. contentEditor.Content = xmlElement; webPartManager.SaveChanges(contentEditor); return; } else { this.warn("Content editor web part already exist on page '{0}', it won't be updated", newForm.Url); return; } } } } using (var fs = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText("ContentEditorWebPart.dwp")))) { using (var reader = new XmlTextReader(fs)) { string error; WebPart wp = webPartManager.ImportWebPart(reader, out error); if (wp == null) { this.error("Imported web part is null. Page '{0}' won't be updated. Error message: {1}", newForm.Url, error); return; } webPartManager.AddWebPart(wp, "Main", 0); webPartManager.SaveChanges(wp); } } } finally { if (webPartManager != null) { if (webPartManager.Web != null) { webPartManager.Web.Dispose(); } webPartManager.Dispose(); } } } private void updatePages(SPWeb web) { var pweb = PublishingWeb.GetPublishingWeb(web); var list = pweb.PagesList; foreach (SPListItem item in list.Items) { string fileUrl = item.File.Url.ToLower(); // if (fileUrl.EndsWith("/autopaikka.aspx") || // fileUrl.EndsWith("/avaintilaus.aspx") || // fileUrl.EndsWith("/hairioilmoitus.aspx") || // fileUrl.EndsWith("/isannoitsijantodistuksentilaus.aspx") || // fileUrl.EndsWith("/muuttoilmoitus.aspx") || // fileUrl.EndsWith("/sahkomittarilukema.aspx") || // fileUrl.EndsWith("/saunavuoro.aspx") || // fileUrl.EndsWith("/talonkirjaote.aspx") || // fileUrl.EndsWith("/vesimittarilukema.aspx") || // fileUrl.EndsWith("/vikailmoitus.aspx")) if (!fileUrl.EndsWith("/default.aspx") && !fileUrl.EndsWith("/kiitos.aspx")) { this.addWebPart(item.File); } } } private void addWebPart(SPFile file) { SPLimitedWebPartManager webPartManager = null; try { 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(); } webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); if (webPartManager == null) { this.error("Web part manager is null. Site won't be updated"); file.UndoCheckOut(); return; } bool exists = false; var webParts = webPartManager.WebParts; if (webParts != null) { foreach (WebPart wp in webParts) { if (wp.GetType().FullName == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart") { exists = true; var contentEditor = (ContentEditorWebPart) wp; if (contentEditor.Content != null && contentEditor.Content.InnerText != null && contentEditor.Content.InnerText.Contains("/_layouts/taloyhtio")) { XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlElement = xmlDoc.CreateElement("MyElement"); xmlElement.InnerText = contentEditor.Content.InnerText.Replace("/_layouts/taloyhtio", "/_layouts/15/taloyhtio"); // we MUST set the Content property, not a property of Content. // For example, changing the InnerXml or InnerText property of // Content will not be saved. contentEditor.Content = xmlElement; webPartManager.SaveChanges(contentEditor); } else { this.warn("Content editor web part already exist on page '{0}', it won't be updated", file.Url); file.UndoCheckOut(); return; } } } } if (!exists) { using (var fs = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText("ContentEditorWebPart.dwp")))) { using (var reader = new XmlTextReader(fs)) { string error; WebPart wp = webPartManager.ImportWebPart(reader, out error); if (wp == null) { this.error( "Imported web part is null. Page '{0}' won't be updated. Error message: {1}", file.Url, error); file.UndoCheckOut(); return; } webPartManager.AddWebPart(wp, "ContentColTwo", 0); webPartManager.SaveChanges(wp); } } } file.CheckIn("Content editor web part is added"); file.Update(); if (file.Item.ParentList.EnableMinorVersions) { file.Publish("Content editor web part is added"); } } catch (Exception x) { this.error("Error occured when add web part to the '{0}' site:\n{1}\n{2}", file.Url, x.Message, x.StackTrace); if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None) { file.UndoCheckOut(); } } finally { if (webPartManager != null) { if (webPartManager.Web != null) { webPartManager.Web.Dispose(); } webPartManager.Dispose(); } } } 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 }); } } } }