> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# SearchFolders

# Package: mediaManager

# Namespace: FoldersService

# Method link: https://dev.wix.com/docs/api-reference/assets/media/media-manager/folders/search-folders.md

## Permission Scopes:
Read Media Manager: SCOPE.DC-MEDIA.READ-MEDIAMANAGER

## Introduction

Retrieves a list of up to 100 folders with the specified filtering, sorting, and cursor paging.

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

---

## REST API

### Schema

```
 Method: searchFolders
 Description: Retrieves a list of up to 100 folders with the specified filtering, sorting, and cursor paging.  If no parameters are specified, the method returns all folders in the `MEDIA_ROOT` folder.  ## Defaults  Search Folders has the following default setting, which you can override: + Sorted by `updatedDate` in descending order.  ## Filters  When using filters for dates, you must use [UTC time](https://en.wikipedia.org/wiki/Coordinated_Universal_Time).  ## See also  To learn about working with *Search* methods, see [API Query Language](https://dev.wix.com/docs/rest/articles/get-started/api-query-language.md) and [Sorting and Paging](https://dev.wix.com/docs/rest/articles/get-started/sorting-and-paging.md).
 URL: https://www.wixapis.com/site-media/v1/folders/search
 Method: POST
 Method parameters:
   param name: paging | type: CursorPaging    
        - name: limit | type: integer | description: Maximum number of items to return in the results.  | validation: minimum 0, maximum 100, format int32
        - name: cursor | type: string | description: Pointer to the next or previous page in the list of results.  Pass the relevant cursor token from the `pagingMetadata` object in the previous call's response. Not relevant for the first request.  | validation: maxLength 16000
   param name: rootFolder | type: RootFolder    
      - enum:
           MEDIA_ROOT - Contains all files and folders in the Media Manager's **Site Files** tab in the UI.
           TRASH_ROOT - Contains all files and folders in the Media Manager's **Trash** tab in the UI.
           VISITOR_UPLOADS_ROOT - Contains all files and folders created by site visitors or members.
   param name: search | type: search | description: Free text to match in searchable fields.  For example, search for specific values in a folder's `displayName`.  | validation: maxLength 200
   param name: sort | type: Sorting    
        - name: fieldName | type: string | description: Name of the field to sort by.  | validation: maxLength 512
        - name: order | type: SortOrder | description: Sort order.  
             - enum: ASC, DESC
        - name: origin | type: AddressLocation | description: Origin point for geo-distance sorting on a GEO field results are ordered by distance from this point (ASC = nearest first, DESC = farthest first).  
           - name: latitude | type: number | description: Address latitude.  | validation: format double
           - name: longitude | type: number | description: Address longitude.  | validation: format double
 Return type: SearchFoldersResponse
  - name: folders | type: array<Folder> | description: Retrieved folders that match the search criteria specified in the request.  Each folder includes all standard folder information.  | validation: maxItems 200
     - name: id | type: string | description: Folder GUID. Generated when a folder is created in the Media Manager.  | validation: maxLength 100
     - name: displayName | type: string | description: Folder name as it appears in the Media Manager.  | validation: maxLength 1000
     - name: parentFolderId | type: string | description: GUID of the folder's parent folder. <br /> Default: `media-root` folder.  | validation: maxLength 100
     - name: createdDate | type: string | description: Date the folder was created.  | read-only: true | validation: format date-time
     - name: updatedDate | type: string | description: Date the folder was updated.  | read-only: true | validation: format date-time
     - name: state | type: State | description: State of the folder.  | read-only: true 
         - enum:
         -     OK: Folder is ready for use.
         -     DELETED: Folder is deleted.
     - name: siteId | type: string | description: Site GUID the folder belongs to.  | read-only: true | validation: maxLength 100
  - name: nextCursor | type: PagingMetadataV2 | description: The next cursor if it exists.  
     - name: total | type: integer | description: Total number of items that match the query. Returned if offset paging is used and the `tooManyToCount` flag is not set.  | validation: format int32
     - name: cursors | type: Cursors | description: Cursors to navigate through the result pages using `next` and `prev`. Returned if cursor paging is used.  
        - name: next | type: string | description: Cursor string pointing to the next page in the list of results.  | validation: maxLength 16000


```

### Examples

