Copied to clipboard

Flag this post as spam?

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


  • reinjunon 7 posts 97 karma points
    May 03, 2017 @ 12:42
    reinjunon
    0

    umbraco 7.6 API model issue with typedmedia

    I have the below 2 lines of code. Through debugging, I can examine media and I see it having an id, however when the 2nd line is run, mediaItem returns null.

    var media = Model.GetPropertyValue("Testimage"); var mediaItem = Umbraco.TypedMedia(media);

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    May 03, 2017 @ 13:18
    Alex Skrypnyk
    0

    Hi Reinjunon

    So one time this code works, another doesn't?

    What is the problem?

    Did you change something with this media item in database?

    Thanks,

    Alex

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    May 03, 2017 @ 13:25
    Jeavon Leopold
    105

    Hi,

    Try this:

    var media = Model.GetPropertyValue<int>("Testimage"); 
    var mediaItem = Umbraco.TypedMedia(media);
    

    This is due to media pickers now returning IPublishedContent in v7.6.

    This would actually allow you to simplify to a single line:

    var mediaItem = Model.GetPropertyValue<IPublishedContent>("Testimage"); 
    

    Jeavon

  • Anders Burla 2560 posts 8256 karma points
    May 08, 2017 @ 09:22
    Anders Burla
    0

    And if this code just returns null then what is wrong? :)

    var mediaItem = Model.GetPropertyValue<IPublishedContent>("Testimage"); 
    

    Upgraded 7.5.11 to 7.6 and merged configs correctly. Is using a media picker. Have republished all content. Works with the old code, but not the new.

  • Anders Burla 2560 posts 8256 karma points
    May 08, 2017 @ 09:44
    Anders Burla
    0

    Figured it out. Was because when you upgrade a site Umbraco creates (Obsolete) media picker because it now need to use UID. So when changing the datatype to the MediaPicker2 then your code works :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    May 08, 2017 @ 09:46
    Jeavon Leopold
    0

    Converters work with with both MediaPicker and MediaPicker2

    What returns with:

    GetProperty("Testimage").DataValue
    
  • Anders Burla 2560 posts 8256 karma points
    May 08, 2017 @ 10:01
    Anders Burla
    0

    The id of the image. 1116 in this case

  • reinjunon 7 posts 97 karma points
    May 03, 2017 @ 14:10
    reinjunon
    0

    How do you know this Jeavon? I was trying to use var mediaItem = Umbraco.TypedMedia(media.Id) to no avail...lol

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    May 03, 2017 @ 14:50
    Jeavon Leopold
    2

    :) It's one of the features of v7.6 and I worked on it

    Documentation needs to be migrated from https://github.com/Jeavon/Umbraco-Core-Property-Value-Converters (you can use that for now, this is the default in Umbraco v7.6 now) which is one of the tasks I'm hoping to work on soon.

    There is a configuration option in umbracoSettings.config that allows you to revert it to behave how it used to but I wouldn't recommend it as this is how things will be forever more (it's really for upgrades that won't work easily)

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft