Copied to clipboard

Flag this post as spam?

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


  • Rob 43 posts 79 karma points
    Nov 10, 2012 @ 03:12
    Rob
    0

    Hopefully a simple nodefactory question..

    How would i find the root of the cms via node?

    I.E. if you wanted to build a nested menu starting from the top level of the website and then dropping down two levels how could i find the top level.

    I know i can get parent and also children, but if you are 5 levels down or 6 levels down how can you reliably get to the top level?

     

    Thanks in advance

     

    Rob

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 10, 2012 @ 13:04
    Jan Skovgaard
    0

    Hi Rob and welcome to our :)

    I guess the answer to this question depends on how you're thinking about building the navigation. Are you going to use a XSLT based macro or a Razor based macro?

    Looking forward to hearing from you.

    /Jan

  • Rob 43 posts 79 karma points
    Nov 10, 2012 @ 13:52
    Rob
    0

    Jan, thanks for the welcome.

    To be honest. the XSLT is above my head. What i was planning on doing was building a usercontrol and querying the node factory for nodes up to 3 levels down from the root for an upper level menu.

    The basic goal would be to produce output that works with PURECSSMENU menu styles.

    What i managed to do so far is find the level of the CurrentPage and navigate up the structure by referencing the parent, then traversing back down. E.G.

     

                var level = Node.GetCurrent().Level;
                Node nd1 = Node.GetCurrent();
                for (int i = 0; i < level-1; i++)
                {
                    nd1 = (Node)nd1.Parent;
                }
    
                var sb = new StringBuilder();
                foreach (Node node in nd1.Children)
                {
                    sb.AppendLine("--" + node.Name);
                    foreach (Node nd in node.Children)
                    {
                        sb.AppendLine("-----" + nd.Name);
                    }
                }
                litMenu.Text = sb.ToString();
    

     It will work, but i was hoping to be able to start at the top and just traverse down.

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 10, 2012 @ 14:44
    Jan Skovgaard
    0

    Hi Rob

    Ok, then I suggest you have a look at the Razor way of doing things instead. Doing a usercontrol to build a menu is not recommended - not that it can't be done but it's just does not make any sense when you can use Razor, which as you probably already know is a subset of #c sharp.

    To give you head start there has already been made some comon snippets that one needs for each project. So if you open Umbraco and go to "Developer -> Script files" and right click and choose "create" you can create a Razor file based on cshtml or vbhtml. Just choose .cshtml and in "choose a template" choose Navigation.

    You can read more about nested navigation with start and end levels here: http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/nested-navigation-with-start-and-finish-levels be aware though that this ressource is describing how Razor works in 4.7 so some things might have changed to the better with the latest release of Umbraco.

    You can probably also benefit from Dan Diplos blog post about using Razor in Umbraco with Visual Studio intellisense etc. here http://www.diplo.co.uk/blog/2011/6/17/using-razor-in-umbraco-47.aspx - Be aware that this is also based on 4.7 but you should still be able to use the concepts.

    Furthermore I think the Razor cheat sheet will probably come in handy to you so go get it here: http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet

    I hope this helps :)

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 10, 2012 @ 14:57
    Jan Skovgaard
    0

    Hi Rob

    Forgot to add the link to the Razor documentation so here it is: http://our.umbraco.org/documentation/Reference/Templating/Macros/Razor/

    /Jan

  • Rob 43 posts 79 karma points
    Nov 10, 2012 @ 15:13
    Rob
    0

    Jan, my thanks.

    I will look over the above links. Re Razor, i am pretty comfortable as i have been using mvc for a while, it is getting at the objects that seem to be the trick. XSLT is way out of my comfort range, so i do like the idea of trying a Razor approach.

     

    Thanks again

     

    Rob

    PS i did get the nodefactory working, but if it is more effecient to use the Razor approach i will work that direction insead.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 10, 2012 @ 15:53
    Jan Skovgaard
    0

    Hi Rob

    You're very welcome. I hope you're going to enjoy working with Umbraco. If you come across something that you don't get don't hesitate to post questions in here since we're a friendly bunch of people who love helping each other out. :)

    Well then Razor is certainly the thing for you - No need to deal with XSLT if it does not make any sense to you at all.

    Glad to hear you got the nodefactory working but I will still recommend you use Razor rather than user controls. And if you choose to go full MVC in Umbraco 4.10...does user controls then make sense? (Not really, right?) ;-)

    /Jan

  • Richard Terris 273 posts 715 karma points
    Nov 12, 2012 @ 14:54
    Richard Terris
    0

    If the root Node is unlikely to change you can add it to web.config

    <add key="Root" value="1225" />

    and use that as you're starting point in C#

    var rootNode = Convert.ToInt32(ConfigurationManager.AppSettings["Root"]));

    But the Razor templates ARE very good!

Please Sign in or register to post replies

Write your reply to:

Draft