Check if a media item still exists in the media library
Hi all,
I have an Umbraco page where I am using a single media picker for an image. This allows the user to select an image so that it can be displayed on the page. It also allows me to programmatically add a media item (by id) to the field so that the media item I have selected programmatically is displayed.
My problem is that my code breaks when a media item is deleted or removed and the ID is left in the media picker which is what happens in Umbraco by default.
So for example, I go into the back office and select an image I want to use on my page. I go to the page and the image is displayed. I then go and delete the image in the media library and try and access the page. I am then presented with the following error:
Object reference not set to an instance of an object.
This is because it is trying to grab the image that previously existed using the old redundant ID but it cannot do so because the image no longer exists. I want to avoid this error being displayed as it essentially breaks the page (YSOD). I therefore need to perform some form of check to see if an image with a specified ID exists before trying to retreive it. How do I go about this?
My current code is as follows and is within the template of my page:
I've looked at numerous other posts on these forums which all seem to suggest using something along the lines of:
var imageNode = Library.MediaById(node.getProperty("sectionImage").Value);
however, for some reason Library is unavailable to me (does not appear in Visual Studio 2013 intellisense). My template inherits the following only:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
How do I ammend my template to be able to use this Library method?
If this method is outdated, what is the best method to use to check whether the media exists before trying to retreive it?
I know that this seems like an odd situation but it is mainly to counteract user error.
Check if a media item still exists in the media library
Hi all,
I have an Umbraco page where I am using a single media picker for an image. This allows the user to select an image so that it can be displayed on the page. It also allows me to programmatically add a media item (by id) to the field so that the media item I have selected programmatically is displayed.
My problem is that my code breaks when a media item is deleted or removed and the ID is left in the media picker which is what happens in Umbraco by default.
So for example, I go into the back office and select an image I want to use on my page. I go to the page and the image is displayed. I then go and delete the image in the media library and try and access the page. I am then presented with the following error:
This is because it is trying to grab the image that previously existed using the old redundant ID but it cannot do so because the image no longer exists. I want to avoid this error being displayed as it essentially breaks the page (YSOD). I therefore need to perform some form of check to see if an image with a specified ID exists before trying to retreive it. How do I go about this?
My current code is as follows and is within the template of my page:
The code errors at this line:
I've looked at numerous other posts on these forums which all seem to suggest using something along the lines of:
however, for some reason Library is unavailable to me (does not appear in Visual Studio 2013 intellisense). My template inherits the following only:
How do I ammend my template to be able to use this Library method? If this method is outdated, what is the best method to use to check whether the media exists before trying to retreive it?
I know that this seems like an odd situation but it is mainly to counteract user error.
I am using the latest version of Umbraco.
Any help would be greatly appreciated.
Hi Jason,
You need to do a null check, e.g.
Jeavon
Something along the lines of this will return DynamicNull if the media item does not exist:
string imageExists = Umbraco.Media(Model.Content.GetPropertyValue("lobbyImage")).GetType().ToString();
is working on a reply...