Copied to clipboard

Flag this post as spam?

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


  • wilmar 19 posts 129 karma points
    Jan 21, 2021 @ 12:21
    wilmar
    0

    GetValue<IPublishedContent> Media picker returns null

    hi im reading a property from IMember Interface that has a media picker but it is returning a wierd string when i use the non generic method enter image description here example string : umb://media/d18bad45aa50445645564545654

    when im using the following generic code, the result returns null , i dont know what to do im following the docs?

    enter image description here

    i think im doing every thing correct ? im using umbraco 8

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Jan 21, 2021 @ 13:18
    Huw Reddick
    0

    not sure what context you are running your code, but something like this should work

    var uHelper = new UmbracoHelper(UmbracoContext.Current);
    var imgUrl = uHelper.TypedMedia(udiResult);
                string urlStr = imgUrl.Url;
    

    where udiResult = your weird string :) umb://media/d18bad45aa50445645564545654

  • Bjarne Fyrstenborg 1286 posts 4060 karma points MVP 8x c-trib
    Jan 21, 2021 @ 13:26
    Bjarne Fyrstenborg
    0

    Hi Wilmar

    Is the Media Picker configurated to allow single or multiple images? The default Media Picker datatype instance allow to select multiple items.

    You can create a new datatype instance e.g. "Image Picker" and configurate this to not allow selecting multiple items (and maybe not folders?).

    Then the following should work:

    var profileImage = member.Value<IPublishedContent>("profileImage");
    var url = profileImage?.Url();
    

    Otherwise you might try this:

    var profileImage = member.Value<IEnumerable<IPublishedContent>>("profileImage")?.FirstOrDefault();
    var url = profileImage?.Url();
    

    /Bjarne

  • wilmar 19 posts 129 karma points
    Jan 21, 2021 @ 13:38
    wilmar
    0

    I know if this is overkill but the only way i manage to serve the image from an IMember was like this

     public string GetMemberImageUrl(IMember contentOwner,string alias ="photo")
        {
            try
            {
                var image = contentOwner.GetValue<string>(alias);
                if (!string.IsNullOrWhiteSpace(image) && Udi.TryParse(image, out var imageGuidUdi))
                {
                    var imageNodeId = Services.EntityService.GetId(imageGuidUdi);
    
                    if (imageNodeId.Success)
                    {
                        var mediaItem = _mediaService.GetById(imageNodeId.Result);
    
                        return mediaItem.GetUrl("umbracoFile", _logger);
                    }
                }
            }
            catch
            {
    
            }
    
            return RANDOM_USER_IMAGE;
        }
    
  • Bjarne Fyrstenborg 1286 posts 4060 karma points MVP 8x c-trib
    Jan 21, 2021 @ 13:56
    Bjarne Fyrstenborg
    100

    Why do you need to use IMember? You can get the member as IPublishedContent.

    If you logic in places inside a controller and don't need to set properties on members, you can use MemberShipHelper otherwise MemberService where you are dealing with IMember.

    Could you share a bit more of the code and the context?

  • wilmar 19 posts 129 karma points
    Jan 21, 2021 @ 14:30
    wilmar
    0

    Thanks you solve my problem, I'm new to Umbraco so it's difficult to understand their setup

  • wilmar 19 posts 129 karma points
    Jan 21, 2021 @ 14:23
    wilmar
    0

    Aa okej i think I understand know IMember is to modify the data and IPublishedContent is more for reading i gonna try that thanks 😊

  • Bjarne Fyrstenborg 1286 posts 4060 karma points MVP 8x c-trib
    Jan 21, 2021 @ 14:38
    Bjarne Fyrstenborg
    0

    You are welcome :)

    Feel free to ask any questions on the forum.

    A great start is also the videos on https://umbraco.tv/

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies