Giving Umbraco custom tree’s children!


This was something i was looking for for a while- turns out it is buried deep in their forums;

As shown in their own example (I’ve reblogged here incase the page ever moves or dissapears!!)

Turns out it’s just a case of setting the “source” property of the XmlTreeNode.

public override void Render(ref XmlTree tree)
{
           
            Dictionary.DictionaryItem[] tmp;
        //this.id is set when the tree.aspx creates this object. 
        //It is the nodeID that is passed in via the tree service parameters (i.e. the query string)
        //this checks if the id is the StartNodeID (root node), if it’s not then this will 
        //look up the child nodes for the current dictionary item.
            if (this.id == this.StartNodeID)
                tmp = Dictionary.getTopMostItems;
            else
                tmp = new Dictionary.DictionaryItem(this.id).Children;

            foreach (Dictionary.DictionaryItem di in tmp)
            {
             XmlTreeNode xNode = XmlTreeNode.Create(this);
             xNode.NodeID = di.id.ToString();
             xNode.Text = di.key;
             xNode.Action = string.Format("javascript:openDictionaryItem({0});", di.id);
             xNode.Icon = "settingDataType.gif";
             xNode.OpenIcon = "settingDataType.gif";
             //if there is no children, then set the source to an empty string
//this will ensure that there is no expand button for this node when it is 
//rendered. Otherwise, set the source to the tree service url by using
//the BaseTree’s GetTreeServiceUrl method
             xNode.Source = di.hasChildren ? this.GetTreeServiceUrl(di.id) : "";
             tree.Add(xNode);
            }
}
  1. #1 by Jason on January 25, 2010 - 20:35

    Hey, when I implemented this there was a logic error on the xNode.Source setting.

    The child node needs to have its parent id set as the source.

    Unless I’m reading it wrong (I’m not using dictionaries) I think this line may be wrong:

    xNode.Source = di.hasChildren ? this.GetTreeServiceUrl(di.id) : “”;

    It loos like its setting its parent to itself. However, I don’t have a way to test this specific case.

  2. #2 by Jason on January 25, 2010 - 20:36

    Nonetheless, thanks for the code and the blog. It got me going fast on this problem!

  3. #3 by shawson on January 27, 2010 - 11:10

    Hey Jason,

    Thanks for posting, Glad it helped! To be honest, I’m not sure- I got lucky finding that amongst the masses of disorganised forum posts and I pasted it straight in and it just worked.

    I can’t say for sure if your interpretation of the code is right without knowing what the GetTreeServiceUrl method does; that might all I can say is that I have that same code snippet up and powering a custom “job application” section on the backend of bookpoint.co.uk and all is live and working as expected.

    Its a shame really- as much as I like what the Umbraco guys have done, I found the documentation to he a massive let down to the project.

(will not be published)


I absolutely love these. They came at the lace wigs are good quality, and very soft. Absolutely love these extensions. I just love this hair. Hair came on time, I waited until after hair extensions uk very full, no shedding and it is been installed for 3 weeks now and yes I jus ordered 3 more bundles. Lol. I definitely recommend this company for your bundles ladies. They are soft, hair bundles on the head for human hair wigs and I would continue to recommend them. I have thinner hair so they cover and blend nicely.