Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Seal 395 posts 2156 karma points MVP 2x c-trib
    May 14, 2019 @ 12:55
    Paul Seal
    0

    How do I add a custom dashboard to the content section so that it shows first in the list of tabs.

    I am developing a package in my spare time and I want to release v1.0.0 which is for Umbraco 7.

    One of the last things left to do is get it to add a dashboard section when you install the package.

    I believe you can do that by adding this package action:

    <Actions> <Action runat="install" alias="addDashboardSection" dashboardAlias="prontoDashboardSection"> <section> <areas> <area>content</area> </areas> <tab caption="Pronto Dashboard"> <control>/App_Plugins/usync/uSyncDashboard.html</control> </tab> </section> </Action> </Actions>

    Do you know how I can get it to be the first tab in the list of tabs, without manually editing the dashboard.config file?

    Any help would be appreciated please.

    Thanks

    Paul

  • Marc Love 313 posts 801 karma points
    May 14, 2019 @ 13:17
    Marc Love
    100

    Hi Paul,

    I use my own package action for this. Not sure where I got the code from but must have come from somewhere:

    public class AddDashboard : IPackageAction
    {
        public string Alias()
        {
            return "AddDashboard";
        }
    
        public bool Execute(string packageName, XmlNode xmlData)
        {
    
            LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, String.Format("Start Action {0}: Execute() for package {1}.", this.Alias(), packageName));
    
            try
            {
                this.AddSectionDashboard("MyDashboard", "content", "Dashboard", "DASHBOARD RELATIVE PATH HERE");
                LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, String.Format("Complete Action {0}: Execute() for package {1}.", this.Alias(), packageName));
    
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, String.Format("Error in {0}: Execute() for package {1}.", this.Alias(), packageName), ex);
    
                return false;
            }
        }
    
        public void AddSectionDashboard(string sectionAlias, string area, string tabCaption, string src)
        {
            bool saveFile = false;
    
            //Path to the file resolved
            var dashboardFilePath = IOHelper.MapPath(SystemFiles.DashboardConfig);
    
            //Load settings.config XML file
            XmlDocument dashboardXml = new XmlDocument();
            dashboardXml.Load(dashboardFilePath);
    
            // Section Node
            XmlNode findSection = dashboardXml.SelectSingleNode("//section [@alias='" + sectionAlias + "']");
    
            //Couldn't find it
            if (findSection == null)
            {
                //Let's add the xml
                var xmlToAdd = "<section alias='" + sectionAlias + "'>" +
                                    "<areas>" +
                                        "<area>" + area + "</area>" +
                                    "</areas>" +
                                    "<tab caption='" + tabCaption + "'>" +
                                        "<control addPanel='true' panelCaption=''>" + src + "</control>" +
                                    "</tab>" +
                               "</section>";
    
                //Get the main root <dashboard> node
                XmlNode dashboardNode = dashboardXml.SelectSingleNode("//dashBoard");
    
                if (dashboardNode != null)
                {
                    //Load in the XML string above
                    XmlDocument xmlNodeToAdd = new XmlDocument();
                    xmlNodeToAdd.LoadXml(xmlToAdd);
    
                    var toAdd = xmlNodeToAdd.SelectSingleNode("*");
    
                    //Prepend the xml above to the dashboard node - so that it will be the first dashboards to show in the backoffice.
                    dashboardNode.PrependChild(dashboardNode.OwnerDocument.ImportNode(toAdd, true));
    
                    //Save the file flag to true
                    saveFile = true;
                }
            }
    
            //If saveFile flag is true then save the file
            if (saveFile)
            {
                //Save the XML file
                dashboardXml.Save(dashboardFilePath);
            }
        }
    
    
        public XmlNode SampleXml()
        {
            string sample = "<Action runat=\"install\" undo=\"true/false\" alias=\"AddDasboard\"/>";
    
            return helper.parseStringToXmlNode(sample);
        }
    
        public bool Undo(string packageName, XmlNode xmlData)
        {
            this.RemoveDashboardTab("USNDashboard");
    
            return true;
        }
    
        public void RemoveDashboardTab(string sectionAlias)
        {
    
            string dbConfig = IOHelper.MapPath(SystemFiles.DashboardConfig);
            XmlDocument dashboardFile = new XmlDocument();
            dashboardFile.Load(dbConfig);
    
            XmlNode section = dashboardFile.SelectSingleNode("//section [@alias = '" + sectionAlias + "']");
    
            if (section != null)
            {
                dashboardFile.SelectSingleNode("//dashBoard").RemoveChild(section);
                dashboardFile.Save(dbConfig);
            }
    
        }
    }
    

    Then I use the following within my package file to fire it:

    <Action runat="install" undo="true" alias="AddDashboard" />
    
  • Paul Seal 395 posts 2156 karma points MVP 2x c-trib
    May 14, 2019 @ 13:35
    Paul Seal
    0

    Thanks for this Marc, it's exactly what I was looking for.

  • Marc Love 313 posts 801 karma points
    May 14, 2019 @ 13:36
    Marc Love
    0

    Great..

  • Paul Seal 395 posts 2156 karma points MVP 2x c-trib
    May 14, 2019 @ 13:55
    Paul Seal
    0

    Can you share your using statements at the top of the class please?

  • Marc Love 313 posts 801 karma points
    May 14, 2019 @ 14:13
    Marc Love
    1
    using System;
    using System.Xml;
    using umbraco.interfaces;
    using umbraco.cms.businesslogic.packager.standardPackageActions;
    using Umbraco.Core.Logging;
    using umbraco.IO;
    
Please Sign in or register to post replies

Write your reply to:

Draft