x First time here? Check out the FAQ
  • Avatar330posts325karma

    Loop Over Media Folder With Razor?

    Robert J. Bullock started this topic March 16, 2011 @ 06:13

    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.

  • Sebastiaan Janssen posted this reply March 16, 2011 @ 07:57

    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.

  • Hendy Racher posted this reply March 16, 2011 @ 08:03

    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:







    as well as static helpers:








  • Avatar924posts1784karma
    Comment with ID: 69629
    Jonas Eriksson posted this reply March 16, 2011 @ 08:23

    Here's a sample "file browser" http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/simple-media-browser . However using uComponents would make the code nicer.

  • Avatar330posts325karma
    Comment with ID: 69634
    Robert J. Bullock posted this reply March 16, 2011 @ 09:19

    If I were to use uQuery.GetChildMedia(), how do I specify the start folder? It doesn't seem to take any parameters.

  • Avatar330posts325karma
    Comment with ID: 69636
    Robert J. Bullock posted this reply March 16, 2011 @ 09:37

    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.

  • Avatar330posts325karma
    Comment with ID: 69637
    Robert J. Bullock posted this reply March 16, 2011 @ 09:41

    FYI, that "file browser" sample doesn't work at all for me.

  • Hendy Racher posted this reply March 16, 2011 @ 09:43

    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())



  • Avatar330posts325karma
    Comment with ID: 69642
    Robert J. Bullock posted this reply March 16, 2011 @ 10:01

    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 in startMedia.GetChildMedia())
        <img src="/imagegen.ashx?image=@m.getProperty("umbracoFile").Value&width=250"/>;

  • Avatar924posts1784karma
    Comment with ID: 69659
    Jonas Eriksson posted this reply March 17, 2011 @ 05:57


    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!

  • Avatar139posts208karma
    Comment with ID: 94904
    Ben Norman posted this reply November 3, 2011 @ 10:53

    What is the link for the wiki sample mentioned above?


Please login or Sign up To post replies