ZauberCMS
  • 🪄Welcome to ZauberCMS
  • Getting Started
    • 💡Quick Start
      • Databases
  • Creating A Website
    • 📹Website Build Video Tutorial
    • Content Types
      • Element Types
      • Compositions
    • Current Content Properties
      • Textbox & Textarea
      • Text Editor (Radzen)
      • Text Editor (TinyMCE)
      • Numeric
      • True / False
      • Rating
      • Checkbox, Dropdown & Radio Lists
      • Media Picker
      • Navigation
      • Material Icon Picker
      • Content Picker
      • Date Picker
      • Custom Component Picker
      • Api Key Picker
      • Colour Picker
      • Block List Editor
      • Editor Notes
      • Google Map Location Picker
      • Simple List Manager
      • Simple Dictionary Manager
      • SEO Property
      • Code Editor
      • Colour Theme Picker
    • Content
      • Publish & Unpublish
    • Querying Data
      • Extension Methods
    • Views
    • Controllers (Route Hi-Jacking)
    • Custom Components
    • Users & Roles
      • Restrict Access By Role
    • Logs
    • Audit
    • Global Settings
      • Using Global Settings
    • SEO Sitemaps
    • Hosting
  • Extending ZauberCMS
    • Overview
    • BlockListEditor
      • Content Block Preview
      • Content Block
    • Custom List Data
    • Custom Content Property
    • Custom Validation
    • Custom Admin Sections
      • Section Nav Group Menu
      • Trees
        • Tree Context Menus
      • Reusing Content Editors
    • Saving Custom Data
    • Using AppState Events
    • Before & After Save
    • Email & Storage Providers
    • Seed Data
    • SEO Checks
  • Identity
    • Overview
    • External Authentication Providers
    • Account Layout
  • Language
    • Overview
    • Adding Language Dictionaries
    • Setting The Language
    • Using Language Dictionaries
  • AppSettings
    • Detailed Errors
    • Media Settings
    • Enable Path Urls
Powered by GitBook
On this page
  • Getting Content Properties
  • Querying Command
  • Get Command
  • Save Command
  1. Creating A Website

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.

PreviousPublish & UnpublishNextExtension Methods

Last updated 7 months ago

In your Content Views you will often want to get data either from the current page or get content from the database

The Content property is a default parameter and passing into the page.

Getting Content Properties

In your IContentView's you can get data using the following methods (Property aliases are used, these can be found on the Content Type page under the name of the property you have added)

Get a content property

@(Content!.GetValue<string>("PropertyAlias"))

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

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.

await Content!.GetMedia("PropertyAlias", Mediator)
await Content!.GetContent("PropertyAlias", Mediator)

Querying Command

Mediatr is used to get all data in ZauberCMS. Examples of using these are below (Again you must have an injected instance of Mediatr)

var posts = await Mediator.Send(new QueryContentCommand
{
    AmountPerPage = 4,
    ContentTypeAlias = "BlogPage",
    OrderBy = GetContentsOrderBy.DateUpdatedDescending
});

Each Command has similar but sometimes different properties, here is the content command options

public class QueryContentCommand : IRequest<PaginatedList<Models.Content>>
{
    public string ContentTypeAlias { get; set; } = string.Empty;
    public Guid? ContentTypeId { get; set; }
    public bool AsNoTracking { get; set; } = true;
    public List<Guid> Ids { get; set; } = [];
    public int PageIndex { get; set; } = 1;
    public int AmountPerPage { get; set; } = 10;
    public string? SearchTerm { get; set; }
    public bool IncludeChildren { get; set; }
    public GetContentsOrderBy OrderBy { get; set; } = GetContentsOrderBy.DateUpdatedDescending;
    public Expression<Func<Models.Content, bool>>? WhereClause { get; set; }
    public IQueryable<Models.Content>? Query { get; set; }
}

Query*Command returns a paged list, which allows you to page server side, you just need to page in the PageIndex and AmountPerPage for the paging

public class PaginatedList<T>
{
    public int PageIndex { get; set; }
    public int TotalPages { get; set; }
    public int TotalItems { get; set; }
    public IEnumerable<T> Items { get; set; } = Enumerable.Empty<T>();

    public PaginatedList()
    {
    }

    public PaginatedList(IQueryable<T> items, int pageIndex, int pageSize)
    {
        var count = items.Count();
        PageIndex = pageIndex-1;
        TotalPages = (int) Math.Ceiling(count / (double) pageSize);
        var skip = PageIndex * pageSize;
        Items = skip > 0 ? items.Skip(skip).Take(pageSize).ToList() : items.Take(pageSize).ToList();
        TotalItems = count;
    }

    public bool HasPreviousPage => PageIndex > 1;

    public bool HasNextPage => PageIndex < TotalPages;
}

You can use the same pattern for getting almost any type of data. i.e.

await Mediator.Send(new QueryUsersCommand { Options added here })
await Mediator.Send(new QueryRolesCommand { Options added here })
await Mediator.Send(new QueryMediaCommand { Options added here })
await Mediator.Send(new QueryContentTypesCommand { Options added here })
await Mediator.Send(new QueryAuditsCommand { Options added here })
await Mediator.Send(new QueryLanguageCommand { Options added here })
await Mediator.Send(new QueryDomainCommand { Options added here })

Get Command

The Get*Command pattern returns a single item. For example the code below, returns a single content item by ID and includes all child content items

var page = await Mediator.Send(new GetContentCommand
{
    Id = content.Id, 
    IncludeChildren = true
});

Again, you use the same pattern for all other data

await Mediator.Send(new GetUserCommand { Options added here })
await Mediator.Send(new GetRoleCommand { Options added here })
await Mediator.Send(new GetMediaCommand { Options added here })
await Mediator.Send(new GetContentTypeCommand { Options added here })
await Mediator.Send(new GetDomainCommand { Options added here })
await Mediator.Send(new GetGlobalDataCommand { Options added here })
await Mediator.Send(new GetGlobalLanguageCommand { Options added here })

Save Command

If you want to programmatically save some content or media, we have a similar pattern for that too. They all follow the Save*Command i.e. SaveContentCommand, SaveMediaCommand etc...

var saveResult = await Mediator.Send(new SaveContentCommand
{
    Content = ContentToSave
});

All Save*Commands return a HandlerResult, which had a Success bool and also messages which contain error, information and warning messages

public class HandlerResult<T>
{
    public T? Entity { get; set; }
    public bool Success { get; set; }
    public List<ResultMessage> Messages { get; set; } = new();
    public bool RefreshSignIn { get; set; }
}
Current Content Properties