Copied to clipboard

Flag this post as spam?

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


  • Jan Hansen 7 posts 27 karma points
    Apr 30, 2015 @ 15:40
    Jan Hansen
    0

    How to access root level nodes used for configuration

    Umbraco version: 7.2.4

    We've used root level nodes as placeholders for global settings as well as language-specific settings - but I cant see how to access these pages in a reasonable manner using the ... well... sparsely documented razor api.

    Our content tree looks a bit like:

    UK site (document type "Home")
    - subpage 1
    - subpage 2
    DK site (document type "Home")
    Global settings (document type "Global Settings")
    UK settings (document type "Site Settings")
    DK settings (document type "Site Settings")

    When I render the master template, I'd like to access settings like

    var globalSettings = CurrentPage.AncestorsOrSelf(0).Where(docmenttype="GlobalSettings")

    and

    var siteSettings = <some query that takes into account the selected language for the page we're on>

    I've been able to get the global settings node like this

    var gs = Umbraco.ContentSingleAtXPath("//GlobalSettings");

    - but it looks like a hack, and I can't figure out how to expand it to get the language-specific settings node.

    When I look at the "documentation" I constantly hit issue after issue with samples that were for v6 or the like, and even https://our.umbraco.org/DOCUMENTATION/Reference/Querying/UmbracoHelper/ which should be for v7 seem to be misleading. One of the samples is

    @foreach (var child in Umbraco.ContentAtRoot().Children) { 
        <a href="@child.Url">@child.Name</a>
    }

    - which on my machine gives this

    Server Error in '/' Application.

    'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Children'

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Children'

     

    Can anybody help finding the nodes - or point me to the documentation? 

    Thanks in advance :)

    /Jan

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    May 08, 2015 @ 12:44
    Sebastiaan Janssen
    0

    There's no children because ContentAtRoot() is returning a list of content (all of the content items in the root). It's easier to spot these things is you use TypedContent, as you can see in the code completion in (for example) WebMatrix what you'll get back.

    enter image description here

    So this should work:

    @foreach (var child in Umbraco.TypedContentAtRoot()) { 
        <a href="@child.Url">@child.Name</a>
    }
    

    Docs for that are here: https://our.umbraco.org/documentation/reference/querying/umbracohelper/#TypedContentAtRoot()

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    May 08, 2015 @ 13:38
    Alex Skrypnyk
    0

    It's better do not use like that :

    CurrentPage.AncestorsOrSelf(0).Where(docmenttype="GlobalSettings")
    

    Better will be as said Sebastiaan - Umbraco.TypedContentAtRoot()

    Thanks

  • Michael Nielsen 153 posts 810 karma points
    Jul 07, 2015 @ 18:44
    Michael Nielsen
    0

    If you look at the examples given for .ContentAtRoot() and .TypedContentAtRoot() in Sebastiaans link, they look like this

    @foreach (var child in Umbraco.TypedContentAtRoot().Children) { 
        <a href="@child.Url">@child.Name</a>
    }
    

    Giving the impression that .Children is possible, when it is not

    I've created an issue to address that.

Please Sign in or register to post replies

Write your reply to:

Draft