About CMS Collections in Blocks

Wix Blocks is open to all Wix Studio users. To access Blocks, go to Custom Apps in your Studio workspace.

Wix Blocks enables you to add CMS database collections to your app. Managing collections in Blocks is similar to managing collections in Wix sites. But, there are a few points that are very different - check them out in this article. 

Tip: Pay attention to collection permissions their security guidelines.

A Blocks collection is a placeholder

The most important thing to understand about a Blocks collection is that it is a placeholder for the data of any site it's installed on. This is because a Blocks collection can be used on multiple sites - even thousands of them. These sites can be very different from each other and have their own databases. Think about a collection that holds customer information. Every site can have a list of their own customers, and your widget can apply to all of them.

When you create a collection in Blocks, you define the fields of the collection, and make it possible to refer to it in the app's code. You can also add default data in Blocks, but you don’t have to. If you do add default data, it is automatically installed on the site together with the app, and can later be replaced by data from any site the app is installed on.

If you change the structure of a collection in Blocks in a future version of your app, it impacts any site it's installed on. So work with caution and don't make changes that can break sites.  

About the default data

Data that you add to your Blocks collections will be imported to the site in which your app is installed, providing your site creators with default data for your app. If you choose to add default data, note that:

  • The site your app is installed on can change that data later.
  • The default data will only be imported in the first installation. If you release a new version of your app and change the data in the collection, it will not override the data that already exists in any site your app is installed on.

To add a collection to your app

  1. Click the Database  icon in your app's left menu. 
  2. Click New Collection.
  3. Create a unique namespace for you app, if you haven't already done it. Make the namespace meaningful and clear (if you haven't named your app yet, you will be triggered to name it now). 
  4. Name your collection.
  5. Define the structure of your collection in the CMS
  6. Optional - add test items to your collection. Remember that they are only placeholders. The actual data won't be imported to a site. 

See your collection in the editor

When you import a Blocks app that has a collection to your site, the collection will appear in the Content Collections section in the Databases  panel. Your app namespace will appear next to this collection, to indicate that it's from Blocks. From now on you can handle your collection like in any other Wix site. 

Important If you delete the app from the site, the collection will be deleted as well.

Access the collection data through code

First, write this import statement in your widget code: 

Copy
1

To access your collection and perform various actions, query, insert, etc, use the following syntax with any wix-data API. For example, this is how you query a collection (learn more about your namespace

Copy
1

Learn more about working with the Wix Data API

Reference a collection on your site through the widget code

You might want to reference a collection on your site directly from your widget code without adding it to Blocks. There are two ways to do this:

  • If you know the name and structure of the collection, you can use it in your widget code. For example, if Wix Stores is installed on the site, you can use the Stores/Products collection.

  • You can pass the name of the collection or other information about the collection as properties in the Widget API.

Note: You can also use datasets to connect widget elements to a collection without using code.

Was this helpful?
Yes
No