Copied to clipboard

Flag this post as spam?

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


  • Morten 105 posts 345 karma points
    May 09, 2017 @ 10:34
    Morten
    0

    Getting image URL from Media Picker not working in 7.6

    This is what I usually do, if I want to grab an image's URL:

    var selection = Umbraco.Content(1234).Children;
    
    foreach(var person in selection) {
        IPublishedContent vorto = (IPublishedContent)person;
        var img = Umbraco.Media(vorto.GetPropertyValue("image"));
    
        <img src="@img.Url" />
    }
    

    This worked until I upgraded to 7.6 a few minutes ago. Vorto values work fine and is grabbed fine from the database, it's simply not converting anymore. The image property is not a Vorto value, but Vorto can automatically grab the translated value or the default one (if not Vorto).

    I have tried this as well:

    var img = Umbraco.Media(person.GetPropertyValue("image"));
    

    But it's the same deal.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 09, 2017 @ 10:42
    Dave Woestenborghs
    2

    Hi Morten,

    In v7.6 this package got integrated in to the core : https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/

    This means that now out of the box property values will be converted to some strongly typed model.

    If you want you can disable this feature in the umbracoSettings.config file. Look for this setting and set it to false :

    <EnablePropertyValueConverters>true</EnablePropertyValueConverters>
    

    But if you want to use the value converters you can use this code.

    var selection = Umbraco.Content(1234).Children;
    
    foreach(var person in selection) {
        IPublishedContent vorto = (IPublishedContent)person;
        var img = vorto.GetVortoValue<IPublishedContent>("image");
    
        <img src="@img.Url" />
    }
    

    Dave

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 11:30
    Morten
    1

    Hi Dave,

    It doesn't matter if I set EnablePropertyValueConverters to true or false, I will get this exception:

    System.NullReferenceException: Object reference not set to an instance of an object

    Whenever I try to run the code you posted. Any idea what I can try next? Also, I'm looking for the best way to do this, not the best workaround :-)

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 09, 2017 @ 11:34
    Dave Woestenborghs
    0

    Hi Morten,

    Setting it to false should make your code work.

    If you have it my code snippet should work.

    But haven't tried using vorto in 7.6 yet I must admit.

    Dave

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 11:38
    Morten
    0

    Unfortunately it does not seem to work. I get a null reference exception. I copied your example character for character. The other Vorto values work fine though.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 09, 2017 @ 11:45
    Dave Woestenborghs
    0

    Which line is giving you this error ?

    Just remove them, and add them one by one.

    Dave

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 11:51
    Morten
    0
    var img = vorto.GetVortoValue<IPublishedContent>("image");
    

    However, this works perfectly:

    var name = vorto.GetVortoValue("name")
    

    Something tell me the conversion to IPublishedContent doesn't go well.

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 11:52
    James Strugnell
    0

    I'm currently having a similar looking problem. I'm trying to upgrade a 7.3.1 site to 7.6.0 but I've had lots of trouble with code that interacts with URL and media fields. I've been tinkering with settings, enabling/disabling the "enablePropertyValueConverters" setting and trying other things.

    For me the following code is not working:

    @foreach (IPublishedContent childPage in thisPageAlbums)
    {
        string albumCoverImage = string.Empty; 
        if (childPage.HasValue("mainImage"))
        {
            albumCoverImage = childPage.GetPropertyValue<IPublishedContent>("mainImage").Url;
        }
    }
    

    Although the "hasValue" check returns true the GetPropertyValue method returns null.

    Any ideas? Is there some official documentation on how these underlying changes should be handled?

    Thanks

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 11:58
    James Strugnell
    1

    Straight after posting the above I checked the "mainImage" data type and it is a Multiple Media Picker. So I've changed the above GetPropertyValue type to childPage.GetPropertyValue<IEnumerable<IPublishedContent>> and the code then started working for me (in this scenario).

    You could perhaps check what data type your "image" field is?

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 11:58
    Morten
    0

    HasValue() returns False for me, but HasProperty() returns True. I just triple checked my content, and they all have an image and the property name/tag/id matches perfectly.

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 11:59
    Morten
    0

    It's a simple Media Picker.

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 12:03
    James Strugnell
    0

    Have you upgraded to 7.6 or clean install? Mine is an upgrade - some things started to work for me after I enabled the new ModelsBuilder settings in the web.config.

    <add key="Umbraco.ModelsBuilder.Enable" value="true" />
    <add key="Umbraco.ModelsBuilder.ModelsMode" value="PureLive" />
    
  • Morten 105 posts 345 karma points
    May 09, 2017 @ 12:07
    Morten
    0

    Upgrade and they were already added.

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 12:15
    Morten
    0

    Odd.. odd.. odd things are happening. I removed all the images, saved the content, inserted the images again, and then saved it all. Now HasValue() returns True and it actually works again.

    var img = Umbraco.Media(person.GetPropertyValue("image"));
    

    So weird. I just confirmed this on another page that had the same issue. How weird is that?

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 12:33
    James Strugnell
    0

    Yes, I've been removing and re-adding images and links along the way also, I think this is because they were previously stored as integer references but are now stored in "UDI" format. I thought I had read somewhere that code should be able to work with the new and old formats.

    It doesn't seem to be enough to just republish a site, you have to remove and re-add the linked content.

    The scary thing is I am upgrading a small website as a test so it's not been too much trouble doing that. We have much bigger websites to upgrade though and we can't go through and fix up all linked content manually...

    I'm pretty sure I must be doing something wrong though, or there's a bug...

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 12:35
    Morten
    0

    I am sure there is a fix for this. I just did this on a small website as well, but I can't start to imagine the time I would have to spend, if I were to go on one of our big sites with hundreds of news articles.

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 12:39
    James Strugnell
    0

    Hopefully someone can point to some proper guidance on what has changed, what the impact is and what the steps are to move to the new model.

    Or perhaps we just have to set EnablePropertyValueConverters to false and continue as before?

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 12:48
    Morten
    0

    That's what I am doing, but I still have to remove images and set them again before it works.

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 13:01
    James Strugnell
    0

    Uh oh...

  • Morten 105 posts 345 karma points
    May 09, 2017 @ 13:06
    Morten
    0

    I know, right? It's such a bummer.

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 14:08
    James Strugnell
    0

    Don't worry, somebody on here will enlighten us. Any minute now...

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 15:28
    James Strugnell
    0

    There's more related info on this here: http://issues.umbraco.org/issue/U4-7318

    If that is the same issue then the line that marked it as "Fixed" says you will "need to re-pick all of the media in your media pickers" - http://issues.umbraco.org/issue/U4-7318

    Please no.

  • James Strugnell 84 posts 192 karma points
    May 09, 2017 @ 16:19
    James Strugnell
    0

    I see 7.6.1 has been released. I'll have to see if that changes any of this.

  • Hayden 32 posts 170 karma points
    Jun 21, 2017 @ 22:55
    Hayden
    3

    I couldn't get the above code to work but converting my value to a string somehow did?

    @Umbraco.Media(Model.Content.GetPropertyValue("subHeroImage").ToString()).Url
    

    Without ToString() it doesn't work, might help somebody :)

  • Edgar Rasquin 326 posts 925 karma points
    Jul 11, 2017 @ 20:15
    Edgar Rasquin
    0

    This worked for me as well.

    But what I really trying to achieve was to get the crop url like so:

    @item.GetCropUrl("listImage", "listImage") 
    

    but it returns the following:

    1118?mode=pad&rnd=131442492180000000
    

    Any ideas how I could possibly get this to work?

  • Hayden 32 posts 170 karma points
    Jul 11, 2017 @ 21:01
    Hayden
    0

    Hi Edgar,

    Where is the @item var coming from (is is typed / dynamic etc), I setup a test on my side using Model.Content.GetCropUrl("..","..") and it brings in the URL

    the 1118 at the start looks like the media Id from Umbraco, looking up Umbraco.Media might help convert the id to a url

    or theres heaps of examples on the documentation which might point you in the right direction as well -> https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/image-cropper

    sorry I couldn't be any further help, I wasn't able to recreate your situation on my end!

    goodluck!

  • Richard Hague 1 post 71 karma points
    Apr 17, 2018 @ 23:58
    Richard Hague
    0

    Legend...this worked a treat!!! Many thanks

  • kaushal 5 posts 25 karma points
    Jun 14, 2018 @ 12:59
    kaushal
    0

    Hayden, your solution worked for me ;)

  • Bo Jacobsen 608 posts 2406 karma points
    Jul 29, 2017 @ 20:58
    Bo Jacobsen
    0

    I got the same issue in version 7.6.4 using the Umbraco.MediaPicker2

     if (content.HasValue("mediaPicker")) // return false.
    

    Have anyone found a solution?

  • John Bergman 483 posts 1132 karma points
    Jul 31, 2017 @ 15:01
    John Bergman
    0

    Not sure if this is your solution, but for all of the images on our site, we use something like this

    Model.Content.imageProperty.FirstOrDefault();

    FirstOrDefault because it is a list of ipublished content

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Aug 12, 2017 @ 16:07
    Jon R. Humphrey
    0

    Just to let everyone know I've just hit this too in an upgrade path from 7.2.6 to 7.6.5 and the only thing that worked for me, including trying to relink media, was the .ToString() solution from @Heyden's comment!

    Will put comments on the issue tracker letting HQ know it's not as fixed as expected?!?

    Thanks nonetheless to all as this really helped me with the final bugs on the migration!

    #h5yr!

  • M N 125 posts 212 karma points
    Oct 17, 2017 @ 16:40
    M N
    0

    Has anyone experienced issues using the new value converters on Members?

    var fullUser = ApplicationContext.Current.Services.MemberService.GetByUsername(curUser.UserName);
    
    IEnumerable<IPublishedContent> files = fullUser.GetValue<IEnumerable<IPublishedContent>>("files");
    

    files is always null (this works fine on content, and I love the new converters), so I have been grabbing the raw string which looks something like this and breaking into an array

    string[] stringarray = fullUser.GetValue<string>("files").Split(',');
    

    umb://media/2572389b7fc24ce9a057d7ba31f19f9c,umb://media/8fc3d88b63ba4c4baad3464b0bc176e4,umb://media/c65c3456e9c2435e97971c5f29ef97c2,umb://media/97daf7c8c5dd414bb906390b7d7ec5d0

    Then looping on the array and grabbing via TypedMedia. Unfortunately there is no TypedMedia that accepts an array or IEnumerable of Udi's so I'm doing it in the loop

    foreach (string f in stringarray)
    {
        var udi = Udi.Parse(f);   // umb://media/udi
        IPublishedContent med = Umbraco.TypedMedia(udi);
    }
    

    This all works fine for now, was just curious if there was general issues with Value Converters on IMember ? or if anyone has a cleaner approach as this is even one additional step compared to pre 7.6

    Cheers!

  • Leonardo Moura 21 posts 90 karma points
    Nov 28, 2017 @ 21:02
    Leonardo Moura
    0

    Hi guys,

    Easy way to get images:

     @{ 
            //Get the ID of the Field
            var mediaId = Umbraco.Field("image");
    
            //Get the MediaType to Write the URL
            var media   = Umbraco.Media(mediaId.ToString());
    
            //Salve the Path into the Image
            var image   = media.Url;
        }
    

    Make sure that there is image in the field or you will get error message.

  • Biagio Paruolo 1619 posts 1912 karma points c-trib
    Jan 18, 2018 @ 15:25
    Biagio Paruolo
    0

    Do Work it with Media Picker 2? I think no.

Please Sign in or register to post replies

Write your reply to:

Draft