Copied to clipboard

Flag this post as spam?

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


  • Poornima Nayar 106 posts 276 karma points MVP 6x c-trib
    May 16, 2016 @ 09:02
    Poornima Nayar
    0

    List all media by a media type

    Hi,

    Is there any way to list all the media items by a media type? For eg. I want to list all media of type "Video".

    Poornima

  • Marcin Zajkowski 112 posts 585 karma points MVP 7x c-trib
    May 16, 2016 @ 10:20
    Marcin Zajkowski
    5

    Hi,

    there are couple options to solve this case.

    1) There is a method in MediaService (https://our.umbraco.org/documentation/reference/management/services/mediaservice) called GetMediaOfMediaType(int id). You just need to retrieve or input related id of your media type (Video in that case).

    Getting ID of media type - easy way :)

    Then code could looks like:

    var mediaService = ApplicationContext.Current.Services.MediaService;
    // ID of media type retrieved from database / administrator panel (see above ^^)
    var videoMediaTypeId = 1080;
    var allVideos = mediaService.GetMediaOfMediaType(videoMediaTypeId);
    
    foreach (var video in allVideos)
    {
        <div>@video.Name</div>
    }
    

    You also need to remember that those calls are going directly to DB and shoulnd't be performed / pinged too often. It may be optimized (cached for example etc).

    2) You can iterate throught the PublishedContent objects in your Media tree and check for specific type name of the objects / files. The sample code is even in sample macros content: https://github.com/umbraco/Umbraco-CMS/blob/75c2b07ad3a093b5b65b6ebd45697687c062f62a/src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/ListImagesFromMediaFolder.cshtml and it was also discussed here for example: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/63217-list-files-from-an-folder-in-umbraco

    var allVideos = Umbraco.TypedMediaAtRoot().DescendantsOrSelf("video");
    foreach (var video in allVideos)
    {
        <div>@video.Name</div>
    }
    

    3) You can create custom Examine index containing files and performing fast search (&listing) directly in there. More about creating custom indexes can be found here: https://umbraco.com/follow-us/blog-archive/2011/9/16/examining-examine/

    4) Umbraco REST API! Wrote about it in 24days.in/umbraco series: http://24days.in/umbraco/2015/umbraco-rest-api/. Maybe better for scenario when we need to access this data from front-end, but also a good way to extend our skills in using it.

    So, depending from expectations and requirements, you can choose the best option for yourself :) Happy coding!

Please Sign in or register to post replies

Write your reply to:

Draft