Querying Data

We use all these methods in the CMS, so if you are not sure just clone the repo source and dig around, it should be fairly simple to understand.

Available Services

The following services are automatically injected in all components (defined in _Imports.razor):

  • IContentService - Content and ContentType queries

  • IMediaService - Media queries

  • IMembershipService - User and role queries

  • ILanguageService - Language and dictionary queries

  • ITagService - Tag queries

  • ISeoService - SEO and redirect queries

  • IDataService - Global data queries

  • IEmailService - Email operations

Getting Content Properties

In your IContentView components, the Content property is passed in automatically. You can access property values using these methods:

Simple Properties

Get a content property value by alias (property aliases are found on the Content Type page):

@(Content!.GetValue<string>("PropertyAlias"))
@(Content!.GetValue<int>("AmountPerPage"))
@(Content!.GetValue<bool>("ShowNavigation"))

The GetValue<TypeHere> accepts from types. Each content property has an example how how to use this method to get the data and the type to use. See the link below

Current Content Properties

To get media or content from a picker , it's a bit different, you need to pass in an instance of Mediatr. If you haven't defined it in the _imports like I have, make sure you Inject and instance of it.

The GetValue method accepts any type. Each content property editor has documentation showing the type to use.

Media Picker Properties

To get media from a media picker property:

Content Picker Properties

To get content from a content picker property:

Block List Editor Properties

To get content blocks from a Block List Editor:

Querying Content

Query Multiple Items

Use QueryContentAsync with QueryContentParameters to get multiple content items with filtering, sorting, and paging:

Query Parameters

QueryContentParameters supports these options:

Example with WhereClause

PaginatedList Result

All Query*Async methods return a PaginatedList<T> for server-side paging:

Usage example with pagination:

Get Single Item

Use Get*Async methods to retrieve a single item by ID:

Querying Other Data Types

The same patterns apply to all other services:

Media

Users

Content Types

Languages

Domains

Saving Data

Use Save*Async methods to create or update data. All save methods return a HandlerResult<T>:

HandlerResult

All save operations return a HandlerResult<T>:

Save Examples

Performance Tips

  1. Use caching: Set Cached = true for frequently accessed, rarely changing data

  2. AsNoTracking: Default is true for read operations (good for performance)

  3. Limit AmountPerPage: Don't request more items than you need

  4. Use Ids filter: When querying specific IDs, it automatically optimizes the query

  5. Leverage WhereClause: Filter at the database level for best performance

Last updated