Copied to clipboard

Flag this post as spam?

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

  • Darryl 31 posts 112 karma points
    Nov 21, 2018 @ 18:47

    Umbraco.TypedContentAtXPath returning null in Macro Partial

    I have a node called NewsArea that has NewsItem child nodes in a Macro Partial that is rendered in the NewsArea template.

    My code was working using this line of code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web.Models
        var newsItems = Model.Content.AncestorOrSelf("homePage").Descendants("newsItem").Take(numberOfItems);

    But to make the site more efficient, I changed it to this:

    var newsItems = Umbraco.TypedContentAtXPath("//NewsItem").Take(numberOfItems);

    By the way, numberOfItems is a property that is retrieved from the Macro property. Basically, I am now not getting any IPublishedContent items when debugging, and thus none are being rendered in the view.

  • Marc Goodson 2149 posts 14354 karma points MVP 9x c-trib
    Nov 21, 2018 @ 23:32
    Marc Goodson

    Hi Darryl

    The xpath will be case sensitive, so is your alias NewsItem or newsItem?

    would changing to this:

    var newsItems = Umbraco.TypedContentAtXPath("//newsItem").Take(numberOfItems);

    make a difference for you?

    regards marc

  • Darryl 31 posts 112 karma points
    Nov 22, 2018 @ 18:20

    Thanks Marc,

    I'm not sure what happened yesterday, but its working now. Yes, newsItem is the alias, but I thought it used the template name NewsItem. I also found that the XML node names can be found in the umbraco.config.

  • Marc Goodson 2149 posts 14354 karma points MVP 9x c-trib
    Nov 22, 2018 @ 18:26
    Marc Goodson

    HI Darryl

    Yes, it's using the alias, if you look in the umbraco.config xml file you'll find the newsItem entry and see if it's lowercase or not...

    ... also using //newsItem is great if the news items are scattered all over the site, but if they are in one folder then specifying the path from the root will be much more performant eg


    or similary might be worth playing around with.

    but glad it's working!



  • Darryl 31 posts 112 karma points
    Nov 22, 2018 @ 18:39

    Thanks again Marc for the further insight :-)

    I will actually implement that as they are in a list view underneath a doctype named newsArea and not scattered all over the site. newsArea actually lives under the doctype homePage, so I will probably be looking at somthing like: homePage/newsArea/newsItem. I also like how you can find items with specific properties like //newsItem[type=news].

  • Marc Goodson 2149 posts 14354 karma points MVP 9x c-trib
    Nov 22, 2018 @ 20:56
    Marc Goodson

    No probs, if you want to feel smug about your refactoring, you can use the Miniprofiler that ships with Umbraco

    to measure the time it takes specifically for the line of code that pulls back the news items, and compare the different approaches, to see how much XPath wins by!

    If the site gets 'really big' thousands of news articles, then consider using Examine or XPathNavigator to speed up the queries, there is a good comparison of approaches in this talk by Dave Woestenborghs

  • Darryl 31 posts 112 karma points
    Dec 03, 2018 @ 12:10

    Very good, thanks Marc, that's very informative. When my node tree grows immensely, I will look into these. At the moment my tree is tiny as I am using this particular instance to learn Umbraco from scratch.

Please Sign in or register to post replies

Write your reply to: