Search.../

searchFolders( )

Developer Preview

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

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

Copy Code
1import { folders } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample options value:
5 * {
6 * search: 'mountains',
7 * sort: {
8 * fieldName: 'updatedDate',
9 * order: 'ASC'
10 * },
11 * paging: {
12 * limit: 2
13 * }
14 * }
15 */
16
17async function mySearchFoldersFunction(options) {
18 try {
19 const elevatedSearchFolders = elevate(folders.searchFolders);
20 const foldersFound = await elevatedSearchFolders(options);
21
22 console.log("Folders found in search:", foldersFound);
23 return foldersFound;
24 } catch (error) {
25 console.error(error);
26 // Handle the error
27 }
28}
29
30/* Promise resolves to:
31 * {
32 * "folders": [
33 * {
34 * "_createdDate": "2023-08-21T09:32:34.000Z",
35 * "_id": "7984b3c5454e4371acbd4f4eedde96bc",
36 * "_updatedDate": "2023-08-21T11:10:51.000Z",
37 * "displayName": "mountains",
38 * "parentFolderId": "103601562ec94214bee61f470b403dd5",
39 * "state": "OK"
40 * },
41 * {
42 * "_createdDate": "2023-08-24T11:26:26.000Z",
43 * "_id": "742d4a0ee5884b119292d52ed964f14d",
44 * "_updatedDate": "2023-08-24T11:26:26.000Z",
45 * "displayName": "mountain_videos",
46 * "parentFolderId": "302fc049d70c41dea33fa4a27ab481ba",
47 * "state": "OK"
48 * }
49 * ],
50 * "nextCursor": {
51 * "cursors": {
52 * "next": "eyJ0b3RhbCI6IDEsICJvZmZzZXQiOiAyfQ=="
53 * },
54 * "hasNext": true
55 * }
56 * }
57 */
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.jsw *
3 *************************************/
4
5import { folders } from 'wix-media.v2';
6import { elevate } from 'wix-auth';
7
8export async function deleteFolders(folderIds) {
9 try {
10 const elevatedBulkDeleteFolders = elevate(folders.bulkDeleteFolders);
11 const deletedFolders = await elevatedBulkDeleteFolders(folderIds);
12
13 console.log('Successfully moved folders to trash.');
14 return deletedFolders;
15 } catch (error) {
16 console.error(error);
17 }
18}
19
20export async function listFolders() {
21 try {
22 const elevatedListFolders = elevate(folders.listFolders)
23 const foldersList = await elevatedListFolders();
24
25 return foldersList;
26 } catch (error) {
27 console.error(error);
28 }
29}
30
31export async function searchFolders(options) {
32 try {
33 const elevatedSearchFolders = elevate(folders.searchFolders)
34 const foldersFound = await elevatedSearchFolders(options);
35
36 return foldersFound;
37 } catch (error) {
38 console.error(error);
39 }
40}
41
42
43/*************
44 * Page code *
45 ************/
46
47import { deleteFolders, listFolders, searchFolders } from 'backend/delete-folders';
48
49$w.onReady(async () => {
50 await populateFoldersDropdown();
51
52 $w('#bulkDelete').onClick(async () => {
53 const searchOptions = { rootFolder: $w('#foldersDropdown').value};
54
55 const foldersToDelete = await searchFolders(searchOptions);
56 const foldersIdsToDelete = foldersToDelete.map((folder) => {
57 return folder._id;
58 });
59 await deleteFolders(foldersIdsToDelete);
60
61 console.log(`Successfully deleted all folders found in ${$w('#foldersDropdown').label}.`)
62 $w('#bulkDeleteSuccessMsg').show();
63 });
64});
65
66async function populateFoldersDropdown() {
67 const folders = await listFolders();
68 const dropdownOptions = folders.map((folder) => {
69 return {
70 label: folder.displayName,
71 value: folder._id
72 };
73 });
74
75 $w('#foldersDropdown').options = dropdownOptions;
76};
77