How to access subfolders from media library?

Jovan Bosnic asked on September 21, 2021 12:55

Hello everyone, I have a problem with accessing media library subfolders on mvc. What I would like to do is to have MediaLibrary

-> subfolder1
    ->subsubfolder
        ->subsubsubfolder
     ->subsubfolder2
-> subfolder2
.....

And I want to iterate through every subfolder and its subfolders in media library. Here is we transfer screenshot of my media library in Kentico. I have tried with MediaFileInfoProvider.GetMediaFiles().WhereStartsWith("FilePath","Subfolder1").WhereEquals("FileLibraryID", mediaLibrary.LibraryId) but i don't know exact names of subfolders and there is going to be a lot of them. So i would like to iterate through each subfolder and its children if possible?

https://we.tl/t-azdUkpGHPA

Recent Answers


Dmitry Bastron answered on September 21, 2021 15:06

Hi Jovan,

You can get a full list of subfolders from your query like this:

var allSubfolders = MediaFileInfoProvider.GetMediaFiles()
    .WhereStartsWith("FilePath", "Subfolder1")
    .WhereEquals("FileLibraryID", mediaLibrary.LibraryId)
    .Columns("FilePath")
    .Select(x => x.FilePath)
    .Distinct()
    .OrderBy(x => x)
    .ToList();
0 votesVote for this answer Mark as a Correct answer

Jovan Bosnic answered on September 21, 2021 15:10 (last edited on September 21, 2021 15:11)

Thanks so much for answer. But is there a way to iterate through all subfolders without entering subfolder name and get its content? To go through them as children?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on September 21, 2021 15:14

There isn't any standard helper for this, unfortunately. But you can retrieve all media files and apply LINQ's .GroupBy(x => x.FilePath) and then iterate through those items.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.