Copied to clipboard

Flag this post as spam?

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


  • ianhoughton 281 posts 605 karma points c-trib
    May 01, 2013 @ 17:59
    ianhoughton
    0

    Check if media item still exists

    I have a media picker on a page and am using this code to show the image:

    @if (@node.HasValue("sectionImage"))
    {
        var imageNode = new Media(Convert.ToInt32(@node.GetProperty("sectionImage").Value));
        if (!string.IsNullOrEmpty(@imageNode.getProperty("crops").Value.ToString()))
        {
            var crop = @Helpers.GetImageCropperUrlFullMedia(@imageNode.getProperty("crops").Value.ToString(), "image124x124");
            <img src="@crop" alt="@node.Name" />
        }
    }

    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 ?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 01, 2013 @ 18:04
    Tom Fulton
    0

    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:

    var imageNode = Library.MediaById(node.getProperty("sectionImage").Value);

    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 

  • Charles Afford 1163 posts 1709 karma points
    May 06, 2013 @ 18:11
    Charles Afford
    0

    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 :)

  • ianhoughton 281 posts 605 karma points c-trib
    May 08, 2013 @ 17:45
    ianhoughton
    0

    Not the most elegant way to check but:

    var invalidMedia = library.GetMedia(favourite.nodeId, true).Current.InnerXml.Contains("error");
    
    if (invalidMedia == false)
    {
        favouriteList.Add(favourite);
    }
  • Charles Afford 1163 posts 1709 karma points
    May 09, 2013 @ 21:20
    Charles Afford
    0

    Dont do it that way ian

  • ianhoughton 281 posts 605 karma points c-trib
    May 10, 2013 @ 09:38
    ianhoughton
    0

    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.

  • Charles Afford 1163 posts 1709 karma points
    May 10, 2013 @ 10:18
    Charles Afford
    0

    You should just be able to check if the object is null so:

    if(Library.MediaById(node.getProperty("sectionImage")) != null)
    {
    MEDIA FOUND FOUND FOR MEDIA ID
    }
    else
    {
    MEDIA NOT FOUND FOR MEDIA ID
    }
  • ianhoughton 281 posts 605 karma points c-trib
    May 10, 2013 @ 10:27
    ianhoughton
    0

    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.

  • Charles Afford 1163 posts 1709 karma points
    May 10, 2013 @ 11:13
    Charles Afford
    0

    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

    if(Library.MediaById(node.getProperty("sectionImage"))!=null)
  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 13, 2013 @ 20:25
    Tom Fulton
    0

    Did you get this figured out?  If I recall, it might return a DynamicNull type, so something like this might need to be done:

    var media = Library.MediaById(node.getProperty("sectionImage"));
    if (media.GetType() != typeof(DynamicNull) {
    ....
    }

    You might also try checking that the Id is > 0 (I seem to remember this working for some reason)

    if (media.Id > 0) {
    ...
    }

    @Charlie - var is just a personal preference, doesn't change how the code works one way or another :)

    -Tom 

Please Sign in or register to post replies

Write your reply to:

Draft