Copied to clipboard

Flag this post as spam?

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


  • David Hyslop 27 posts 181 karma points
    Aug 02, 2017 @ 08:48
    David Hyslop
    0

    List all Media Items from Media Library

    Hi

    I'm looking for a way to list all media items in a page from the media library.

    I've got scripts that will list media in a certain folder but what I need to do is list all media from a certain folder including the media that will be in subfolders.

    So

    • Folder (Start here)
      • Sub Folder
        • Media Item
      • Sub Folder
        • Media

    I would just need to list all the media from the start folder.

    So this makes a bit more sense, I'm going to use this to build an xml feed that will be passed into our search collection.

    Any help would be great, thanks.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 02, 2017 @ 09:49
    Alex Skrypnyk
    101

    Hi David

    If I understood you right, you need script like that:

    @inherits UmbracoViewPage
    
    @{
        var list = Umbraco.TypedMediaAtRoot();
    }
    
    @if (list.Any())
    {
        <ul>
            <li>
                @foreach (var media in list)
                {
                    <a href="@media.Url">@media.Name</a>
                    if (media.Children != null && media.Children.Any())
                    {
                        @RenderChildMedias(media.Children)
                    }
                }
            </li>
        </ul>
    }
    
    @helper RenderChildMedias(IEnumerable<IPublishedContent> mediaItems)
    {
        if (mediaItems.Any())
        {
            <ul>
                @foreach (var media in mediaItems)
                {
                    <li>
                        <a href="@media.Url">@media.Name</a>
                        @{
                            if (media.Children != null && media.Children.Any())
                            {
                                @RenderChildMedias(media.Children)
                            }
                        }
                    </li>
                }
            </ul>
        }
    }
    

    It renders all media items from all folders in the unordered list.

    Thanks,

    Alex

  • David Hyslop 27 posts 181 karma points
    Aug 02, 2017 @ 10:05
    David Hyslop
    0

    Works perfect, thank you very much.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 02, 2017 @ 10:06
    Alex Skrypnyk
    0

    You are welcome, David. Have a great day.

    Alex

  • David Hyslop 27 posts 181 karma points
    Aug 08, 2017 @ 12:42
    David Hyslop
    0

    Just as a follow up to this question I now have a working xml feed of all media.

    But I have a custom field in the media item for search category.

    i.e. enter image description here

    Does anyone know how I get this to output to the page. i.e using @media.contentCategory just gives error CS0118: 'media' is a 'variable' but is used like a 'method'

    Thanks.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 08, 2017 @ 13:37
    Alex Skrypnyk
    0

    Hi David

    Yes, try this code:

    @media.GetPropertyValue("searchCategory")
    

    Be sure that alias of field is right, as I see from screen your alias is "searchCategory"

    Thanks,

    Alex

  • David Hyslop 27 posts 181 karma points
    Aug 08, 2017 @ 14:03
    David Hyslop
    0

    Again perfect, Thanks Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 08, 2017 @ 14:11
    Alex Skrypnyk
    0

    You are welcome, David!

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft