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);
@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
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:
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:
Is this a bug or am I thinking wrong?
Thanks in advance!
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
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:
Thanks Dennis, the same code works fine in 7.4.3
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!
Awesome! Glad it worked out for you!
You never bother anyone here! :)
Have a great day, take care!
is working on a reply...