Copied to clipboard

Flag this post as spam?

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


  • jonok 297 posts 658 karma points
    Mar 19, 2019 @ 19:17
    jonok
    0

    U8 - How to display the media URL from Media Picker property?

    I am trying to display the URL of a media item that has been selected from a media picker property. Any assistance would be greatly appreciated.

    I have a nested content datatype:

    var contentBlocks = Model.Value<IEnumerable<IPublishedElement>>("contentBlocks");
    

    and I am then trying to loop through these, and get a node from a content picker, and then get a media picker property named 'image' from this node:

    foreach(var item in contentBlocks){
        var contentBlock = item.GetProperty("block").Value<IPublishedElement>();
        var imageUrl = contentBlock.GetProperty("image").Value().Url;
    }
    

    but I am having trouble retrieving the Url into the 'imageUrl' var. I am receiving the error "Cannot assign method group to an implicitly-typed variable", and I've tried a few different things but still can't get the Url property from the Image.

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Mar 19, 2019 @ 22:43
    Paul Seal
    100

    Hi Jonok

    This is how you do it in v8.

    var imageUrl = Model.Value<IPublishedContent>("imageAlias")?.Url ?? "";
    
  • jonok 297 posts 658 karma points
    Mar 20, 2019 @ 08:33
    jonok
    0

    One other thing Paul - what do the question marks do? Do they check if the property exists before attempting to get the URL?

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Mar 20, 2019 @ 09:13
  • Natasha 9 posts 99 karma points
    Mar 20, 2020 @ 01:12
    Natasha
    0

    HI how do I write this for a macro? does it go inside curley braces for example? I would like to add an image to the latest latest blog Macro but so far it is either coming up blank or throwing an error

    @post.Excerpt

                <div class="col-md-3">
       var imageUrl = item.Value<IPublishedContent>("listingImage")?.Url ?? "";
       <img src="image.Url">
                    </div>
            }
                </div>
    
  • jonok 297 posts 658 karma points
    Mar 19, 2019 @ 22:53
    jonok
    1

    Thanks very much Paul. Not sure why I was using IPublishedElement (I think I saw it on some example code somewhere), but changing it to IPublishedContent has fixed it.

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Mar 20, 2019 @ 07:34
    Søren Gregersen
    0

    An element does not have an url :-)

    It only lives as a property on a document, like nested content.

Please Sign in or register to post replies

Write your reply to:

Draft