Copied to clipboard

Flag this post as spam?

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


  • Matt Salmon 22 posts 43 karma points
    Jan 14, 2011 @ 15:53
    Matt Salmon
    0

    Calling the Umbraco GetMedia function from Razor macro

    I'm loving the new Razor support in 4.6, it's going to present a really maintainable option for dynamic template code, particularly for people who don't speak XSLT!

    Can anyone tell me how I'd make a call to the umbraco.library:GetMedia function from within a Razor script. For example, say my document type has a 'thumbnail' media picker data type and I want to display a thumbnail list, like this:

    <ul>
    
    @foreach(var page in Model.Children) {
    <li> <a href="@page.Url" title="@page.Name">@page.Name</a><img src="@page.thumbnail" /> </li>
    }
    </ul>

    The thumbnail obviously returns the ID of the media node, and I'd normally have to wrap that in a call to GetMedia in order to output the source URL. I'm not sure exactly how I'd achieve this, can I @import the Umbraco library and wrap GetMedia in an inline function call? Or is there a more straightforward way?

    Thanks

    Matt

     

  • Yannick Smits 321 posts 718 karma points
    Jan 14, 2011 @ 17:55
    Yannick Smits
    0

    try:

    <a href="@umbraco.library.GetMedia(System.Int32.Parse(page.thumbnail))" />

  • Matt Salmon 22 posts 43 karma points
    Jan 17, 2011 @ 18:38
    Matt Salmon
    0

    OK, thanks for the response. Firstly, the GetMedia method doesn't have an overload with 1 parameter, so I had to make a small adjustment just to get the Macro to compile:

    <a href="@umbraco.library.GetMedia(System.Int32.Parse(page.thumbnail), false)" />

    When the macro runs on the page the contents of the href attribute is:

    MS.Internal.Xml.XPath.XPathSelectionIterator

    Obviously not exactly what we're looking for. I see where you were going with that though, does anyone else have any other suggestions as to the syntax for this?

     

  • Yannick Smits 321 posts 718 karma points
    Jan 18, 2011 @ 00:10
    Yannick Smits
    0

    oeps mixed up some method's. Try this:

    <a href="@umbraco.library.GetMedia(System.Int32.Parse(page.thumbnail), false)/umbracoFile" />

  • Matt Salmon 22 posts 43 karma points
    Jan 18, 2011 @ 09:56
    Matt Salmon
    0

    Tried that and the result is now

    <img src="MS.Internal.Xml.XPath.XPathSelectionIterator/umbracoFile">

    Still no path to the image?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Jan 18, 2011 @ 10:17
    Hendy Racher
    0

    Hi Matt,

    As an alternative to using the XPathNodeIterator how about using the uQuery helper library which will return an umbraco.cms.businesslogic.media.Media object from the media ID instead ? (not tested this snippet)

    @using umbraco.presentation.nodeFactory
    @using umbraco.cms.businesslogic.media
    @using
    uComponents.Core
    @using uComponents.Core.uQueryExtensions


    @foreach(var page in Model.Children) {
    <li>
    <a href="@page.Url" title="@page.Name">@page.Name</a>
    <img
    src="@uQuery.GetMedia(System.Int32.Parse(page.thumbnail).GetProperty<string>("umbracoFile")" />
    </li>
    }

    HTH,

    Hendy

  • Yannick Smits 321 posts 718 karma points
    Jan 18, 2011 @ 11:07
    Yannick Smits
    0

    Oeps, must have been a sleep last night... mixing xslt with C#. Hendy's approach looks correct. You could do it without uComponents too using the code snippet from Lee at this post.

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Jul 23, 2011 @ 17:03
    Damiaan
    1

    Hi, 

    I know it's already answered. But I tought it might be interesting to share an other solution.  

    Basically it's plain old .Net code but used in a razor script.

    @{
        var media = new umbraco.cms.businesslogic.media.Media(
                    Convert.ToInt32(@Parameter.Video)
            );
        string imageUrl = media.getProperty("umbracoFile").Value.ToString();
    }
  • Chris Frewin 3 posts 23 karma points
    Jan 29, 2012 @ 14:18
    Chris Frewin
    0

    Hi Damiaan, having read through this thread and tried several approaches, your reply helped very much.

    The exact code (which sits inside an @if statement) that I used is as follows.

    foreach (var item in @Model.Masters.Where("Visible"))
    {
    var media = new umbraco.cms.businesslogic.media.Media(Convert.ToInt32(@item.mainImage));
    string imageUrl = media.getProperty("umbracoFile").Value.ToString();
    <div class="slide">
        <a href="@item.Url">
          <img src="@imageUrl" />
        </a>
        <div class="caption" style="bottom: 0"><p>@item.Name</p></div>
    </div>                           
    }                 

    This is my first Umbraco project, so this may not be the best approach, but it works.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 29, 2012 @ 22:35
    Jeroen Breuer
    0

    Don't use the Media API. It's very slow and has no caching. Try umbraco.library.GetMedia or Library.MediaById.

    You can also try DAMP. Than you can have the media xml in your umbraco.config which is easier to use.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 06, 2012 @ 18:57
    Anthony Candaele
    0

    Hi Jeroen,

    Thanks a lot for the tip. 

    Library.MediaById works fine.

    Actually what's the difference between the umbraco.library and Library namespaces?

    greetings,

    Anthony

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Oct 07, 2012 @ 15:37
    Jeroen Breuer
    0

    Hello,

    umbraco.library is originally created for xslt. If you use umbraco.library.GetMedia it will return a XPathNodeIterator and it's not easy to work with that in Razor. If you use Library.MediaById it will return a DynamicMedia object which is easier to work with in Razor.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 07, 2012 @ 17:51
    Anthony Candaele
    0

    @Jeroen, thanks for the info. I'm learning everyday something new in Umbraco land :)

    greetings,

    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft