Copied to clipboard

Flag this post as spam?

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


  • Curetos 7 posts 32 karma points
    Mar 29, 2013 @ 03:07
    Curetos
    0

    Getting media url

    I'm building a small recent articles widget that will grab the first 150 characters of the bodytext, and display the thumbnail associated with it (from a media picker).

    I'm using a partial and am having problems trying to get the media URL.  I've checked out what other people have said about this, but still can't seem to get it to work.

    This is what my code looks like thus far:

    @foreach (var item in root.Descendants("Post").OrderBy("CreateDate desc") {
       dynamic d = @Umbraco.Media(@item.GetPropertyValue("contentImage"));
       string imgUrl = d.UmbracoFile();
       <p>@imgUrl</p>
    }

    From what I've read and found out, getting the property value for a media picker should return the ID, and using @Umbraco.Media() is how you are theoretically supposed to get it . . .

    I think my main problem is finding old documentation or snippets that are supposed to be used in a different type of file..  Anyway, could someone please kindly point me in the right direction?

    Thanks

  • Curetos 7 posts 32 karma points
    Mar 29, 2013 @ 04:15
    Curetos
    0

    I think I have kind of figured it out, but it still feels kind of sketchy to me :\

    Anyway, this is what I've done thus far:

    int y = int.Parse(@item.GetPropertyValue("contentImage").ToString());
    <p> @uQuery.GetMedia(y).GetImageUrl() </p>

    Might there be a slightly cleaner way of doing this, where I don't have to convert the ID to a string and then parse it into an int?

  • Curetos 7 posts 32 karma points
    Mar 29, 2013 @ 04:34
    Curetos
    3

    Just in case anyone looks at this later, it looks to me like there are 2 semi-easy ways to get the URL from a media picker property:

    You can get it with @Umbraco.Media() like this:

    @Umbraco.Media(@item.GetPropertyValue("contentImage").umbracoFile

    Where item is the IPublishedContent object and contentImage is the alias for the image you want to grab.

    Or with uQuery like this:

    int i = int.Parse(@item.GetPropertyValue("contentImage").ToString());
    @uQuery.GetMedia(i).GetImageUrl()

    While it looks like uQuery has more methods after GetMedia(i) (such as getting the thumbnail), you can use the Umbraco helper to get it all in one line (without parsing).

     

    You can now ignore this thread.

     

Please Sign in or register to post replies

Write your reply to:

Draft