Copied to clipboard

Flag this post as spam?

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


  • Pierre Breau 11 posts 121 karma points
    Apr 09, 2019 @ 00:05
    Pierre Breau
    0

    Umbraco CMS 8 Retrieve Media File

    I have setup a page to display news article and I can't seem to retrieve the url of the Media for the new article to add it to the SRC of the img.

    I am not sure of the approach to get the value of headerImage url,.

    Here is the code:

    @{
    var selection = Umbraco.Content(Guid.Parse("d84ed744-7582-4507-83f9-18b98fe83838"))
    .Children("articlesItem")
    .Where(x => x.IsVisible())
    .OrderByDescending(x => x.CreateDate);}
    
    @foreach (var item in selection)
    {
    
    <div id="fh5co-blog">
        <div class="container">
            <div class="col-lg-4 col-md-4">
                <div class="animate-box">
                    <a href="@item.Url"><img class="img-responsive" src="@item.Value("headerImage")" alt=""></a>
                    <div class="blog-text">
                        <h3><a href="@item.Url">@item.Name</a></h3>
                        <span class="posted_on">@item.CreateDate</span>
                        <p>@Html.Raw(@item.Value("articleContent").ToString().Truncate(100))</p>
                        <a href="@item.Url" class="btn btn-primary">Read More</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    


    }

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 09, 2019 @ 07:00
    Jonathan Distenfeld
    0

    Hi Pierre,

    This code @item.Value("headerImage") gives you the complete media node instead of the actual url. Also, as Rann suggested, you should check if the media item and the url exists (Null/Empty check) Try using this codesnippet:

    @if (item.HeaderImage != null && !String.IsNullOrEmpty(item.HeaderImage.Url))
    {
    <a href="@item.Url"><img class="img-responsive" src="@item.HeaderImage.Url" alt=""></a>
    }
    

    -Jonathan

  • Ambert van Unen 175 posts 819 karma points c-trib
    Apr 09, 2019 @ 07:17
    Ambert van Unen
    1

    Don't forget to check if it's not null/empty, else you'll get an exception when directly checking the .Url

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 09, 2019 @ 07:39
    Jonathan Distenfeld
    0

    Thanks Rann! I updated my answer

  • Pierre Breau 11 posts 121 karma points
    Apr 09, 2019 @ 09:29
    Pierre Breau
    0

    Thanks for your reply

    Gave that a try as you suggested and in visual studio as well, it's not recognizing headerImage or HeaderImage.

    here is a screenshot of Visual studio and umbraco

    enter image description here

    enter image description here

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 09, 2019 @ 09:57
    Jonathan Distenfeld
    100

    Alright, so youre item is dynamic. In that case, you can do it like this:

    @{ var headerImage = item.Value<IPublishedContent>("headerImage"); }
    @if (headerImage != null && !String.IsNullOrEmpty(headerImage.Url))
    {
    <a href="@item.Url"><img class="img-responsive" src="@headerImage.Url" alt=""></a>
    }
    
  • Pierre Breau 11 posts 121 karma points
    Apr 09, 2019 @ 10:13
    Pierre Breau
    0

    that did the trick thanks a lot for your help. much appreciated

Please Sign in or register to post replies

Write your reply to:

Draft