Search.../

searchFolders( )

Searches the Media Manager and returns a list of folders that match the terms specified in the parameters.

Description

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

If no parameters are specified, the function returns all folders in the MEDIA_ROOT folder.

Admin Method

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

Syntax

function searchFolders(options: SearchFoldersOptions): Promise<SearchFoldersResponse>

searchFolders Parameters

NAME
TYPE
DESCRIPTION
options
Optional
SearchFoldersOptions

Options specifying which folders to search.

Returns

Return Type:

Promise<
SearchFoldersResponse
>
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?

Search folders (dashboard page code)

Copy Code
1import { folders } from 'wix-media.v2';
2
3async function mySearchFoldersFunction() {
4 try {
5 const foldersFound = await folders.searchFolders();
6
7 console.log("Folders found in search:", foldersFound);
8 return foldersFound;
9 } catch (error) {
10 console.error(error);
11 // Handle the error
12 }
13}
14
15/* Promise resolves to:
16 * {
17 * "folders": [
18 * {
19 * "_createdDate": "2023-08-14T07:41:19.000Z",
20 * "_id": "103601562ec94214bee61f470b403dd5",
21 * "_updatedDate": "2023-08-21T11:09:08.000Z",
22 * "displayName": "Pictures",
23 * "parentFolderId": "media-root",
24 * "state": "OK"
25 * },
26 * {
27 * "_createdDate": "2023-08-21T05:34:03.000Z",
28 * "_id": "302fc049d70c41dea33fa4a27ab481ba",
29 * "_updatedDate": "2023-08-21T05:34:03.000Z",
30 * "displayName": "Videos",
31 * "parentFolderId": "media-root",
32 * "state": "OK"
33 * }
34 * ],
35 * "nextCursor": {
36 * "cursors": {
37 * "next": ""
38 * },
39 * "hasNext": false
40 * }
41 * }
42 */
Search 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 mySearchFoldersFunction = webMethod(Permissions.Anyone, async () => {
6 try {
7 const elevatedSearchFolders = elevate(folders.searchFolders);
8 const foldersFound = await elevatedSearchFolders();
9
10 console.log("Folders found in search:", foldersFound);
11 return foldersFound;
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-21T11:09:08.000Z",
25 * "displayName": "Pictures",
26 * "parentFolderId": "media-root",
27 * "state": "OK"
28 * },
29 * {
30 * "_createdDate": "2023-08-21T05:34:03.000Z",
31 * "_id": "302fc049d70c41dea33fa4a27ab481ba",
32 * "_updatedDate": "2023-08-21T05:34:03.000Z",
33 * "displayName": "Videos",
34 * "parentFolderId": "media-root",
35 * "state": "OK"
36 * }
37 * ],
38 * "nextCursor": {
39 * "cursors": {
40 * "next": ""
41 * },
42 * "hasNext": false
43 * }
44 * }
45 */
46
Search for 2 folders by key-word, returned alphabetically

When no rootFolder is specified in the options parameter, the search returns results from the 'MEDIA_ROOT' folder.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { folders } from 'wix-media.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample options value:
6 * {
7 * search: 'mountains',
8 * sort: {
9 * fieldName: 'updatedDate',
10 * order: 'ASC'
11 * },
12 * paging: {
13 * limit: 2
14 * }
15 * }
16 */
17
18export const mySearchFoldersFunction = webMethod(Permissions.Anyone, async (options) => {
19 try {
20 const elevatedSearchFolders = elevate(folders.searchFolders);
21 const foldersFound = await elevatedSearchFolders(options);
22
23 console.log("Folders found in search:", foldersFound);
24 return foldersFound;
25 } catch (error) {
26 console.error(error);
27 // Handle the error
28 }
29});
30
31/* Promise resolves to:
32 * {
33 * "folders": [
34 * {
35 * "_createdDate": "2023-08-21T09:32:34.000Z",
36 * "_id": "7984b3c5454e4371acbd4f4eedde96bc",
37 * "_updatedDate": "2023-08-21T11:10:51.000Z",
38 * "displayName": "mountains",
39 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
40 * "state": "OK"
41 * },
42 * {
43 * "_createdDate": "2023-08-24T11:26:26.000Z",
44 * "_id": "742d4a0ee5884b119292d52ed964f14d",
45 * "_updatedDate": "2023-08-24T11:26:26.000Z",
46 * "displayName": "mountain_videos",
47 * "parentFolderId": "302fc049d70c41dea33fa4a27ab481ba",
48 * "state": "OK"
49 * }
50 * ],
51 * "nextCursor": {
52 * "cursors": {
53 * "next": "eyJ0b3RhbCI6IDEsICJvZmZzZXQiOiAyfQ=="
54 * },
55 * "hasNext": true
56 * }
57 * }
58 */
59
Bulk delete all folders

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

Copy Code
1/****************************************
2 * Backend code - delete-folders.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 deleteFolders = webMethod(Permissions.Anyone, async (folderIds) => {
10 try {
11 const elevatedBulkDeleteFolders = elevate(folders.bulkDeleteFolders);
12 const deletedFolders = await elevatedBulkDeleteFolders(folderIds);
13
14 console.log('Successfully moved folders to trash.');
15 return deletedFolders;
16 } catch (error) {
17 console.error(error);
18 }
19});
20
21export const listFolders = webMethod(Permissions.Anyone, async () => {
22 try {
23 const elevatedListFolders = elevate(folders.listFolders)
24 const foldersList = await elevatedListFolders();
25
26 return foldersList;
27 } catch (error) {
28 console.error(error);
29 }
30});
31
32export const searchFolders = webMethod(Permissions.Anyone, async (options) => {
33 try {
34 const elevatedSearchFolders = elevate(folders.searchFolders)
35 const foldersFound = await elevatedSearchFolders(options);
36
37 return foldersFound;
38 } catch (error) {
39 console.error(error);
40 }
41});
42
43
44/*************
45 * Page code *
46 ************/
47
48import { deleteFolders, listFolders, searchFolders } from 'backend/delete-folders.web';
49
50$w.onReady(async () => {
51 await populateFoldersDropdown();
52
53 $w('#bulkDelete').onClick(async () => {
54 const searchOptions = { rootFolder: $w('#foldersDropdown').value};
55
56 const foldersToDelete = await searchFolders(searchOptions);
57 const foldersIdsToDelete = foldersToDelete.map((folder) => {
58 return folder._id;
59 });
60 await deleteFolders(foldersIdsToDelete);
61
62 console.log(`Successfully deleted all folders found in ${$w('#foldersDropdown').label}.`)
63 $w('#bulkDeleteSuccessMsg').show();
64 });
65});
66
67async function populateFoldersDropdown() {
68 const folders = await listFolders();
69 const dropdownOptions = folders.map((folder) => {
70 return {
71 label: folder.displayName,
72 value: folder._id
73 };
74 });
75
76 $w('#foldersDropdown').options = dropdownOptions;
77};