### Search folders
```curl
curl -X POST \
  'https://www.wixapis.com/site-media/v1/folders/search' \
  -H 'Authorization: <AUTH>'
  --data-binary '{
    "rootFolder": "TRASH_ROOT",
    "sort": {
      "fieldName": "displayName",
      "order": "DESC"
    },
    "paging": {
      "limit": 20
    }
  }'
```

---

## JavaScript SDK

### Schema

```
 Method: wixClientAdmin.media.folders.searchFolders(options)
 Description: Retrieves a list of up to 100 folders with the specified filtering, sorting, and cursor paging.  If no parameters are specified, the method returns all folders in the `MEDIA_ROOT` folder.  ## Defaults  Search Folders has the following default setting, which you can override: + Sorted by `updatedDate` in descending order.  ## Filters  When using filters for dates, you must use [UTC time](https://en.wikipedia.org/wiki/Coordinated_Universal_Time).  ## See also  To learn about working with *Search* methods, see [API Query Language](https://dev.wix.com/docs/rest/articles/get-started/api-query-language.md) and [Sorting and Paging](https://dev.wix.com/docs/rest/articles/get-started/sorting-and-paging.md).
 Method parameters:
   param name: options | type: SearchFoldersOptions  none  
        - name: rootFolder | type: RootFolder | description: The root folder in the Media Manager to search in. For more information, see Root Folders in [Terminology](https://dev.wix.com/docs/rest/assets/media/media-manager/introduction.md#terminology).   Default: `MEDIA_ROOT`.  
             - enum:
             -     MEDIA_ROOT: Contains all files and folders in the Media Manager's **Site Files** tab in the UI.
             -     TRASH_ROOT: Contains all files and folders in the Media Manager's **Trash** tab in the UI.
             -     VISITOR_UPLOADS_ROOT: Contains all files and folders created by site visitors or members.
        - name: sort | type: Sorting | description: Field name and order to sort by. One of:  `displayName` `updatedDate`  Default: `updatedDate` in `desc` order.  
           - name: fieldName | type: string | description: Name of the field to sort by.  | validation: maxLength 512
           - name: order | type: SortOrder | description: Sort order.  
                 - enum: ASC, DESC
           - name: origin | type: AddressLocation | description: Origin point for geo-distance sorting on a GEO field results are ordered by distance from this point (ASC = nearest first, DESC = farthest first).  
              - name: latitude | type: number | description: Address latitude.  
              - name: longitude | type: number | description: Address longitude.  
        - name: paging | type: CursorPaging | description: Cursor and paging information.  
           - name: limit | type: integer | description: Maximum number of items to return in the results.  | validation: minimum 0, maximum 100, format int32
           - name: cursor | type: string | description: Pointer to the next or previous page in the list of results.  Pass the relevant cursor token from the `pagingMetadata` object in the previous call's response. Not relevant for the first request.  | validation: maxLength 16000
        - name: search | type: string | description: Free text to match in searchable fields.  For example, search for specific values in a folder's `displayName`.  | validation: maxLength 200
 Return type: PROMISE<SearchFoldersResponse>
  - name: folders | type: array<Folder> | description: Retrieved folders that match the search criteria specified in the request.  Each folder includes all standard folder information.  | validation: maxItems 200
     - name: _id | type: string | description: Folder GUID. Generated when a folder is created in the Media Manager.  | validation: maxLength 100
     - name: displayName | type: string | description: Folder name as it appears in the Media Manager.  | validation: maxLength 1000
     - name: parentFolderId | type: string | description: GUID of the folder's parent folder. <br /> Default: `media-root` folder.  | validation: maxLength 100
     - name: _createdDate | type: Date | description: Date the folder was created.  | read-only: true 
     - name: _updatedDate | type: Date | description: Date the folder was updated.  | read-only: true 
     - name: state | type: State | description: State of the folder.  | read-only: true 
         - enum:
         -     OK: Folder is ready for use.
         -     DELETED: Folder is deleted.
     - name: siteId | type: string | description: Site GUID the folder belongs to.  | read-only: true | validation: maxLength 100
  - name: nextCursor | type: PagingMetadataV2 | description: The next cursor if it exists.  
     - name: total | type: integer | description: Total number of items that match the query. Returned if offset paging is used and the `tooManyToCount` flag is not set.  | validation: format int32
     - name: cursors | type: Cursors | description: Cursors to navigate through the result pages using `next` and `prev`. Returned if cursor paging is used.  
        - name: next | type: string | description: Cursor string pointing to the next page in the list of results.  | validation: maxLength 16000


```

### Examples

### Bulk delete all folders (with $w)
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.

