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).
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).
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!
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
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).
Then code could looks like:
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
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!
is working on a reply...