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 524 posts 2889 karma points MVP 6x 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 (uSkinned.net) 431 posts 1669 karma points
    May 14, 2019 @ 13:17
    Marc Love (uSkinned.net)
    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 524 posts 2889 karma points MVP 6x c-trib
    May 14, 2019 @ 13:35
    Paul Seal
    0

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

  • Marc Love (uSkinned.net) 431 posts 1669 karma points
    May 14, 2019 @ 13:36
    Marc Love (uSkinned.net)
    0

    Great..

  • Paul Seal 524 posts 2889 karma points MVP 6x 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 (uSkinned.net) 431 posts 1669 karma points
    May 14, 2019 @ 14:13
    Marc Love (uSkinned.net)
    1
    using System;
    using System.Xml;
    using umbraco.interfaces;
    using umbraco.cms.businesslogic.packager.standardPackageActions;
    using Umbraco.Core.Logging;
    using umbraco.IO;
    
  • Hiren Bariya 2 posts 73 karma points
    Jun 29, 2020 @ 15:40
    Hiren Bariya
    0

    Hi @Paul Seal

    I saw your youtube tutorials recently and it was fantastic to go ahead in Umbraco. I just start using Umbraco a few months back and I am currently Learning Umbraco plugins in Umbraco V7. so I started with the basic package: welcome dashboard. in that, I required to add a section in the dashboard.config file while installation of the package and remove that change during uninstall. I can see those things in the discussion above here but not sure where to place AddDashboard.cs file in folder architecture. so if you have any reference link or any documentation with an example like this with you, that will be very helpful to me.

    or anyone else who can suggest any reference related to this will be very helpful to me.

    Thanks in advance guys.

Please Sign in or register to post replies

Write your reply to:

Draft