in

Platinum Bay

Peace, Love, and...

This Blog

Syndication


.NETicated

April 2011 - Posts

  • Modifying Tree Node Text in the Umbraco Backend

    I’m currently working on rebuilding the http://dotnetda.org site using Umbraco and I have created the usual assortment of document types: speakers, meetings, sponsors, etc. For meetings, the name of the node is simply the session title. Unfortunately, this means that finding a meeting requires knowing the session title and I want the node text to be more descriptive and include the date as well. I can use the Umbraco API and write a little code to do this for me.

    To start, I added the following using statements:

    using System;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.presentation.Trees;

    Then, I created a class that inherits from ApplicationBase. By simply inheriting from ApplicationBase, Umbraco will automatically load the class during initialization:

    public class TreeEvents : ApplicationBase
    {
    }

    Next, I added a TreeEvents constructor and handled the BaseTree.BeforeNodeRender event:

    public TreeEvents()
    {
        BaseTree.BeforeNodeRender += BaseTree_BeforeNodeRender;
    }

    And finally I implemented the BaseTree_BeforeNodeRender method with the necessary logic to update the node’s Text property if it’s a meeting:

    void BaseTree_BeforeNodeRender(
        ref XmlTree sender, ref XmlTreeNode node, EventArgs e)
    {
        if (node.NodeType == "content")
        {
            var content = new Content(int.Parse(node.NodeID));
    
            if (content.ContentType.Alias == "Meeting")
            {
                var startDate = content.getProperty("meetingStartDate")
                    .Value.ToString();
    
                var date = DateTime.Parse(startDate);
                node.Text = String.Format("{0:MM/dd/yyyy} {1}",
                    date,
                    node.Text
                    );
            }
        }
    }

    First, I ensure that I’m dealing with a content node, then I grab the content item based on NodeID. If the content type alias is Meeting, I parse the meetingStartDate property and prepend it to the node’s existing text.

    The final result is that I can now see the meeting date which will make meetings much easier to find:

    image

    Final Code

    using System;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.presentation.Trees;
    
    namespace Netda.Umbraco
    {
        public class TreeEvents : ApplicationBase
        {
            public TreeEvents()
            {
                BaseTree.BeforeNodeRender += BaseTree_BeforeNodeRender;
            }
    
            void BaseTree_BeforeNodeRender(
                ref XmlTree sender, ref XmlTreeNode node, EventArgs e)
            {
                if (node.NodeType == "content")
                {
                    var content = new Content(int.Parse(node.NodeID));
    
                    if (content.ContentType.Alias == "Meeting")
                    {
                        var startDate = content.getProperty("meetingStartDate")
                            .Value.ToString();
    
                        var date = DateTime.Parse(startDate);
                        node.Text = String.Format("{0:MM/dd/yyyy} {1}",
                            date,
                            node.Text
                            );
                    }
                }
            }
        }
    }
    Posted Apr 13 2011, 02:23 PM by Steve with 1 comment(s)
    Filed under:
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.