Tom i have a quick question and wonder if i am doing something wrong. When people use the 'var' keyword rather than the type. Why is this? Should i be using var? Thanks. Charlie :)
The problem is that "sectionImage" is a media picker. If someone has picked an image, but then later on that image is deleted from the media section, then "sectionImage" still holds the id of the non-existant media item.
So checking "sectionImage" would return true as it has an ID, but then would fail as Umbraco cannot find the actual image.
Check if media item still exists
I have a media picker on a page and am using this code to show the image:
The problem is, that if the client has deleted the media item, but leaves the id in the media picker, then the code above fails on the new Media call.
What would be the best way to check if the media items still exists ?
Hi,
Firstly I think you shouldn't be using the Media API as it's not meant for front-end use (slow). Instead I think you should use:
Then from there you should be able to check if imageNode == null, or maybe imageNode.GetType() == typeof(DynamicNull) or something along those lines.
Hope this helps,
Tom
Tom i have a quick question and wonder if i am doing something wrong. When people use the 'var' keyword rather than the type. Why is this? Should i be using var? Thanks. Charlie :)
Not the most elegant way to check but:
Dont do it that way ian
Hi Charles, how would you suggest ?
Checking if a node exists is easy, you just check the name property, but I struggled with media items.
You should just be able to check if the object is null so:
{
MEDIA FOUND FOUND FOR MEDIA ID
}
else
{
MEDIA NOT FOUND FOR MEDIA ID
}
The problem is that "sectionImage" is a media picker. If someone has picked an image, but then later on that image is deleted from the media section, then "sectionImage" still holds the id of the non-existant media item.
So checking "sectionImage" would return true as it has an ID, but then would fail as Umbraco cannot find the actual image.
I belive it would return false as it would not be able to create an new instance of the Media for the given id parameter.
I am not checking the ID or MediaItem but wether a new media object was succesfully instantiated or not.
Could be wrong!
Charlie :)
*Just seen my typo: Now checking the Media object and not "sectionImage" property
Did you get this figured out? If I recall, it might return a DynamicNull type, so something like this might need to be done:
You might also try checking that the Id is > 0 (I seem to remember this working for some reason)
@Charlie - var is just a personal preference, doesn't change how the code works one way or another :)
-Tom
is working on a reply...