<p>You can use <a href="https://docs.craftcms.com/api/v4/craft-services-assets.html#method-getalldescendantfolders" rel="nofollow noreferrer"><code>craft\services\Assets::getAllDescendantFolders</code></a> to get a list of folders in a given folder. To list all folders in the root folder of the volume, you first need to find the root folder using <a href="https://docs.craftcms.com/api/v4/craft-services-assets.html#method-getrootfolderbyvolumeid" rel="nofollow noreferrer"><code>craft\services\Assets::getRootFolderByVolumeId</code></a>.</p>
<pre><code>{% set myVolume = craft.app.volumes.getVolumeByHandle('handle') %}
{% set rootFolder = craft.app.assets.getRootFolderByVolumeId(myVolume.id) %}
{% set foldersInRootFolder = craft.app.assets.getAllDescendantFolders(rootFolder) %}
</code></pre>
<p>This will return an array of <a href="https://docs.craftcms.com/api/v4/craft-models-volumefolder.html" rel="nofollow noreferrer"><code>VolumeFolder</code></a> models that you can iterate through. If you need a hierarchical list, you can access the children with the <a href="https://docs.craftcms.com/api/v4/craft-models-volumefolder.html#children" rel="nofollow noreferrer"><code>VolumeFolder::children</code> property</a>, or use <a href="https://docs.craftcms.com/api/v4/craft-services-assets.html#method-getfoldertreebyfolderid" rel="nofollow noreferrer"><code>craft\services\Assets::getFolderTreeByFolderId</code></a> to get a hierarchical list. To find all assets in a given folder, use an asset query with the <code>folderId</code> parameter.</p>

CC BY-SA 4.0
2023-01-02T16:17:53.167Z
2023-01-02T16:17:53.167Z
MoritzLost
<p>I want to query a list of all folders in a specific volume in my front-end template (loggedin users only). I'll need the folderId to display all files of the selected folder.</p>
<p>The idea is to show a list of folders on the left side, right displays a list of files in the selected folder on the left side. The folders need to be the same as in the backend.</p>
<pre><code>{% set assets = craft.assets().volume('handle').folderId(folderId) %}
&lt;ul class=&quot;files_folders&quot;&gt;
    {% for folder in folders %}
        &lt;li&gt;&lt;a href=&quot;{{ url('folderId=' ~ folder.id) }}&quot;&gt;{{ folder.title }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
&lt;ul class=&quot;files__list&quot;&gt;
    {% for asset in assets %}
        &lt;li&gt;&lt;a href=&quot;{{ craft.digitalDownload.url(asset, options) }}&quot;&gt;{{ asset.title }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<query><files><subfolder><volumes>
How query all folders of a volume?
# How query all folders of a volume?

(Asked by: username_0 on 2023-01-02)

I want to query a list of all folders in a specific volume in my front-end template (loggedin users only). I'll need the folderId to display all files of the selected folder.
The idea is to show a list of folders on the left side, right displays a list of files in the selected folder on the left side. The folders need to be the same as in the backend.
{% set assets = craft.assets().volume('handle').folderId(folderId) %}

    {% for folder in folders %}
        {{ folder.title }}
    {% endfor %}


    {% for asset in assets %}
        {{ asset.title }}
    {% endfor %}

**Tags:** query, files, subfolder, volumes

---

## 1 answers

### Answer by username_1 on 2023-01-02. Score: 2 | Accepted answer

You can use craft\services\Assets::getAllDescendantFolders to get a list of folders in a given folder. To list all folders in the root folder of the volume, you first need to find the root folder using craft\services\Assets::getRootFolderByVolumeId.
{% set myVolume = craft.app.volumes.getVolumeByHandle('handle') %}
{% set rootFolder = craft.app.assets.getRootFolderByVolumeId(myVolume.id) %}
{% set foldersInRootFolder = craft.app.assets.getAllDescendantFolders(rootFolder) %}

This will return an array of VolumeFolder models that you can iterate through. If you need a hierarchical list, you can access the children with the VolumeFolder::children property, or use craft\services\Assets::getFolderTreeByFolderId to get a hierarchical list. To find all assets in a given folder, use an asset query with the folderId parameter.


---

