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.
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 specifying which folders to search.
Returns
Return Type:
NAME
TYPE
DESCRIPTION
Information about the folders in the requested folder.
The next cursor if it exists.
Was this helpful?
1import { folders } from 'wix-media.v2';23async function mySearchFoldersFunction() {4 try {5 const foldersFound = await folders.searchFolders();67 console.log("Folders found in search:", foldersFound);8 return foldersFound;9 } catch (error) {10 console.error(error);11 // Handle the error12 }13}1415/* 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": false40 * }41 * }42 */
1import { Permissions, webMethod } from 'wix-web-module';2import { folders } from 'wix-media.v2';3import { elevate } from 'wix-auth';45export const mySearchFoldersFunction = webMethod(Permissions.Anyone, async () => {6 try {7 const elevatedSearchFolders = elevate(folders.searchFolders);8 const foldersFound = await elevatedSearchFolders();910 console.log("Folders found in search:", foldersFound);11 return foldersFound;12 } catch (error) {13 console.error(error);14 // Handle the error15 }16});1718/* 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": false43 * }44 * }45 */46
When no rootFolder
is specified in the options parameter, the search returns results from the 'MEDIA_ROOT' folder.
1import { Permissions, webMethod } from 'wix-web-module';2import { folders } from 'wix-media.v2';3import { elevate } from 'wix-auth';45/* Sample options value:6 * {7 * search: 'mountains',8 * sort: {9 * fieldName: 'updatedDate',10 * order: 'ASC'11 * },12 * paging: {13 * limit: 214 * }15 * }16 */1718export const mySearchFoldersFunction = webMethod(Permissions.Anyone, async (options) => {19 try {20 const elevatedSearchFolders = elevate(folders.searchFolders);21 const foldersFound = await elevatedSearchFolders(options);2223 console.log("Folders found in search:", foldersFound);24 return foldersFound;25 } catch (error) {26 console.error(error);27 // Handle the error28 }29});3031/* 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": true56 * }57 * }58 */59
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.
1/****************************************2 * Backend code - delete-folders.web.js *3 ***************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { folders } from 'wix-media.v2';7import { elevate } from 'wix-auth';89export const deleteFolders = webMethod(Permissions.Anyone, async (folderIds) => {10 try {11 const elevatedBulkDeleteFolders = elevate(folders.bulkDeleteFolders);12 const deletedFolders = await elevatedBulkDeleteFolders(folderIds);1314 console.log('Successfully moved folders to trash.');15 return deletedFolders;16 } catch (error) {17 console.error(error);18 }19});2021export const listFolders = webMethod(Permissions.Anyone, async () => {22 try {23 const elevatedListFolders = elevate(folders.listFolders)24 const foldersList = await elevatedListFolders();2526 return foldersList;27 } catch (error) {28 console.error(error);29 }30});3132export const searchFolders = webMethod(Permissions.Anyone, async (options) => {33 try {34 const elevatedSearchFolders = elevate(folders.searchFolders)35 const foldersFound = await elevatedSearchFolders(options);3637 return foldersFound;38 } catch (error) {39 console.error(error);40 }41});424344/*************45 * Page code *46 ************/4748import { deleteFolders, listFolders, searchFolders } from 'backend/delete-folders.web';4950$w.onReady(async () => {51 await populateFoldersDropdown();5253 $w('#bulkDelete').onClick(async () => {54 const searchOptions = { rootFolder: $w('#foldersDropdown').value};5556 const foldersToDelete = await searchFolders(searchOptions);57 const foldersIdsToDelete = foldersToDelete.map((folder) => {58 return folder._id;59 });60 await deleteFolders(foldersIdsToDelete);6162 console.log(`Successfully deleted all folders found in ${$w('#foldersDropdown').label}.`)63 $w('#bulkDeleteSuccessMsg').show();64 });65});6667async function populateFoldersDropdown() {68 const folders = await listFolders();69 const dropdownOptions = folders.map((folder) => {70 return {71 label: folder.displayName,72 value: folder._id73 };74 });7576 $w('#foldersDropdown').options = dropdownOptions;77};