Search.../

listFolders( )

Retrieves a list of folders in the Media Manager.

Description

The listFolders() function returns a Promise that resolves to information about the specified folders and cursor information.

To retrieve a list of folders within a specific folder in the Media Manager, pass the specific folder's ID in the parentFolderId parameter. If no folder is specified, the function retrieves only the list of folders within the root folder of the Media Manager.

To retrieve a list of (non-permanently) deleted folders, use the listDeletedFolders() function.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function listFolders(options: ListFoldersOptions): Promise<ListFoldersResponse>

listFolders Parameters

NAME
TYPE
DESCRIPTION
options
Optional
ListFoldersOptions

Options to use when listing folders from the Media Manager.

Returns

Return Type:

Promise<
ListFoldersResponse
>
NAME
TYPE
DESCRIPTION
folders
Array<
Folder
>

Information about the folders in the requested folder.

nextCursor
PagingMetadataV2

The next cursor if it exists.

Was this helpful?

List folders (dashboard page code)

Copy Code
1import { folders } from 'wix-media.v2';
2
3async function myListFoldersFunction()
4{
5 try {
6 const foldersList = await folders.listFolders();
7
8 console.log("Folders:", foldersList);
9 return foldersList;
10 } catch (error) {
11 console.error(error);
12 // Handle the error
13 }
14}
15
16/* Promise resolves to:
17 * {
18 * "folders": [
19 * {
20 * "_createdDate": "2023-08-14T07:41:19.000Z",
21 * "_id": "103601562ec94214bee61f470b403dd5",
22 * "_updatedDate": "2023-08-14T07:41:19.000Z",
23 * "displayName": "Pictures",
24 * "parentFolderId": "media-root",
25 * "state": "OK"
26 * },
27 * {
28 * "_createdDate": "2023-08-14T07:38:31.000Z",
29 * "_id": "c956d69906414e7faf8a0ad81117b17d",
30 * "_updatedDate": "2023-08-14T07:38:31.000Z",
31 * "displayName": "Videos",
32 * "parentFolderId": "media-root",
33 * "state": "OK"
34 * },
35 * {
36 * "_createdDate": "2023-08-09T08:57:12.000Z",
37 * "_id": "b2bc72834460412494c93617d88b8c89",
38 * "_updatedDate": "2023-08-09T08:57:12.000Z",
39 * "displayName": "Documents",
40 * "parentFolderId": "media-root",
41 * "state": "OK"
42 * }
43 * ],
44 * "nextCursor": {
45 * "cursors": {
46 * "next": ""
47 * },
48 * "hasNext": false
49 * }
50 * }
51 */
List folders (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { folders } from 'wix-media.v2';
3import { elevate } from 'wix-auth';
4
5export const myListFoldersFunction = webMethod(Permissions.Anyone, async () => {
6 try {
7 const elevatedListFolders = elevate(folders.listFolders);
8 const folderList = await elevatedListFolders();
9
10 console.log("Folders:", folderList);
11 return folderList;
12 } catch (error) {
13 console.error(error);
14 // Handle the error
15 }
16});
17
18/* Promise resolves to:
19 * {
20 * "folders": [
21 * {
22 * "_createdDate": "2023-08-14T07:41:19.000Z",
23 * "_id": "103601562ec94214bee61f470b403dd5",
24 * "_updatedDate": "2023-08-14T07:41:19.000Z",
25 * "displayName": "Pictures",
26 * "parentFolderId": "media-root",
27 * "state": "OK"
28 * },
29 * {
30 * "_createdDate": "2023-08-14T07:38:31.000Z",
31 * "_id": "c956d69906414e7faf8a0ad81117b17d",
32 * "_updatedDate": "2023-08-14T07:38:31.000Z",
33 * "displayName": "Videos",
34 * "parentFolderId": "media-root",
35 * "state": "OK"
36 * },
37 * {
38 * "_createdDate": "2023-08-09T08:57:12.000Z",
39 * "_id": "b2bc72834460412494c93617d88b8c89",
40 * "_updatedDate": "2023-08-09T08:57:12.000Z",
41 * "displayName": "Documents",
42 * "parentFolderId": "media-root",
43 * "state": "OK"
44 * }
45 * ],
46 * "nextCursor": {
47 * "cursors": {
48 * "next": ""
49 * },
50 * "hasNext": false
51 * }
52 * }
53 */
List 5 folders found in a parent folder, sorted alphabetically by name

Copy Code
1import { folders } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3import { Permissions, webMethod } from 'wix-web-module';
4
5/* Sample options value:
6 * {
7 * paging: {
8 * limit: 5
9 * },
10 * parentFolderId : '103601562ec94214bee61f470b403dd5',
11 * sort : {
12 * fieldName : 'displayName',
13 * order : 'ASC'
14 * }
15 * }
16 */
17
18export const myListFoldersFunction = webMethod(Permissions.Anyone, async (options) => {
19 try {
20 const result = await folders.listFolders(options);
21
22 const folderList = result.folders;
23
24 folderList.forEach(element => {
25 console.log(element.displayName);
26 });
27
28 return result;
29 } catch (error) {
30 console.error(error);
31 // Handle the error
32 }
33});
34
35/* Promise resolves to:
36 * {
37 * "folders": [
38 * {
39 * "_createdDate": "2023-08-21T09:33:47.000Z",
40 * "_id": "0cfdf68ac3b9453b81cfe171caf40b0c",
41 * "_updatedDate": "2023-08-21T09:33:47.000Z",
42 * "displayName": "31-08",
43 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
44 * "state": "OK"
45 * },
46 * {
47 * "_createdDate": "2023-08-21T09:33:30.000Z",
48 * "_id": "193161aeef8f4b80b97d7032fb2c4f0a",
49 * "_updatedDate": "2023-08-21T11:09:58.000Z",
50 * "displayName": "Water",
51 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
52 * "state": "OK"
53 * },
54 * {
55 * "_createdDate": "2023-08-21T09:32:39.000Z",
56 * "_id": "4b9e2dd061de4f3e80e3ada23b0fb440",
57 * "_updatedDate": "2023-08-21T09:32:39.000Z",
58 * "displayName": "city",
59 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
60 * "state": "OK"
61 * },
62 * {
63 * "_createdDate": "2023-08-21T09:56:43.000Z",
64 * "_id": "6a86df2ce32c4d279c21fbdd2887c18b",
65 * "_updatedDate": "2023-08-21T09:56:43.000Z",
66 * "displayName": "docs1",
67 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
68 * "state": "OK"
69 * },
70 * {
71 * "_createdDate": "2023-08-21T09:33:10.000Z",
72 * "_id": "5d0625ed28494bf7938c03e9a21130ba",
73 * "_updatedDate": "2023-08-21T09:33:24.000Z",
74 * "displayName": "docs2",
75 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
76 * "state": "OK"
77 * },
78 * {
79 * "_createdDate": "2023-08-21T09:32:34.000Z",
80 * "_id": "7984b3c5454e4371acbd4f4eedde96bc",
81 * "_updatedDate": "2023-08-21T11:10:51.000Z",
82 * "displayName": "mountains",
83 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
84 * "state": "OK"
85 * }
86 * ],
87 * "nextCursor": {
88 * "cursors": {
89 * "next": ""
90 * },
91 * "hasNext": false
92 * }
93 * }
94 */
Create a folder from visitor input

This code is an example of a page on which a visitor chooses a folder from a dropdown list, and then creates a folder within that folder.

Copy Code
1/***************************************
2 * Backend code - create-folder.web.js *
3 **************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { folders } from 'wix-media.v2';
7import { elevate } from 'wix-auth';
8
9export const listFolders = webMethod(Permissions.Anyone, async () => {
10 try {
11 const elevatedListFolders = elevate(folders.listFolders)
12 const foldersList = await elevatedListFolders();
13
14 return foldersList;
15 } catch (error) {
16 console.error(error);
17 }
18});
19
20export const createFolder = webMethod(Permissions.Anyone, async (displayName, options) => {
21 try {
22 const elevateCreateFolder = elevate(folders.createFolder);
23 const newFolder = await elevateCreateFolder(displayName, options);
24
25 return newFolder;
26 } catch (err){
27 console.error(err);
28 }
29});
30
31/*************
32 * Page code *
33 ************/
34
35import { createFolder, listFolders } from 'backend/create-folder.web';
36
37$w.onReady(async () => {
38 await populateFoldersDropdown();
39
40 $w('#createFolder').onClick(async () => {
41 const folderName = $w('folderName').value;
42 const createOptions = {parentFolderId: $w('foldersDropdown').value};
43 const newFolder = await createFolder(folderName, createOptions);
44
45 console.log(`Successfully created new folder "${folderName}" in ${$w('#foldersDropdown').label}.`)
46 $w('#createdFolderSuccessMsg').show();
47 });
48});
49
50async function populateFoldersDropdown() {
51 const folders = await listFolders();
52 const dropdownOptions = folders.map((folder) => {
53 return {
54 label: folder.displayName,
55 value: folder._id
56 };
57 });
58
59 $w('#foldersDropdown').options = dropdownOptions;
60};