Copied to clipboard

Flag this post as spam?

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


  • julius 107 posts 289 karma points
    Dec 30, 2013 @ 15:34
    julius
    0

    How to get the root node in Umbraco 6/7

    Hey, I am trying to get the root node in Umbraco 7 (case should be the same for Umbraco 6).

    The suggestions I found here don't work.

    http://stackoverflow.com/questions/11940537/umbraco-finding-root-node-in-c-sharp

    UmbracoHelper.TypedContent(-1) returns null

    Umbraco.Content(-1) returns an empty 'dynamic'

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
        var rootNode = umbracoHelper.TypedContentSingleAtXPath("//root"));

    ..results in the exception "ContentTypeService failed to find a content type with alias "root"".

     

    Any ideas? Are we supposed to still use the Node(-1) method for this? I thought "Node" is a thing from the past.

    Are we supposed to access the root node at all in Umbraco 6/7?

    On an different but related note: I think it would be a good idea to agree on one name for a Node/Item/Page/Content and work towards consistency in the API on this point, because it is becoming hard to distuingish between all the terms that are used for what was called a Node in Umbraco 4 and is now called Content in the Content API, but a CurrentPage when we are in a Controller or Razor. This is especially confusing for beginners...

  • Andy Butland 422 posts 2333 karma points MVP 4x hq c-trib
    Dec 30, 2013 @ 16:29
    Andy Butland
    7

    You can use this:

    Umbraco.TypedContentAtRoot().First();

    I think the idea is more that you access the content nodes at the root (maybe more than one) rather than a single root node per se.  But from that you can get the first one.

    Andy

  • julius 107 posts 289 karma points
    Dec 30, 2013 @ 16:38
    julius
    0

    Hi Andy,

    I think this is indeed the case and that Umbraco has seen an undocumented (or I didn't find it yet) paradigm shift in how the node tree is accessed.

    In pre-Umbraco 6 we would often get the root node through the node API by using Node(-1)

    I have tried every possible way of finding the root node in U6/7 but haven't been able to get hold of it. 

    I also tried this:

    var publishedContent = _currentNode.Ancestor(1).Ancestor();

    but this results in null.

    I think we are now supposed to use the TypedContentAtRoot nodes or base our searches on the current node. This does make it harder to search the entire tree which ius exactly want I want to implement in a separate class library I am building for retrieving content from the Umbraco tree.

     

     

     

  • Comment author was deleted

    Dec 30, 2013 @ 20:20

    FWIW, Umbraco best practice is to build a site with a single root node.

    i.e.

    Content

    - Home

    -- Node

    -- Node

    as opposed to

    Content

    - Node

    - Node

    - Node

    If you have multiple sites on one install, it should look like this:

    - Sites

    -- Home 1

    --- Node

    -- Home 2

    --- Node

     

    Therefore if you do an Ancestor(1), you'll get the the top level node which you can then search.

  • Charles Afford 1163 posts 1709 karma points
    Dec 30, 2013 @ 21:14
    Charles Afford
    0

    I would start getting root node using typed content and Xpath expressions.  Its just messy imo.  If you need to get Nodes on the same level as Level 1 you can do i think its:

    siblings  > where doc type = your nodes doc type.

    Hope this helps.  Charlie :)

  • julius 107 posts 289 karma points
    Dec 30, 2013 @ 21:34
    julius
    101

    @Kevin: I see what you mean, but I am creating a library that I want to work with any tree, so I don't want to impose a particular way of working for my lib to work :)

    I found a strategy where the client programmer can decide whether he/she wants to start search the current branch (ancestors and descendants of the current node), just the descendants of the current node or the entire tree. Works for me :)

    private IEnumerable<IPublishedContent> GetSearchStartNodes(SearchBehaviour behaviour)
    {
    var searchStartNodes = new List<IPublishedContent>();

    if (behaviour.Equals(SearchBehaviour.EntireTree))
    {
    searchStartNodes = _helper.TypedContentAtRoot().ToList();
    }
    else if(behaviour.Equals(SearchBehaviour.CurrentBranch))
    {
    searchStartNodes.Add(_currentNode.AncestorOrSelf(1));
    }
    else if (behaviour.Equals(SearchBehaviour.DescendantsOrSelf))
    {
    searchStartNodes.Add(_currentNode);
    }
    else
    {
    throw new UmbracoDataException(string.Format("Behaviour called '{0}' is not recognized", behaviour));
    }

    return searchStartNodes;
    }

     

     

  • Comment author was deleted

    Dec 30, 2013 @ 21:37

    Great!

  • David Dimmer 76 posts 134 karma points
    Mar 09, 2014 @ 00:23
    David Dimmer
    3

    Get nodes at root level in Umbraco 7:

    Umbraco.TypedContentAtRoot().First();
    

    Credit to Andy Butland.

    My solution:

    <ul>
            @{                                        
            foreach (var languages in @Umbraco.TypedContentAtRoot())
            {
                <li>
                <a href="@languages.GetProperty("frontEndUrl").Value">@languages.Name</a>
            </li>
            }}
        <li>
           <a href="@(CurrentPage.AncestorOrSelf().FrontEndUrl)Umbraco/ClearCache" >Clear Cache and Reload</a>
         </li>
    </ul>
    

    Fyin.com: Chicago Umbraco Developers

  • Trevor Kilvington 1 post 20 karma points notactivated
    Jan 13, 2017 @ 04:53
    Trevor Kilvington
    0

    Hi,

    Git is trying to commit files in the App_data folder that are not included in the .gitignore that you provided. Are we able to ignore these files, get rid of them somehow, or should we be committing them with our changes?

    enter image description here

  • John B. 2 posts 72 karma points
    Oct 26, 2018 @ 12:10
    John B.
    0

    This was a part of my solution to getting the correct node for a multilanguage site in Umbraco 7

                    var pathList = Node.GetCurrent().Path.Split(',');
                    int domainNodeId = -1;
                    if (int.TryParse(pathList[1], out domainNodeId))
                        domainNode = new Node(domainNodeId);
    
Please Sign in or register to post replies

Write your reply to:

Draft