Copied to clipboard

Flag this post as spam?

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

  • jonok 293 posts 643 karma points
    Mar 19, 2019 @ 19:17

    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 515 posts 2839 karma points MVP 5x c-trib
    Mar 19, 2019 @ 22:43
    Paul Seal

    Hi Jonok

    This is how you do it in v8.

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

    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 515 posts 2839 karma points MVP 5x c-trib
    Mar 20, 2019 @ 09:13
  • Natasha 9 posts 99 karma points
    Mar 20, 2020 @ 01:12

    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


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

    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 1882 karma points MVP 2x c-trib
    Mar 20, 2019 @ 07:34
    Søren Gregersen

    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: