Copied to clipboard

Flag this post as spam?

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


  • Robert J. Bullock 386 posts 405 karma points
    Jan 27, 2010 @ 15:57
    Robert J. Bullock
    0

    XML/Config Driven Custom Section

    Has anyone create a custom section with a XML-driven tree? Like:

    <customPages>
    <page label="Physicians" url="/custom/physicians.aspx"/>
    </customPages>

    This file could be stored in the the Umbrco config folder and easily edited. Basically, what I'm looking for is a data-driven custom section where I don't have to manually edit that assembly that creates the navigation tree.

     

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 27, 2010 @ 16:05
    Lee Kelleher
    0

    Hi Robert,

    Yes, it's possible.  I am using this approach for my upcoming WordPress integration package.

    My base appTree reads the XML config, loops through each node/page and creates child appTrees from that.

    Let me know if you want to see a code sample, (as my WordPress integration package is not where near release yet).

    Cheers, Lee.

  • Robert J. Bullock 386 posts 405 karma points
    Jan 27, 2010 @ 16:22
    Robert J. Bullock
    0

    Yes, I'd love to see your tree code. Are you intending to store your config file in the Umbraco config folder? I figured then you could edit it with the ConfigTree package...

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 27, 2010 @ 17:54
    Lee Kelleher
    2

    Hi Robert,

    Here's an example of how you'd do it in the Render() method of the new appTree:

    public override void Render(ref XmlTree tree)
    {
        String configFile = HttpContext.Current.Server.MapPath("~/config/customPages.config");
    
        if (File.Exists(configFile))
        {
            XmlDocument configDoc = new XmlDocument();
            configDoc.Load(configFile);
    
            if (configDoc != null)
            {
                XmlElement root = configDoc.DocumentElement;
    
                foreach (XmlNode node in root.ChildNodes)
                {
                    if (node.Name.ToUpper() == "PAGE")
                    {
                        if ((this.m_id == -1) && !this.IsDialog)
                        {
                            String label = node.Attributes["label"].Value;
                            String url = node.Attributes["url"].Value;
    
                            // create a new tree/node
                            NullTree pageTree = new NullTree(this.m_app);
                            pageTree.RootNode.Action = String.Concat("javascript:go('", url, "');");
                            pageTree.RootNode.Text = label;
    
                            // add the node to the tree
                            tree.Add(pageTree.RootNode);
                        }
    
                    }
                }
            }
        }
    }

    What is happening here is that the XML config file is opened, loop through each of the ChildNodes, then creating new appTrees from them.

    This isn't what I'm using for my WP package, but it's similar.  I haven't tested this code either, just whacked it together as an example of what you could do.  I've used NullTree - just because I didn't know what else to use for this example.

    Good luck, Lee.

  • Robert J. Bullock 386 posts 405 karma points
    Jan 27, 2010 @ 23:02
    Robert J. Bullock
    0

    Cool, actually got it working! Thanks for the ideas... I borrowed some code from another project I found that seemed to handle the tree build better.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 27, 2010 @ 23:52
    Lee Kelleher
    0

    >> code from another project I found that seemed to handle the tree build better

    Sounds interesting... care to share?

  • Robert J. Bullock 386 posts 405 karma points
    Jan 28, 2010 @ 15:03
    Robert J. Bullock
    0

    I'll post the code here in a bit. 

  • Robert J. Bullock 386 posts 405 karma points
    Jan 30, 2010 @ 01:04
    Robert J. Bullock
    2

    Tree code:

    using System;
    using System.Collections.Generic;
    using System.Web;
    using umbraco.cms.presentation.Trees;
    using umbraco.BusinessLogic.Actions;
    using umbraco.interfaces;
    using System.Text;
    using System.Xml;
    using System.IO;
    
    namespace CustomUmbracoSection.Trees
    {
        public class LoadCustomTree : BaseTree
        {
            public LoadCustomTree(string application) : base(application) { }
    
            protected override void CreateRootNode(ref XmlTreeNode rootNode)
            {
                rootNode.Icon = FolderIcon;
                rootNode.OpenIcon = FolderIconOpen;
                rootNode.NodeType = "init" + TreeAlias;
                rootNode.NodeID = "init";
            }
    
            protected override void CreateRootNodeActions(ref List<IAction> actions)
            {
                actions.Clear();
                //actions.Add(ActionNew.Instance);
                actions.Add(ActionRefresh.Instance);
            }
    
            protected override void CreateAllowedActions(ref List<IAction> actions)
            {
                actions.Clear();
                actions.Add(ActionRefresh.Instance);
                actions.Add(ActionNew.Instance);
                //actions.Add(ActionDelete.Instance);
            }
    
            public override void Render(ref XmlTree tree)
            {
                String configFile = HttpContext.Current.Server.MapPath("~/config/customPages.config");
    
                if (File.Exists(configFile))
                {
                    XmlDocument configDoc = new XmlDocument();
                    configDoc.Load(configFile);
    
                    if (configDoc != null)
                    {
                        XmlElement root = configDoc.DocumentElement;
    
                        foreach (XmlNode node in root.ChildNodes)
                        {
                            if (node.Name.ToUpper() == "PAGE")
                            {
                                if ((this.m_id == -1) && !this.IsDialog)
                                {
                                    String label = node.Attributes["label"].Value;
                                    String url = node.Attributes["url"].Value;
    
                                    XmlTreeNode xNode = XmlTreeNode.Create(this);
                                    xNode.NodeID = node.Attributes["id"].Value;
                                    xNode.Text = label;
                                    xNode.Action = String.Concat("javascript:openCustom('", url, "');");
                                    xNode.Icon = "doc.gif";
                                    tree.Add(xNode);
                                }
    
                            }
                        }
                    }
                }
            }
    
            public override void RenderJS(ref StringBuilder Javascript)
            {
                Javascript.Append(
                   @"
                        function openCustom(url) {
                            parent.right.document.location.href = url;
                        }
                    ");
            }
        }
    }

    XML file:

    <?xml version="1.0" encoding="utf-8" ?>
    <pages>
    <page id="1" label="Physicians" url="/umbraco/lourdes/physicians.aspx"/>
    <page id="2" label="Babies" url="/umbraco/lourdes/babies.aspx"/>
        <page id="3" label="Media Log" url="/umbraco/lourdes/medialog.aspx"/>
        <page id="4" label="Test Page" url="/umbraco/lourdes/physicians.aspx"/>
        <page id="5" label="Test Page" url="/umbraco/lourdes/physicians.aspx"/>
    </pages>

Please Sign in or register to post replies

Write your reply to:

Draft