Copied to clipboard

Flag this post as spam?

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


  • Joseph Esquibel 7 posts 99 karma points
    Aug 22, 2015 @ 05:15
    Joseph Esquibel
    2

    How to get media url by property alias?

    Hello everybody, i'm very new to Umbraco, and you'll soon know why. Here's my dilemma:

    1. I'm creating an Umbraco with multiple sites
    2. I want to build a Document Type I can use EVERYWHERE
    3. I need to be able to get a media URL from that document type by a property ALIAS name, not a media ID because I need to use this code EVERYWHERE

    As far as I understand, I get the media id after i create an instance of that document type within the properties tab, and each instance is a different media id. Am I correct?

    so the question: How to get media url by property alias? like this:

    var url - umbraco.GetMedia("backgroundImage").Url;

    Please help!! or, point me to some good reading to I can figure it out myself.

    Thanks.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Aug 22, 2015 @ 11:13
    Dennis Aaen
    0

    Hi Joseph and welcome to our.

    Try to have a look at hos documentation.

    https://our.umbraco.org/documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Media-Picker

    Hope this helps,

    /Dennis

  • Joseph Esquibel 7 posts 99 karma points
    Aug 23, 2015 @ 03:35
    Joseph Esquibel
    0

    Thanks Dennis, but I read this article already, and I tried these methods. I'm simple doing something wrong, or I created page incorrectly. please read the response I gave to Marc, and I has all the details I can give.

    Thanks again

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 22, 2015 @ 11:22
    Marc Goodson
    1

    Hi Joseph

    is your backgroundImage a Media Picker for a single item ?

    If so then Umbraco stores the Id of the picked Media as the value of 'backgroundImage', on your content object

    and then you can use something like this to turn the id into a url

    (strongly typed)

        var backgroundUrl = String.Empty;    
        var backgroundImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue<int>("backgroundImage"));
    if (backgroundImage != null){
    backgroundUrl = backgroundImage.Url;
    }
    

    or dynamic:

    var backgroundImageId = CurrentPage.backgroundImage;
    var backgroundImage = Umbraco.Media(backgroundImageId);
    var backgroundImageUrl = backgroundImage.Url;
    

    to get the Url of an image in any template

    see: https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/media-picker

    for multiple picked images

    or If you are using the ImageCropper:

    https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/image-cropper

    (which also has an overload for property alias)

  • Joseph Esquibel 7 posts 99 karma points
    Aug 23, 2015 @ 03:34
    Joseph Esquibel
    0

    Thanks for the response Marc.

    Well, I tried both methods, and i'm still not getting anything back. Here's my view/template code:

    Here's the view code:

    enter image description here

    Here's the Document Type

    enter image description here

    Here's the instance of the document type:

    enter image description here

    I'm simply not seeing any value when I inspect the element:

    enter image description here

    Hopefully this is enough information for you. Any help would be appreciated. If you want my code files, I'll be happy to email them to you.

    Thanks!

  • jivan thapa 194 posts 681 karma points
    Aug 23, 2015 @ 05:12
    jivan thapa
    100

    Could you output the Name of the current page? Do something like @Model.Content.Name

    Based on your code. 1) If you are browsing the url /joeyesquibel.com/Header/ , it should show the image a aspected.

    2) However if you are browsing the url /joeyesquibel.com/ , It will not show the image because (currentpage) does not have "backgroundImage" property.

    I need to do something like @Mode.Content.Chilldren().FirstOrDefault(x=>x.Name.Equal("Header")).GetPropertyValue

    I have not tested the code.

  • Joseph Esquibel 7 posts 99 karma points
    Aug 23, 2015 @ 05:37
    Joseph Esquibel
    0

    Jivan,

    I believe you're right. Here's the result:

    enter image description here

    The main page wouldn't have the background image, but the child would. Here's my working code:

    enter image description here

    Thanks for your time everybody.

Please Sign in or register to post replies

Write your reply to:

Draft