Custom Dynamic DataSources - Configuration
Requirements
Often you will need a DataSource that accepts some kind of parameters (configuration).
The code must have [Configuration]
attributes on the properties that should be configurable.
When calling GetSource(...)
use options: new { ... }
to set the values.
Custom DataSource with Configuration
This simple example will get a DataSource from a file, and pass some configuration options.
Specifically we'll give it the AmountOfItems
and FavoriteColor
.
List of Data in the WithConfig DataSource (3)
- Hello from WithConfig #1 - Favorite Color: dark blue
- Hello from WithConfig #2 - Favorite Color: dark blue
- Hello from WithConfig #3 - Favorite Color: dark blue
Source Code of this file
Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code
@inherits Custom.Hybrid.Razor14 @using ToSic.Razor.Blade; @using System.Linq; @using ToSic.Eav.DataSources; <!-- unimportant stuff, hidden --> <h2>Custom Dynamic DataSources - Configuration</h2> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <p> Often you will need a DataSource that accepts some kind of parameters (configuration). The code must have <code>[Configuration]</code> attributes on the properties that should be configurable. When calling <code>GetSource(...)</code> use <code>options: new { ... }</code> to set the values. </p> </div> </div> Custom DataSource with Configuration... <!-- unimportant stuff, hidden --> @{ // Create the Dynamic DataSource with the name "WithConfig" // and set some configuration options var withConfig = Kit.Data.GetSource(name: "WithConfig", parameters: new { AmountOfItems = 3, FavoriteColor = "dark blue" }); } <h3>List of Data in the WithConfig DataSource (@withConfig.List.Count())</h3> <ul> @foreach (var item in AsList(withConfig)) { <li> <strong>@item.Title</strong> - Favorite Color: @item.FavoriteColor </li> } </ul> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })