Copied to clipboard

Flag this post as spam?

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


  • Kyle Jorve 11 posts 94 karma points
    Jun 11, 2019 @ 17:01
    Kyle Jorve
    0

    Umbraco 8 Nested Content always returns null value

    I'm grabbing a nested content item from the home page and assigning it to a variable. I know that I've added values to the item in the back end, but no matter what I try it always returns null. I'm using Umbraco v 8.0.2.

    This is my code:

    // hours
    var homePgID = 1065;
    var hours = Umbraco.Content(homePgID).Value<IEnumerable<IPublishedContent>>("busHours").ToList();
    
  • Rhys Hamilton 140 posts 942 karma points
    Jun 13, 2019 @ 15:33
    Rhys Hamilton
    100

    The reason you're getting a null exception is because you're returning IPublishedContent, instead of IPublishedElement.

    Something like the following, should work:

    var hours = Umbraco.Content(homePgID).Value<IEnumerable<IPublishedElement>>("busHours").ToList();
    

    You can find out more about how to use Nested Content here.

  • Kyle Jorve 11 posts 94 karma points
    Jun 13, 2019 @ 15:36
    Kyle Jorve
    1

    Thanks Rhys! That seems to have worked. Would changing it to IPublishedElement affect it negatively if there were more than one item in the nested content?

  • Rhys Hamilton 140 posts 942 karma points
    Jun 13, 2019 @ 15:51
    Rhys Hamilton
    0

    Glad that sorted it!

    Yeah - if you only type IPublishedElement when trying to return multiple items, you'll most likely encounter issues.

    By default, I would always recommend that you use IEnumerable<IPublishedElement> since most Nested Content will likely be trying to retrieve a list of IPublishedElement entities (multiple items).

  • Kyle Jorve 11 posts 94 karma points
    Jun 13, 2019 @ 16:05
    Kyle Jorve
    1

    Awesome, thank you very much for your help!

Please Sign in or register to post replies

Write your reply to:

Draft