Copied to clipboard

Flag this post as spam?

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


  • Ben Palmer 176 posts 842 karma points c-trib
    Dec 02, 2017 @ 21:33
    Ben Palmer
    0

    Custom Tree Controllers with Different Views

    Hi,

    I'm attempting to create a package that provides documentation via a custom section in the Umbraco back office.

    I've been playing around with tree controllers but I'm struggling to group things and have separate views for each item.

    Here's my code, first up I'm registering a new application like so:

    using umbraco.businesslogic;
    using umbraco.interfaces;
    
    namespace TheUnofficialUmbracoUserGuide.Applications
    {
        [Application("TheUnofficialUmbracoUserGuide", "User Guide", "icon-books", 15)]
        public class TheUnofficialUmbracoUserGuide : IApplication
        {
        }
    }
    

    Next I'm using a tree controller to register my menu items:

    using System.Net.Http.Formatting;
    using Umbraco.Web.Models.Trees;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.Trees;
    
    namespace TheUnofficialUmbracoUserGuide.Trees.Basics
    {
        [PluginController("TheUnofficialUmbracoUserGuide")]
        [Tree("TheUnofficialUmbracoUserGuide", "Basics", "Basics", "icon-doc")]
        public class BasicsTreeController : TreeController
        {
            protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
            {
                var nodes = new TreeNodeCollection();
    
                if (id == "-1")
                {
                    nodes.Add(CreateTreeNode("basics", id, queryStrings, "Basics", "icon-book", true));
                }
    
                if (id == "basics")
                {
                    nodes.Add(CreateTreeNode("login", "basics", queryStrings, "Login", "icon-book", false));
                }
    
                return nodes;
            }
    
            protected override MenuItemCollection GetMenuForNode(string id, FormDataCollection queryStrings)
            {
                return new MenuItemCollection();
            }
        }
    }
    

    I have a view file at ~/App_Plugins/TheUnofficialUmbracoUserGuide which gets loaded but it seems to be used for both Basics, and Login in my menu structure (which now looks like the below image).

    enter image description here

    So my question is, how do I group my content yet still have totally separate views? Ideally I'd like to have a structure like the below example:

    Basics
        Login
        Resetting Your Password
    Content Editing
        Adding Content
        Deleting Content
    

    Thanks in advance!

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Dec 02, 2017 @ 21:53
    Kevin Jump
    101

    Hi

    first thing to say it's that is probably worth pointing out that there is an existing package that pretty much does what you are looking for.

    https://our.umbraco.org/projects/backoffice-extensions/bookshelf/ (github : https://github.com/kgiszewski/UmbracoBookshelf)

    this allows you to build documentation like books and display them in a custom section.

    but - when building a custom tree.

    you can specify the view to be displayed as an additional argument of the CrateTreeNode function:

    CreateTreeNode("login", "basics", queryStrings, "Login", "icon-book", false, "path/to/view")
    

    the path needs to share the namespace with your tree, and Umbraco will add a back office folder. so for example:

    TheUnofficialUmbracoUserGuide/Basics/basics/-1

    might go to a file in app_plugins/TheUnofficalUmbracoUserGuide/Backoffice/Basics/basics.html

    if you path doesn't match your tree contraller attributes then umbraco will actuall start to look in /views/umbraco so you have to keep things in there so in your case i think everything will have to live in the backoffice/basics folder but you can have diffrent 'views' that all go to diffrent html pages so login might be

    TheUnofficialUmbracoUserGuide/Basics/login/

    pointing at a file App_Plugins/...../backoffice/basics/login.html

    Kevin

  • Ben Palmer 176 posts 842 karma points c-trib
    Dec 03, 2017 @ 09:42
    Ben Palmer
    0

    Thanks Kevin,

    Good to know about bookshelf, I'll check it out.

    In the meantime, this has done the trick nicely. I ran in to the extra argument earlier but had thought it only changed the route, must not have got my view path right.

    Thanks again for the help!

Please Sign in or register to post replies

Write your reply to:

Draft