Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Jun 17, 2014 @ 18:11
    Jason Espin
    1

    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:

     @if (Model.Content.HasValue("lobbyImage"))
     {
    
       IPublishedContent lobbyImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue("lobbyImage"));
        <div class="col-md-6 col-xs-6 taggedImage">
            <img src="@lobbyImage.Url" alt="@lobbyImage.Name" class="img-responsive img-thumbnail">
        </div>
     }  
    

    The code errors at this line:

    IPublishedContent lobbyImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue("lobbyImage"));
    

    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.

    I am using the latest version of Umbraco.

    Any help would be greatly appreciated.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 17, 2014 @ 18:17
    Jeavon Leopold
    1

    Hi Jason,

    You need to do a null check, e.g.

    @if (Model.Content.HasValue("lobbyImage"))
    {
    
        var lobbyImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue("lobbyImage"));
        if (lobbyImage != null)
        {
            <div class="col-md-6 col-xs-6 taggedImage">
                <img src="@lobbyImage.Url" alt="@lobbyImage.Name" class="img-responsive img-thumbnail">
            </div>
        }
    }
    

    Jeavon

  • Jason Espin 368 posts 1335 karma points
    Jun 17, 2014 @ 18:17
    Jason Espin
    102

    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();

Please Sign in or register to post replies

Write your reply to:

Draft