Copied to clipboard

Flag this post as spam?

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


  • Steve Kromer 11 posts 121 karma points
    Jul 12, 2022 @ 13:04
    Steve Kromer
    0

    View YSOD

    Within views, I'm retrieving content using the below method. However, if the selected content becomes unpublished, I'm receiving the yellow screen of death with the exception "Object reference not set to an instance of an object.". Is there a better way to retrieve content from a model to avoid this from happening?

    IPublishedContent B2C = Umbraco.Content(Home.GetProperty("b2c").Value

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 12, 2022 @ 14:21
    Huw Reddick
    0

    you could check if it has a value before trying to use it.

  • Steve Kromer 11 posts 121 karma points
    Jul 12, 2022 @ 14:24
    Steve Kromer
    0

    I tried using if (Home.HasProperty("b2c")) {}, but the issue is that this check returns true because it does have that property assigned with a value, the content value is just unpublished.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 12, 2022 @ 15:30
    Huw Reddick
    0

    Try checking the IPublishedContent.IsDraft property as in

    if (!item.IsDraft) { }

  • Johannes Lantz 156 posts 839 karma points c-trib
    Jul 12, 2022 @ 19:30
    Johannes Lantz
    100

    Hi there,

    From my experiences .HasValue() dosen't really work on IPublishedContent. You would have to create a variable and do a null check.

    Maybe something like this would work then?

    var B2C =Home.GetProperty("b2c");
    if(B2C != null)
    {
      Umbraco.Content(B2C.Value);
    }
    

    Since I don't know that much about what you are trying to achieve. I am just guessing you are tying to get the value from a content picker? Couldn't you do something like this then?

    var B2C = Home.Value<IPublishedContent>("b2c");
    if(B2C != null)
    {
       B2C.Name
    }
    

    Hope this helps.

    //Johannes

Please Sign in or register to post replies

Write your reply to:

Draft