Taylohtio/CondoUpdate/CondoUpdate.FixAttachmentsR.../UpdaterImpl.cs

157 lines
5.4 KiB
C#

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebPartPages;
using Taloyhtio.CondoUpdate.Common;
namespace CondoUpdate.AddContactsWebPart
{
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;
if (!web.MasterUrl.ToLower().EndsWith("taloyhtio_custom_layout_system.master"))
{
return;
}
var file = web.GetFile(web.MasterUrl);
if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
// if file was checked out by another user, need undo check out
file.UndoCheckOut();
}
string content = "";
using (var sr = new StreamReader(file.OpenBinaryStream()))
{
content = sr.ReadToEnd();
}
if (string.IsNullOrEmpty(content))
{
this.warn("Masterpage content is empty");
return;
}
if (content.Contains("\"SPSecurityTrimmedControlEditList\""))
{
this.warn("Masterpage already has SPSecurityTrimmedControlEditList");
return;
}
int idx1 = content.IndexOf("SPSecurityTrimmedControlPages");
if (idx1 < 0)
{
this.warn("SPSecurityTrimmedControlPages sub string not found");
return;
}
int start = idx1 + "SPSecurityTrimmedControlPages".Length;
int idx2 = content.Substring(start).IndexOf("</SharePoint:SPSecurityTrimmedControl>");
if (idx2 < 0)
{
this.warn("</SharePoint:SPSecurityTrimmedControl> sub string not found");
return;
}
int insertAt = start + idx2 + "</SharePoint:SPSecurityTrimmedControl>".Length;
var subStr1 = content.Substring(0, insertAt);
if (!subStr1.EndsWith("</SharePoint:SPSecurityTrimmedControl>"))
{
// something go wrong
this.warn("subStr1 doesn't end with </SharePoint:SPSecurityTrimmedControl>");
return;
}
var subStr2 = content.Substring(insertAt);
string newStr =
subStr1 +
"\n<SharePoint:SPSecurityTrimmedControl ID=\"SPSecurityTrimmedControlEditList\" PermissionsString=\"AddListItems\" PermissionContext=\"CurrentList\" runat=\"server\">" +
" <script type=\"text/javascript\">" +
" document.getElementById(\"s4-ribbonrow\").style.display = \"block\";" +
" document.getElementById(\"RibbonContainer-TabRowRight\").style.visibility = \"hidden\";" +
" </script>" +
"</SharePoint:SPSecurityTrimmedControl>" +
subStr2;
newStr = newStr.Replace("&nbsp; </Triggers>", " </Triggers>");
file.CheckOut();
var catalog = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
var files = catalog.RootFolder.Files;
file = files.Add(web.MasterUrl, Encoding.UTF8.GetBytes(newStr), true);
file.CheckIn("Attachments ribbon button fix");
if (file.Item.ParentList.EnableMinorVersions)
{
file.Publish("Attachments ribbon button fix");
}
if (file.Item.ParentList.EnableModeration)
{
file.Approve("Attachments ribbon button fix");
}
}
}
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 });
}
}
}
}