Copied to clipboard

Flag this post as spam?

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


  • Tobias Nylin 24 posts 117 karma points
    Jul 20, 2016 @ 08:16
    Tobias Nylin
    0

    Umbraco.TypedContent throws null exception when passing Id which belongs to a Media object

    Hi! I'm building a search page where it should work to search for both content and media. For retrieving content I use Umbraco.TypedContent.

    My initial idea was to use Umbraco.TypedMedia to get the media file if TypedContent returns null. However, TypedContent throws a null exception when I pass an Id which belongs to a media file. I expected it to return null. I have double checked so that the Id itself isn't null. This is my code:

    var node = Umbraco.TypedContent(result.Id);
    

    If I use the overloaded version of TypedContent which takes an array of string it works, and the return value of TypedContent gets "null" instead of throwing an exception:

    var resultId = result.Id.ToString().Split(',');
    var content = Umbraco.TypedContent(resultId);
    

    Is this a bug or am I thinking wrong?

    Thanks in advance!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 20, 2016 @ 08:46
    Alex Skrypnyk
    0

    Hi Tobias,

    What version of Umbraco are you using?

    In 7.4.3 it works fine, TypedContent returns null when node doesn't exist.

    Thanks,

    Alex

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Jul 20, 2016 @ 08:55
    Dennis Adolfi
    100

    Hi Tobias.

    @Umbraco.TypedContent(int) should return null if it cant find a content node with that id, even if the id provided belongs to an media item.

    As a test, I tried the following in Umbraco v.7.4.2 and it works fine:

    @{
        var id = 1067; <!-- Id of a Media item
        var content = Umbraco.TypedContent(id);
        if (content == null)
        {
            content = Umbraco.TypedMedia(id);
        }
    }
    
    @content.Name
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 20, 2016 @ 09:00
    Alex Skrypnyk
    1

    Thanks Dennis, the same code works fine in 7.4.3

  • Tobias Nylin 24 posts 117 karma points
    Jul 20, 2016 @ 09:19
    Tobias Nylin
    1

    Hi! I'm using Umbraco version 7.4.3 assembly: 1.0.5948.18141.

    I tried again now, and now it's working, not sure what I made differently. Strange. Sorry to bother you :)

    Have a nice day!

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Jul 20, 2016 @ 09:24
    Dennis Adolfi
    0

    Awesome! Glad it worked out for you!

    You never bother anyone here! :)

    Have a great day, take care!

Please Sign in or register to post replies

Write your reply to:

Draft