Table of Contents

Prepare Data for Templates and WebApi

you are here (click to zoom) - discover the stack

A common Step in the data-flow is the Data Preparation before the View or Custom WebApi work with it. This is fully automated and has three common scenarios:

  1. No Prepare where no Data is used or at least no Instance Data
  2. Default where content added to a Module by an editor is retrieved and prepared
  3. Query where a custom VisualQuery is configured to get the data

Note: A special case where data is prepared is when it's used for indexing in the full-text search. This is documented in Search.

No Preparation

Two cases need no preparation at all

  1. Templates / code which don't use any data
  2. Templates / code which use only general App data or Queries but not instance data

In these cases the View is configured to not use data and the template or WebApi will either not use data or will only access it through the App.Data which has all data at it's disposal.

Default Preparation: Instance Data

By default all Module Instances can have data which the editor has added manually in the CMS UI. This corresponds to the normal Content Editing scenario. Internally it uses a Query which looks like this:

This does the following (starting from the bottom):

  1. The ICache is an IAppRoot DataSource which hass all data of this App
  2. The PublishingFilter will then ensure that editors can see draft-data, and public users can only see published data
  3. The ModuleDataSource is a CmsBlock which will select all relevant data of this Module instance
  4. It then provides the retrieved data on a Stream Default (all the items) and Header (previously ListContent)

The Template or WebApi running for this Module Instance will then have this data

  1. Content or often @Content has the first item on the Default stream
  2. Header or ofter @Header has the first and only item on the ListContent stream
  3. Data will contain all the streams as configured in the above query, usually you will use Data["Default"] to loop through lists of all the content items

Note that the ModuleDataSource also does some more magic like

  1. Retrieve the Demo-Item as configured in the View, if no data exists (or a public user is looking at the page, and the data is still draft)
  2. Add more information like IsDemoItem to each piece of Information

The Content App only has this kind of preparation. Customs Apps can also use this, but it can also be configured to use custom Queries (see below).

Query Preparation

If your View is configured to use a Query it will instead run this query.

Note

Note that your query can also extend the standard Query as mentioned above and provide both the normal edited content and add more streams as needed.

Tip

A common use case for Queries extending the default query is scenarios where the edited content provides parameters for the Query.

For example, the Header of the News App could contain a property Category which the editor can choose. The rest of the Query could then use this category to filter what items are retrieved.

Note about Accessing all App Data in your Code

Your template / WebAPI can also always access all the data in the App using App.Data.

You may Also want to Learn about

  1. Create queries using VisualQuery
  2. Prepare data in code
  3. Prepare data for the full-text search index of the platform
  4. Headless WebAPI
  5. DataSources

Read more