```javascript
/*******************************************
 * Backend code - delete-folders.web.js/ts *
 ******************************************/

import { Permissions, webMethod } from '@wix/web-methods';
import { folders } from '@wix/media';
import { auth } from '@wix/essentials';

export const deleteFolders = webMethod(Permissions.Anyone, async (folderIds) => {
  try {
    const elevatedBulkDeleteFolders = auth.elevate(folders.bulkDeleteFolders);
    const deletedFolders = await elevatedBulkDeleteFolders(folderIds);

    console.log('Successfully moved folders to trash.');
    return deletedFolders;
  } catch (error) {
    console.error(error);
  }
});

export const listFolders = webMethod(Permissions.Anyone, async () => {
  try {
    const elevatedListFolders = auth.elevate(folders.listFolders)
    const foldersList = await elevatedListFolders();

    return foldersList;
  } catch (error) {
    console.error(error);
  }
});

export const searchFolders = webMethod(Permissions.Anyone, async (options) => {
  try {
    const elevatedSearchFolders = auth.elevate(folders.searchFolders)
    const foldersFound = await elevatedSearchFolders(options);

    return foldersFound;
  } catch (error) {
    console.error(error);
  }
});

/*************
 * Page code *
 ************/

import { deleteFolders, listFolders, searchFolders } from 'backend/delete-folders.web';

$w.onReady(async () => {
  await populateFoldersDropdown();

  $w('#bulkDelete').onClick(async () => {
    const searchOptions = { rootFolder: $w('#foldersDropdown').value};

    const foldersToDelete = await searchFolders(searchOptions);
    const foldersIdsToDelete = foldersToDelete.folders.map((folder) => {
      return folder._id;
    });
    await deleteFolders(foldersIdsToDelete);

    console.log(`Successfully deleted all folders found in ${$w('#foldersDropdown').label}.`)
    $w('#bulkDeleteSuccessMsg').show();
  });
});

async function populateFoldersDropdown() {
  const folderList = await listFolders();
  const dropdownOptions = folderList.folders.map((folder) => {
    return {
      label: folder.displayName,
      value: folder._id
    };
  });

  $w('#foldersDropdown').options = dropdownOptions;
};

```

### Search for 2 folders by key-word, returned alphabetically (with elevated permissions)
When no `rootFolder` is specified in the options parameter, the search returns results from the 'MEDIA_ROOT' folder.

```javascript
import { folders } from '@wix/media';
import { auth } from '@wix/essentials';

/* Sample options value:
 * {
 *   search: 'mountains',
 *   sort: {
 *     fieldName: 'updatedDate',
 *     order: 'ASC'
 *   },
 *   paging: {
 *     limit: 2
 *   }
 * }
 */

export async function mySearchFoldersFunction(options) {
  try {
    const elevatedSearchFolders = auth.elevate(folders.searchFolders);
    const foldersFound = await elevatedSearchFolders(options);

    console.log("Folders found in search:", foldersFound);
    return foldersFound;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
}

/* Promise resolves to:
 * {
 *   "folders": [
 *     {
 *       "_createdDate": "2023-08-21T09:32:34.000Z",
 *       "_id": "7984b3c5454e4371acbd4f4eedde96bc",
 *       "_updatedDate": "2023-08-21T11:10:51.000Z",
 *       "displayName": "mountains",
 *       "parentFolderId": "103601562ec94214bee61f470b403dd5",
 *       "state": "OK"
 *     },
 *     {
 *       "_createdDate": "2023-08-24T11:26:26.000Z",
 *       "_id": "742d4a0ee5884b119292d52ed964f14d",
 *       "_updatedDate": "2023-08-24T11:26:26.000Z",
 *       "displayName": "mountain_videos",
 *       "parentFolderId": "302fc049d70c41dea33fa4a27ab481ba",
 *       "state": "OK"
 *     }
 *   ],
 *   "nextCursor": {
 *     "cursors": {
 *       "next": "eyJ0b3RhbCI6IDEsICJvZmZzZXQiOiAyfQ=="
 *     },
 *     "hasNext": true
 *   }
 * }
 */
```

### 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.

```javascript
import { folders } from '@wix/media';

/* Sample options value:
 * {
 *   search: 'mountains',
 *   sort: {
 *     fieldName: 'updatedDate',
 *     order: 'ASC'
 *   },
 *   paging: {
 *     limit: 2
 *   }
 * }
 */

export async function mySearchFoldersFunction(options) {
  try {
    const foldersFound = await folders.searchFolders(options);

    console.log("Folders found in search:", foldersFound);
    return foldersFound;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
}

/* Promise resolves to:
 * {
 *   "folders": [
 *     {
 *       "_createdDate": "2023-08-21T09:32:34.000Z",
 *       "_id": "7984b3c5454e4371acbd4f4eedde96bc",
 *       "_updatedDate": "2023-08-21T11:10:51.000Z",
 *       "displayName": "mountains",
 *       "parentFolderId": "103601562ec94214bee61f470b403dd5",
 *       "state": "OK"
 *     },
 *     {
 *       "_createdDate": "2023-08-24T11:26:26.000Z",
 *       "_id": "742d4a0ee5884b119292d52ed964f14d",
 *       "_updatedDate": "2023-08-24T11:26:26.000Z",
 *       "displayName": "mountain_videos",
 *       "parentFolderId": "302fc049d70c41dea33fa4a27ab481ba",
 *       "state": "OK"
 *     }
 *   ],
 *   "nextCursor": {
 *     "cursors": {
 *       "next": "eyJ0b3RhbCI6IDEsICJvZmZzZXQiOiAyfQ=="
 *     },
 *     "hasNext": true
 *   }
 * }
 */
```

### Search folders (with elevated permissions)
```javascript
import { folders } from '@wix/media';
import { auth } from '@wix/essentials';

const elevatedSearchFolders = auth.elevate(folders.searchFolders);

async function mySearchFoldersFunction() {
  try {
    const foldersFound = await elevatedSearchFolders();

    console.log("Folders found in search:", foldersFound);
    return foldersFound;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
}

/* Promise resolves to:
 * {
 *   "folders": [
 *     {
 *       "_createdDate": "2023-08-14T07:41:19.000Z",
 *       "_id": "103601562ec94214bee61f470b403dd5",
 *       "_updatedDate": "2023-08-21T11:09:08.000Z",
 *       "displayName": "Pictures",
 *       "parentFolderId": "media-root",
 *       "state": "OK"
 *     },
 *     {
 *       "_createdDate": "2023-08-21T05:34:03.000Z",
 *       "_id": "302fc049d70c41dea33fa4a27ab481ba",
 *       "_updatedDate": "2023-08-21T05:34:03.000Z",
 *       "displayName": "Videos",
 *       "parentFolderId": "media-root",
 *       "state": "OK"
 *     }
 *   ],
 *   "nextCursor": {
 *     "cursors": {
 *       "next": ""
 *     },
 *     "hasNext": false
 *   }
 * }
 */
```

### Search folders
```javascript
import { folders } from '@wix/media';

async function mySearchFoldersFunction() {
  try {
    const foldersFound = await folders.searchFolders();

    console.log("Folders found in search:", foldersFound);
    return foldersFound;
  } catch (error) {
    console.error(error);
    // Handle the error
  }
}

/* Promise resolves to:
 * {
 *   "folders": [
 *     {
 *       "_createdDate": "2023-08-14T07:41:19.000Z",
 *       "_id": "103601562ec94214bee61f470b403dd5",
 *       "_updatedDate": "2023-08-21T11:09:08.000Z",
 *       "displayName": "Pictures",
 *       "parentFolderId": "media-root",
 *       "state": "OK"
 *     },
 *     {
 *       "_createdDate": "2023-08-21T05:34:03.000Z",
 *       "_id": "302fc049d70c41dea33fa4a27ab481ba",
 *       "_updatedDate": "2023-08-21T05:34:03.000Z",
 *       "displayName": "Videos",
 *       "parentFolderId": "media-root",
 *       "state": "OK"
 *     }
 *   ],
 *   "nextCursor": {
 *     "cursors": {
 *       "next": ""
 *     },
 *     "hasNext": false
 *   }
 * }
 */
```

### searchFolders (self-managed)
Self-managed SDK calls require you to [create a client](https://dev.wix.com/docs/sdk/articles/work-with-the-sdk/about-the-wix-client.md).

```javascript
import { createClient } from '@wix/sdk';
import { folders } from '@wix/media';
// Import the auth strategy for the relevant access type
// Import the relevant host module if needed

const myWixClient = createClient ({
  modules: { folders },
  // Include the auth strategy and host as relevant
});


async function searchFolders(options) {
  const response = await myWixClient.folders.searchFolders(options);
};
```

---