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:
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?
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:
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?
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)
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.
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.
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:
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
try:
<a href="@umbraco.library.GetMedia(System.Int32.Parse(page.thumbnail))" />
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:
When the macro runs on the page the contents of the href attribute is:
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?
oeps mixed up some method's. Try this:
<a href="@umbraco.library.GetMedia(System.Int32.Parse(page.thumbnail), false)/umbracoFile" />
Tried that and the result is now
Still no path to the image?
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)
HTH,
Hendy
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.
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.
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.
This is my first Umbraco project, so this may not be the best approach, but it works.
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
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
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
@Jeroen, thanks for the info. I'm learning everyday something new in Umbraco land :)
greetings,
Anthony
is working on a reply...