Copied to clipboard

Flag this post as spam?

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


  • Kieron 152 posts 390 karma points
    Aug 08, 2018 @ 15:35
    Kieron
    1

    Insert image with Media Picker in 7.11

    Hi guys I'm using Umbraco 7.11.1, and can't get a mediapicker to just give me a URL to an item so I can use the image in the page.

    I take real issue with Umbraco and its seemingly sporadic effort at keeping any consistency between updates, it seems like every version breaks all techniques before it & the documentation is no help at all, I don't mean to moan about a free product but when you're trying to work with this technology it is frustrating.

    All I want to do is embed an image with the following:

    @{
        var selection = Umbraco.TypedContent(1098).Children("Section")
                            .Where(x => x.IsVisible());
    }
    
        @{
            foreach(var item in selection){
                <article class="style1">
                    <span class="image">
                     @Umbraco.Field("tileImage")
                    </span><a href="@item.Url">
                    <h2>@item.Name</h2>
                    <div class="content">
                    <p>@item.GetProperty("summaryText").Value</p>
                    <input type="checkbox" id="" name="" checked>
                    </div></a>
                </article>  
            }
        }
    

    You can see the line '@Umbraco.Field("tileImage")' is my latest guess, though I just wanted it to print anything at this point.

    I've tried about 12 different things from 12 different threads, even stuff that has worked in 7.10 no longer works.

    Thank you

  • Ben Palmer 176 posts 842 karma points c-trib
    Aug 08, 2018 @ 17:26
    Ben Palmer
    101

    Hi Kieron,

    I'm assuming that you're wanting to get the image from the item rather than the current page?

    Try outputting the image value from the item and see what that gives you. You can do this like so:

    @item.GetPropertyValue("tileImage")
    

    This will potentially give you the id of the item in which case you can do something like this:

    @{
        int imageId = item.GetPropertyValue<int>("tileImage");
    
        IPublishedContent image = null;
    
        if(imageId > 0)
        {
            image = Umbraco.TypedMedia(imageId);
        }
    }
    

    Then you'd be able to output the URL like this:

    @if(image != null)
    {
        @image.Url
    }
    

    Alternatively, if you've got property value converters enabled you can tidy that up like so:

    @{
        IPublishedContent image = item.GetPropertyValue<IPublishedContent>("tileImage")
    
        if(image != null)
        {
            @image.Url
        }
    }
    

    I've just typed this out quickly so apologies if there are any typos but hopefully it'll get you on the right track.

    Thanks,

    Ben

  • Kieron 152 posts 390 karma points
    Aug 09, 2018 @ 08:22
    Kieron
    0

    Thank you for taking the time to type that Ben, I felt like I had tried literally everything and you picked something I hadn't tried.

    Appreciate it!

    Do you know what use the Umbraco Field call actually has, considering its right there in the page for you to drop variables in straight away, it rarely works for me lol.

  • Ben Palmer 176 posts 842 karma points c-trib
    Aug 09, 2018 @ 09:03
    Ben Palmer
    1

    Glad it worked!

    To be honest, I've always avoided the use of Umbraco.Field as I find it better to get the values directly from the IPublishedContent or Model depending on your context.

    I'd also suggest looking into the Models Builder as this will really help tidy up your code.

    Thanks,

    Ben

Please Sign in or register to post replies

Write your reply to:

Draft