Copied to clipboard

Flag this post as spam?

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


  • ThomasBrunbjerg 90 posts 182 karma points
    Aug 25, 2017 @ 06:44
    ThomasBrunbjerg
    0

    Can't access media picker property in content node

    I want to display my header logo on all pages from a master template, but I am having trouble getting the media URL from the document type.

    My header logo resides in this content node: http://i.imgur.com/QuugP1J.png

    Which has the template set as master.

    The document type for the page is located here in the tree: http://i.imgur.com/6XZbecu.png

    All my templates inherit from a single master template, where I want to display the header logo, but for some reason it won't display it when i insert the code for retrieving media items.

    @{
                                    var typedMediaPickerSingle = Model.Content.GetPropertyValue<IPublishedContent>("headerLogo");
                                    if (typedMediaPickerSingle != null)
                                    {
                                        <a href="index.html"><img src='@typedMediaPickerSingle.Url" style="width:320px" alt="@typedMediaPickerSingle.GetPropertyValue("headerLogo")' /></a>
                                    }
                                }  
    

    Sorry for the horrible formatting, I don't know why the Umbraco Forum editor does this. It looks fine in my project.

  • Neil Hodges 338 posts 987 karma points
    Aug 25, 2017 @ 14:40
    Neil Hodges
    0

    Depending on which node within that tree you are on you will need to reference the node that has the 'headerLogo' property.

    To test do something like this:

    var DKnode = Umbraco.TypedContent(123) //Use the ID of the node that has the header logo
    
    var typedMediaPickerSingle = DKnode.GetPropertyValue<IPublishedContent>("headerLogo");
    

    That should return the header logo

  • Nathan Sleigh 109 posts 353 karma points
    Aug 25, 2017 @ 16:19
    Nathan Sleigh
    0

    Nothing wrong with the above method, but I thought I would add a way to do it without putting a hard-coded id into the code.

    So alternatively you could try:

    var sprogNode = Model.Content.AncestorOrSelf("sprog");
    var headerLogo = sprogNode.GetPropertyValue<IPublishedContent>("headerLogo");
    

    Again, should do exactly the same as the above but wont have hard-coded id, just in case your ids change per environment.

  • Avatar 15 posts 92 karma points
    Aug 25, 2017 @ 18:27
    Avatar
    1

    Hi Thomas,

    Depending on the version, that method may or may not work. The above ways of getting the media items rely on property value converters. Also, is this an image upload or media picker? If it's an image upload, I believe that the URL will automatically be returned, but if its a media picker, basically you have to get the ID and run it through Umbraco.TypedMedia(int id) to get back the media item as IPublishedContent ( I think that's right).

    int imageId = node.GetPropertyValue<int>("headerImage");
    var image = Umbraco.TypedMedia(imageId);
    var imageUrl = imageItem.Url;
    

    If you want it recursive, just add true as the second argument in GetPropertyValue

    Umbraco 7.6 introduced new ways of getting URLs, honestly I haven't used those methods enough to have them memorized, but here's a link that I keep handy: http://www.codeshare.co.uk/blog/how-to-get-the-file-path-of-a-media-item-in-umbraco/

    Also make sure you have propertyvalueconverters enabled in your umbracosettings.config.

Please Sign in or register to post replies

Write your reply to:

Draft