Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-02T16:17:53.167Z
Answers.LastActivityDate: 2023-01-02T16:17:53.167Z
Answers.UserDisplayName: MoritzLost
Body: <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>
  
Tags: <query><files><subfolder><volumes>
Title: How query all folders of a volume?
ThreadText: # 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) %}
  <ul class="files_folders">
      {% for folder in folders %}
          <li><a href="{{ url('folderId=' ~ folder.id) }}">{{ folder.title }}</a></li>
      {% endfor %}
  </ul>
  <ul class="files__list">
      {% for asset in assets %}
          <li><a href="{{ craft.digitalDownload.url(asset, options) }}">{{ asset.title }}</a></li>
      {% endfor %}
  </ul>
  
  ```
  
  **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`](https://docs.craftcms.com/api/v4/craft-services-assets.html#method-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`](https://docs.craftcms.com/api/v4/craft-services-assets.html#method-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`](https://docs.craftcms.com/api/v4/craft-models-volumefolder.html) models that you can iterate through. If you need a hierarchical list, you can access the children with the [`VolumeFolder::children` property](https://docs.craftcms.com/api/v4/craft-models-volumefolder.html#children), or use [`craft\services\Assets::getFolderTreeByFolderId`](https://docs.craftcms.com/api/v4/craft-services-assets.html#method-getfoldertreebyfolderid) to get a hierarchical list. To find all assets in a given folder, use an asset query with the `folderId` parameter.
  
  
  ---
  
  