Copied to clipboard

Flag this post as spam?

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


  • Mikkel 14 posts 115 karma points
    May 03, 2018 @ 07:33
    Mikkel
    0

    Can't retrieve image URL from Umbraco.MediaPicker2

    Hey!

    So I wan't to make a macro that creates a list of all content pages posted of the document type "press", with text and image from every page.

    Currently I'm able to get the text to show, but there seems to be a problem with getting the image.

    I've been trying a lot of different things, but can't seem to get it to work. Searching hasn't been a help for me either.

    I am using the media picker "Umbraco.MediaPicker2", which is attached to the document type of "press". The media pickers alias is "pressImg" and doesn't allow for multiple items to be picked.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ 
        var selection = Model.Content.AncestorOrSelf().Descendants().Where(x => x.IsVisible() && x.DocumentTypeAlias == "press").OrderBy("UpdateDate desc").Take(6);
    }
    
    @if (selection != null) {
            @foreach (var item in selection)
            {
                var logo = Model.Content.GetPropertyValue<IPublishedContent>("pressImg");
                if(logo != null)
                {
                    <li>
                        <a href="@item.Url">
                            <p>@item.GetPropertyValue("h1")</p>
                            <img src="@logo.Url" alt='@logo.GetPropertyValue("altTekst")' />
                        </a>
                    </li>
                }
            }
    }
    
    else {
        <h3>Content coming soon</h3>
    }
    

    Hoping someone's able to help me out :) Thanks in advance! - Mikkel

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 03, 2018 @ 08:02
    Dave Woestenborghs
    0

    Hi Mikkel,

    I think the problem is that you are using Model.Content.AncestorOrSelf() without any parameters. This will take just the current item and look for descendants of the type "press"

    Can you try it with passing in the document type alias of your root node ?

    Dave

  • Mikkel 14 posts 115 karma points
    May 03, 2018 @ 08:08
    Mikkel
    0

    Hey, thanks for your reply!

    So I tried as you described, but there seems to be no difference, sadly.

    var selection = Model.Content.AncestorOrSelf("home").Descendants().Where(x => x.IsVisible() && x.DocumentTypeAlias == "press").OrderBy("UpdateDate desc").Take(6);
    

    The selection seems to work with and without it, as I'm able to get the text content from the selection, but the safety check for the image prevents any of it being posted.

    Mikkel

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    May 03, 2018 @ 08:11
    Dave Woestenborghs
    101

    Hi Mikkel,

    Should have looked better at your code example. Can you try this

    @if (selection != null) {
            @foreach (var item in selection)
            {
                var logo = item.GetPropertyValue<IPublishedContent>("pressImg");
                if(logo != null)
                {
                    <li>
                        <a href="@item.Url">
                            <p>@item.GetPropertyValue("h1")</p>
                            <img src="@logo.Url" alt='@logo.GetPropertyValue("altTekst")' />
                        </a>
                    </li>
                }
            }
    }
    
    else {
        <h3>Content coming soon</h3>
    }
    

    You were retreiving the image from the Model.Content, but that should be retreived from the item in the for loop

    Dave

  • Mikkel 14 posts 115 karma points
    May 03, 2018 @ 08:13
    Mikkel
    0

    That did the trick!

    Thanks a bunch!

  • Nadine Fisch 159 posts 429 karma points
    May 03, 2018 @ 08:13
    Nadine Fisch
    1

    I always get images-Urls like this

    var image = Umbraco.Media(content.pressImg);
    var imageUrl = image.Url;
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    May 03, 2018 @ 10:12
    Alex Skrypnyk
    0

    Hi Nadine

    Do not use "Umbraco.Media" - it's dynamic type.

    Always use Umbraco.TypedMedia()

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft