Custom Dynamic DataSources - Process In-Data
Requirements
DataSources be attached together. This means that you can create a DataSource which takes one or more DataSources as input.
Keep Only Odd IDs
This example has a DataSource which receives data from an upstream source. The upstream source is the list of all authors in this App. Our DataSource will then filter this list, and only keep the authors with an odd ID.
Data from the KeepOdd DataSource (4)
- Douglas (ID: 4001)
- Neil (ID: 4003)
- George (ID: 4007)
- Raphael (ID: 4011)
Error Handling if No In
If your DataSource requires attached data (In
) you may get hard-to-debug errors.
Because of this, the sample also has error handling for this.
The following code uses the KeepOdd
but forgets to attach the in.
Data from the KeepOdd DataSource (1)
-
Error: Stream 'Default' not found (ID: 0)
Message: This DataSource needs the stream 'Default' on the In to work, but it couldn't find it.
Important: Note that the DataSource also has code to handle errors if the In was not attached.
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 - Process In-Data</h2> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <p> DataSources be attached together. This means that you can create a DataSource which takes one or more DataSources as input. </p> </div> </div> Keep Only Odd IDs This example has a... <!-- unimportant stuff, hidden --> @{ // Get the stream of all authors var authors = App.Data["Persons"]; // Create the Dynamic DataSource "KeepOdd" and attach the authors to it var keepOdd = Kit.Data.GetSource(name: "KeepOdd", attach: authors); } <h3>Data from the KeepOdd DataSource (@keepOdd.List.Count())</h3> <ul> @foreach (var item in AsList(keepOdd)) { <li> <strong>@item.EntityTitle</strong> (ID: @item.EntityId) </li> } </ul> Error Handling if No In If your... <!-- unimportant stuff, hidden --> @{ // Create the Dynamic DataSource "KeepOdd" and forget to attach the In keepOdd = Kit.Data.GetSource(name: "KeepOdd"); } <h3>Data from the KeepOdd DataSource (@keepOdd.List.Count())</h3> <ul> @foreach (var item in AsList(keepOdd)) { <li> <strong>@item.EntityTitle</strong> (ID: @item.EntityId) <br> <strong>Message: </strong> @item.Message </li> } </ul> Important: Note that the DataSource also... <!-- unimportant stuff, hidden --> @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })