> 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`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Create a Collection

## Article: Create a Collection

## Article Link: https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/introduction/create-a-collection.md

## Article Content:

# Create a Collection

Create a [collection](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/introduction/about-collections.md) to store a kind of content on a Studio Two site, such as property listings or contact form submissions. This article shows how to create one with a script from the **Wix Data** view in the Code tab. The script sets the collection's ID, fields, and permissions.

You can also create a collection in the [CMS dashboard](https://support.wix.com/en/article/cms-formerly-content-manager-creating-a-collection), or in site code with [Create Data Collection](https://dev.wix.com/docs/api-reference/business-solutions/cms/collection-management/data-collections/create-data-collection?apiView=SDK.md).

## Step 1 | Open a Create Collection script

**Create Collection** opens a script that already contains a template collection definition.

To open a Create Collection script:

1. In the Studio Two editor, open the **Code** tab.
2. In the row of icons at the top of the sidebar, click the database icon to open the **Wix Data** view.

   ![The top of the Code tab's sidebar, with the Wix Data icon highlighted and the Wix Data view open](https://wixmp-833713b177cebf373f611808.wixmp.com/images/983b41f68c3cda4c4e10eb8deb25d301.png)

3. Click **Create Collection**, the plus icon at the top of the view. A script opens in a new tab.

   If the **Wix Data** view doesn't list the site's collections, connect it with **Configure Credentials** first. See [About the Code Tab](https://dev.wix.com/docs/studio-two/get-started/about-the-code-tab.md#work-with-collections).

The script isn't part of the site's code, and you choose when to run it.

## Step 2 | Define the collection

This step sets the collection's ID, fields, and permissions. The template script looks like this:

```javascript
collections.createDataCollection({
  _id: "<id>",
  displayName: "<displayName>",
  fields: [
    {
      key: "title",
      displayName: "Title",
      type: "TEXT",
    },
  ],
  permissions: {
    insert: "ADMIN",
    update: "ADMIN",
    remove: "ADMIN",
    read: "ADMIN",
  },
});
```

To define the collection:

1. Replace `<id>` with the collection ID. Site code uses this ID to refer to the collection.
2. Replace `<displayName>` with the name that the CMS dashboard shows.
3. In `fields`, add an object for each field. Set these properties:

   - `key`: The [field ID](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/introduction/about-collection-fields.md#field-ids) that code uses.
   - `displayName`: The name that the CMS dashboard shows.
   - `type`: The [field type](#field-types).

4. In `permissions`, set the [role](#permission-roles) that can add, update, remove, and read items. The template script sets every permission to `ADMIN`, so site visitors can't read the items. To show the items on the site, set `read` to `ANYONE`.

This example defines a `Listings` collection that anyone can read and only admins can change:

```javascript
collections.createDataCollection({
  _id: "Listings",
  displayName: "Listings",
  fields: [
    { key: "title", displayName: "Title", type: "TEXT" },
    { key: "price", displayName: "Price", type: "NUMBER" },
    { key: "bedrooms", displayName: "Bedrooms", type: "NUMBER" },
    { key: "status", displayName: "Status", type: "TEXT" },
  ],
  permissions: {
    insert: "ADMIN",
    update: "ADMIN",
    remove: "ADMIN",
    read: "ANYONE",
  },
});
```

The `collections` object is available in Wix Data scripts with no import. A script runs with the permissions of the credentials that the **Wix Data** view is connected with, such as a CLI login or an API key. In site code, creating a collection requires elevated permissions, so do it only in backend code. Import `collections` from `@wix/data`, and call `createDataCollection()` with [elevated permissions](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/data-api/about-the-data-api.md#permissions).

You don't need to add the [system fields](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/introduction/about-collection-fields.md#system-fields), such as `_id` and `_createdDate`. Every collection has them.

### Field types

These are the most common values for a field's `type`:

- `TEXT`: Plain text.
- `NUMBER`: A number.
- `BOOLEAN`: `true` or `false`.
- `DATE`: A date, in `YYYY-MM-DD` format.
- `DATETIME`: A date and time.
- `TIME`: A time of day.
- `URL`: A web address.
- `RICH_TEXT`: Text with formatting.
- `IMAGE`: An image.
- `ADDRESS`: A physical address.
- `REFERENCE`: A reference to an item in another collection.
- `MULTI_REFERENCE`: References to several items in another collection.
- `ARRAY`: A list of values.
- `OBJECT`: A JavaScript object.

A `REFERENCE` or `MULTI_REFERENCE` field also needs the ID of the collection it refers to. For example:

```javascript
{
  key: "agent",
  displayName: "Agent",
  type: "REFERENCE",
  typeMetadata: { reference: { referencedCollectionId: "Agents" } },
}
```

For every field type, see [Create Data Collection](https://dev.wix.com/docs/api-reference/business-solutions/cms/collection-management/data-collections/create-data-collection?apiView=SDK.md). For the format of each type's values in code, see [Data Types in Wix Data](https://dev.wix.com/docs/api-reference/business-solutions/cms/data-types-in-wix-data?apiView=SDK.md).

### Permission roles

Each permission takes one of these roles:

- `ANYONE`: Any site visitor.
- `SITE_MEMBER`: Any logged-in site member.
- `SITE_MEMBER_AUTHOR`: The logged-in site member who added the item.
- `ADMIN`: Site admins only.

Backend code can perform operations that a collection's permissions don't allow by [elevating permissions](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/data-api/about-the-data-api.md#permissions). For how roles work, and how they relate to the permission settings in the CMS dashboard, see the [Data Permissions API](https://dev.wix.com/docs/api-reference/business-solutions/cms/collection-management/data-permissions/introduction?apiView=SDK.md) and [About collection permissions](https://support.wix.com/en/article/cms-collection-permissions-overview).

## Step 3 | Run the script

To run the script, click the play icon, **Wix Data: Run Query**, at the top right of the script tab. The response opens in a `result.json` tab.

## Step 4 | Check the collection

To check the collection:

1. In the **Wix Data** view, click **Refresh Collections**. The new collection appears in the list.
2. Expand the collection to see its fields. Each field is listed as `<fieldId>: <TYPE> ('<Display name>')`.

Wix also creates [CMS Data Sources](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/data-sources-and-dynamic-pages/about-cms-data-sources.md) for the collection, so you can display its items on the site's pages.

## See also

- [Add a Field to a Collection](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/introduction/add-a-field-to-a-collection.md)
- [Find Collection and Field IDs](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/introduction/find-collection-and-field-ids.md)
- [Manage Items](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/data-api/manage-items.md)
- [About CMS Data Sources](https://dev.wix.com/docs/studio-two/code-your-site/work-with-data/data-sources-and-dynamic-pages/about-cms-data-sources.md)