Search In
Learn from 350 other Umbracians at the annual Umbraco Conference - CodeGarden '13. More than twenty high quality sessions, open spaces, hackathons and social events you'll remember. Not to be missed! Less than 25 tickets left - get yours now!
How the heck do I just get a media folder and loop over it's contents with Razor? Can't find any examples for this.
According to this other topic, Media.Children hasn't been implemented because media isn't cached.
The workaround would be to do a good old fashioned umbraco.library.GetMedia(mediaId, true) and work from there.
Hi, or if you're using uComponents how about the uQuery methods to access collections of Media ?
There are the usual XPath axis type extension methods that can be used with Linq expressions:
GetAncestorMedia()
GetAncestorOrSelfMedia()
GetSiblingMedia()
GetDescendantMedia()
GetDescendantOrSelfMedia()
GetChildMedia()
as well as static helpers:
GetMediaByXPath(string)
GetMediaByCsv(string)
GetMediaByXml(string)
GetMediaByName(string)
GetMediaByType(string)
HTH,
Hendy
Here's a sample "file browser" our.umbraco.org/.../simple-media-browser . However using uComponents would make the code nicer.
If I were to use uQuery.GetChildMedia(), how do I specify the start folder? It doesn't seem to take any parameters.
I get this error when trying to loop over the collection:
: error CS0117: 'umbraco.item' does not contain a definition for 'Image'.
This is confusing... Razor looks cool, but not enough good examples out there yet to be productive with it.
FYI, that "file browser" sample doesn't work at all for me.
Hi Robert,
The GetChildMedia() method is an extension method on the umbraco.cms.businesslogic.media.Media object, so you can do the following:
using umbraco.cms.businesslogic.media;Media startMedia = new Media(123);foreach(Media childMedia in startMedia.GetChildMedia()){...}
That was hard... Here's what ultimately worked for me...
@using umbraco.cms.businesslogic.media;@using uComponents.Core;@using uComponents.Core.uQueryExtensions;@{Media startMedia = new Media(1102);foreach(Media m in startMedia.GetChildMedia()){ <img src="/imagegen.ashx?image=@m.getProperty("umbracoFile").Value&width=250"/>; }}
Nice!
The wiki sample had a missing { } (which worked with 4.6) sorry for that.
I updated the wiki sample now and changed the extra "get safe property" to GetPropertyAsString from uQuery instead. Looks much better now.
About number of samples - this is still fresh so yes, not that many, but the count is increasing day by day :), in the forum, in the wiki (which everyone is free to add to), and don't forget the included razor macro templates.
Happy coding!
What is the link for the wiki sample mentioned above?