Skip to content

Instantly share code, notes, and snippets.

@barryokane
Created March 22, 2016 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryokane/fef9996ef6fb9c639672 to your computer and use it in GitHub Desktop.
Save barryokane/fef9996ef6fb9c639672 to your computer and use it in GitHub Desktop.
Umbraco install package action that adds a new "tab" to the dashboard (not a "Section"). Used in: https://github.com/EndzoneSoftware/uWhiteLabel
using System.Xml;
using umbraco.interfaces;
using Umbraco.Core.IO;
using Umbraco.Core;
using uWhiteLabel.Properties;
namespace uWhiteLabel.Install
{
public class uWhiteLabelDashboardAction : IPackageAction
{
private const string startupSectionXPth = "//section[@alias='StartupDashboardSection']";
// we assume that no tab with same caption attribute value exists, if it does then do nothing!
private const string welcomeTabXPath = "//section[@alias='StartupDashboardSection']/tab[@caption='Welcome']";
public string Alias()
{
return "uWhiteLabelDashboard";
}
public bool Execute(string packageName, XmlNode xmlData)
{
string xmlTab = Resources.DashboardWelcomeTab;
string dbConfig = SystemFiles.DashboardConfig;
XmlDocument dashboardFile = XmlHelper.OpenAsXmlDocument(dbConfig);
XmlNode existingWelcomeTab = dashboardFile.SelectSingleNode(welcomeTabXPath);
if (existingWelcomeTab == null)
{
XmlNode section = dashboardFile.SelectSingleNode(startupSectionXPth);
XmlDocumentFragment xfrag = dashboardFile.CreateDocumentFragment();
xfrag.InnerXml = xmlTab;
section.PrependChild(xfrag);
dashboardFile.Save(IOHelper.MapPath(dbConfig));
}
return true;
}
public bool Undo(string packageName, XmlNode xmlData)
{
string dbConfig = SystemFiles.DashboardConfig;
XmlDocument dashboardFile = XmlHelper.OpenAsXmlDocument(dbConfig);
XmlNode section = dashboardFile.SelectSingleNode(welcomeTabXPath);
if (section != null)
{
dashboardFile.SelectSingleNode(startupSectionXPth).RemoveChild(section);
dashboardFile.Save(IOHelper.MapPath(dbConfig));
}
return true;
}
public XmlNode SampleXml()
{
var xml = "<Action runat=\"install\" undo=\"true\" alias=\"uWhiteLabelDashboard\" />";
XmlDocument x = new XmlDocument();
x.LoadXml(xml);
return x;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment