215 lines
6.8 KiB
C#
215 lines
6.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web.UI.WebControls.WebParts;
|
|
using System.Xml;
|
|
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.RemoveOldResponsibleUserField
|
|
{
|
|
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;
|
|
var list = web.Lists.Cast<SPList>().FirstOrDefault(l => string.Compare(l.Title, "Sivustot", true) == 0);
|
|
if (list == null)
|
|
{
|
|
warn("List not found");
|
|
return;
|
|
}
|
|
|
|
var oldField = list.Fields.Cast<SPField>().FirstOrDefault(f => f.Title == "Isännöitsijä");
|
|
if (oldField != null && oldField.Type == SPFieldType.MultiChoice)
|
|
{
|
|
oldField.Delete();
|
|
list.Update();
|
|
}
|
|
|
|
var newField = list.Fields.Cast<SPField>().FirstOrDefault(f => f.Title == "Isännöitsijä FBA");
|
|
if (newField != null)
|
|
{
|
|
newField.Title = "Isännöitsijä";
|
|
newField.Update();
|
|
}
|
|
|
|
this.fixAllSitesPage(web);
|
|
}
|
|
}
|
|
|
|
private void fixAllSitesPage(SPWeb web)
|
|
{
|
|
var pweb = PublishingWeb.GetPublishingWeb(web);
|
|
var item = pweb.PagesList.Items.Cast<SPListItem>().FirstOrDefault(i => i.Title == "Kaikki taloyhtiöt");
|
|
if (item == null)
|
|
{
|
|
warn("Page not found");
|
|
return;
|
|
}
|
|
var file = item.File;
|
|
|
|
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();
|
|
}
|
|
|
|
string viewId = "";
|
|
SPLimitedWebPartManager webPartManager = null;
|
|
try
|
|
{
|
|
webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
|
|
if (webPartManager == null)
|
|
{
|
|
file.UndoCheckOut();
|
|
warn("Web part manager can't be created");
|
|
return;
|
|
}
|
|
|
|
WebPart webPart = null;
|
|
var webParts = webPartManager.WebParts;
|
|
if (webParts != null)
|
|
{
|
|
foreach (WebPart wp in webParts)
|
|
{
|
|
if (wp.GetType() == typeof(ListViewWebPart))
|
|
{
|
|
webPart = wp;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
viewId = ((ListViewWebPart) webPart).ViewGuid;
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.error("Error occured when change list view web part to the '{0}' site:\n{1}\n{2}", pweb.Web.Url, x.Message, x.StackTrace);
|
|
file.UndoCheckOut();
|
|
return;
|
|
}
|
|
finally
|
|
{
|
|
if (webPartManager != null)
|
|
{
|
|
if (webPartManager.Web != null)
|
|
{
|
|
webPartManager.Web.Dispose();
|
|
}
|
|
webPartManager.Dispose();
|
|
}
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(viewId))
|
|
{
|
|
file.UndoCheckOut();
|
|
warn("View id is not determined");
|
|
return;
|
|
}
|
|
|
|
var list = pweb.Web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == "Sivustot");
|
|
if (list == null)
|
|
{
|
|
file.UndoCheckOut();
|
|
warn("List not found");
|
|
return;
|
|
}
|
|
|
|
var field = list.Fields.Cast<SPField>().FirstOrDefault(i => i.Title == "Isännöitsijä");
|
|
if (field == null)
|
|
{
|
|
file.UndoCheckOut();
|
|
warn("Field not found");
|
|
return;
|
|
}
|
|
|
|
var view = list.Views.Cast<SPView>().LastOrDefault(v => v.ID == new Guid(viewId) && v.Hidden);
|
|
if (view == null)
|
|
{
|
|
file.UndoCheckOut();
|
|
warn("View not found");
|
|
return;
|
|
}
|
|
var viewFields = view.ViewFields;
|
|
if (viewFields.Exists(field.InternalName))
|
|
{
|
|
warn("View already contains field");
|
|
return;
|
|
}
|
|
viewFields.Add(field);
|
|
view.Update();
|
|
|
|
file.Update();
|
|
file.CheckIn("Add new responsibel user field to list view web part");
|
|
if (file.Item.ParentList.EnableMinorVersions)
|
|
{
|
|
file.Publish("Add new responsibel user field to list view web part");
|
|
}
|
|
info("Field was successfully added");
|
|
}
|
|
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|