Copied to clipboard

Flag this post as spam?

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


  • Sérgio 30 posts 171 karma points
    Dec 01, 2021 @ 19:04
    Sérgio
    0

    IMember.GetValue<MediaWithCrops>("photo") returning null

    Hey everyone.

    I am loading a member, which is, of course, returned as an IMember but when I try to access its photo through GetValue< MediaWithCrops >("photo") I am getting a null.

    If I try using GetValue("photo"), I get the following:

    [\r\n  {\r\n    \"key\": \"c0bef275-d973-4bf7-be05-2c52728527a9\",\r\n    \"mediaKey\": \"96ef7f85-3d97-4a3a-8d5c-176f9ec43063\",\r\n    \"crops\": [],\r\n    \"focalPoint\": {\r\n      \"left\": 0.5,\r\n      \"top\": 0.5\r\n    }\r\n  }\r\n]
    

    I'm using MediaPicker3 and Umbraco 9.1.1.

    Any light shed into this would be much appreciated.

    Thank you very much!

  • Frank Laumann 39 posts 303 karma points
    Dec 01, 2021 @ 20:27
    Frank Laumann
    0

    Hi Sérgio

    It looks like the problem is that you can't cast that property to "MediaWithCrops" model.

    What I usually do in these cases is to do var photo = GetValue("photo") with out the template cast and that way check which model the photo variable is by debugging. Is that exactly what you did there?

    I think I used IPublishedContent for images, MediaWithCrops is new to me.

    Hope this will help.

    Best regards Frank

  • Sérgio 30 posts 171 karma points
    Dec 01, 2021 @ 20:40
    Sérgio
    0

    Hi Frank. Thank you very much for pitching in.

    MediaWithCrops is the new type that MediaPicker3 uses. When I try GetValue("Photo") I get that JSON I have shown before, so it's not even an Udi.

  • Frank Laumann 39 posts 303 karma points
    Dec 01, 2021 @ 20:42
    Frank Laumann
    0

    That's wierd. In the docs it says media picker 3 return IPublishedContent or IEnumerable

    https://our.umbraco.com/documentation/fundamentals/Backoffice/Property-Editors/Built-in-Property-Editors/Media-Picker/

    Best regards Frank

  • Sérgio 30 posts 171 karma points
    Dec 01, 2021 @ 20:50
    Sérgio
    0

    That's the old Media Picker.

    Media Picker 3 (the new one, which I am using), returns MediaWithCrops. Check it out here.

  • Frank Laumann 39 posts 303 karma points
    Dec 01, 2021 @ 20:53
    Frank Laumann
    0

    Sorry I see.

    Have you tried IEnumerable

    Best Regards Frank

  • Sérgio 30 posts 171 karma points
    Dec 01, 2021 @ 21:06
    Sérgio
    0

    I have, although it should only return a single object. I have tried that, IPublishedContent as well (and IEnumerable of IPublishedContent).

    None of it worked :(

  • Sérgio 30 posts 171 karma points
    Dec 12, 2021 @ 20:52
    Sérgio
    0

    Bumping this to see if we can get more eyes on it. I find it weird that no one else has found this issue or has a possible answer for it.

  • Frank Laumann 39 posts 303 karma points
    Dec 12, 2021 @ 20:54
    Frank Laumann
    0

    Can you post your code ? Then I will have a look.

  • Sérgio 30 posts 171 karma points
    Dec 12, 2021 @ 20:59
    Sérgio
    0

    Hi Frank. Sure. Thanks.

    var authorTemp = _serviceContext.MemberService.GetMembersByMemberType("author").FirstOrDefault(x => x.Name == authorName);
    var avatarImage = authorTemp.GetValue<MediaWithCrops>("photo");
    

    avatarImage is always null. I have also tried it with IPublishedContent, IMedia, just .GetValue("photo"). Nothing works.

    .GetValue("photo") returns the following, as I indicated above:

    [\r\n  {\r\n    \"key\": \"c0bef275-d973-4bf7-be05-2c52728527a9\",\r\n    \"mediaKey\": \"96ef7f85-3d97-4a3a-8d5c-176f9ec43063\",\r\n    \"crops\": [],\r\n    \"focalPoint\": {\r\n      \"left\": 0.5,\r\n      \"top\": 0.5\r\n    }\r\n  }\r\n]
    
  • Frank Laumann 39 posts 303 karma points
    Dec 12, 2021 @ 22:18
    Frank Laumann
    100

    I think I solved it. You need to use a different service called MemberManager.

    var authorTemp = await _memberManager.FindByNameAsync(authorName);
    var author = _memberManager.AsPublishedMember(authorTemp);
    var avatarImage = author.Value<MediaWithCrops>("photo");
    

    You can inject it with the interface IMemberManage from Umbraco.Cms.Core.Security.

    Hope that helps :)

    Best Regards Frank

  • Sérgio 30 posts 171 karma points
    Dec 12, 2021 @ 22:25
    Sérgio
    0

    Thanks, Frank. That's it. Appreciate your time!

Please Sign in or register to post replies

Write your reply to:

Draft