Copied to clipboard

Flag this post as spam?

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


  • Jim 17 posts 60 karma points
    Jan 26, 2016 @ 04:00
    Jim
    0

    Unable to list media items from razor template

    I have media items including folders. I am able to list the contents of the media root using this code:

    @foreach (var child in Umbraco.TypedMediaAtRoot()) {
    <h5>@child.Url | @child.Name</h5>}
    

    I can't figure out how to start with a folder under the root. I don't get any IntelliSense in VS2015, and I am having problems discovering the right solution.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 26, 2016 @ 08:07
    Jeavon Leopold
    0

    You have to decide how you want to select that folder, the easiest way is to use its id, then you can use @Umbraco.TypedMedia(1234). Other methods could be to create a specific folder media type or to select by name

  • Jim 17 posts 60 karma points
    Jan 26, 2016 @ 12:10
    Jim
    0

    I would like to select it by name

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 26, 2016 @ 12:24
    Jeavon Leopold
    0

    Ok, then assuming the folder is in the root I would use something like this:

    var myFolder = Umbraco.TypedMediaAtRoot().FirstOrDefault(f => f.Name == "MyFolderName");
    
  • Jim 17 posts 60 karma points
    Jan 26, 2016 @ 17:25
    Jim
    0

    now this code:

    @{
        var myFolder = Umbraco.TypedMediaAtRoot().FirstOrDefault(f => f.Name == "Documents");
    
        foreach (var child in myFolder) {
            <h5>@child.Url | @child.Name</h5>
        }
    }
    

    generates this error:

     foreach statement cannot operate on variables of type 'Umbraco.Core.Models.IPublishedContent' because 'Umbraco.Core.Models.IPublishedContent' does not contain a public definition for 'GetEnumerator'
    
  • Ian 178 posts 752 karma points
    Jan 26, 2016 @ 17:57
    Ian
    1

    I think myFolder should have a Children property so it would be

    foreach (var child in myFolder.Children) {
    

    see this very similar thread for ideas on how to achieve what you're after

    https://our.umbraco.org/forum/developers/razor/63629-Iterate-through-Media-Folder

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jan 26, 2016 @ 18:10
    Jeavon Leopold
    0

    Absolutely correct!

  • Jim 17 posts 60 karma points
    Jan 26, 2016 @ 18:12
    Jim
    0

    Thank you all so much. It is working and I finally understand how to use it.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies