The previous example just returned one item/entity.
But of course you can also return a list of items.
This is the more common case.
Our sample basically will just return data it generated, but your code will usually get data from elsewhere and provide it as a list.
Basic List
Return a list of items numbered 1...5 with random Guid identifier.
List of Data in the DataSource (5)
Hello from ListBasic (1 / 7ec2d1ac-1648-4859-b403-f1c25633d29d) - Fav Number: 2742
Hello from ListBasic (2 / 5bb327b4-3fa1-4def-a9ac-1b42a710f3bb) - Fav Number: 2742
Hello from ListBasic (3 / 6f2895b0-30ea-408e-b901-cb8cb9f8902e) - Fav Number: 2742
Hello from ListBasic (4 / aea58259-f550-4fce-8899-9ccf25d0ffd7) - Fav Number: 2742
Hello from ListBasic (5 / 2e579750-acb3-45e8-bbc5-7f996e374951) - Fav Number: 2742
Source Code of ../DataSources/ListBasic.cs
// This sample uses some LINQ
using System.Linq;
public class ListBasic : Custom.DataSource.DataSource16
{
public ListBasic(MyServices services) : base(services)
{
ProvideOut(() =>
// For demo, create a few of these items using numbers 1 to 5
Enumerable.Range(1, 5).Select(i => new {
// Property with name "Id" is automatically used for the EntityId
Id = i,
// Property with name "Guid" is automatically used for the EntityGuid
Guid = System.Guid.NewGuid(),
Title = "Hello from ListBasic",
FavoriteNumber = 2742,
})
);
}
}
@{
// Create the Dynamic DataSource with the name "Basic101"
var basic101 = Kit.Data.GetSource(name: "ListBasic");
}
<h3>List of Data in the DataSource (@basic101.List.Count())</h3>
<ul>
@foreach (var item in AsList(basic101)) {
<li>
<strong>@item.Title</strong> (@item.EntityId / @item.EntityGuid) - Fav Number: @item.FavoriteNumber
</li>
}
</ul>
Advanced List
Data in the DataSource (1)
Current Temperature: 13.1
WindSpeed: 18.6
WindDirection: 261
Source Code of ../DataSources/DataFromWebService.cs
using System.Net.Http;
using System.Linq;
using System.Text.Json.Serialization; // For JsonPropertyName
public class DataFromWebService : Custom.DataSource.DataSource16
{
public DataFromWebService(MyServices services) : base(services)
{
ProvideOut(() => {
var response = new HttpClient()
.GetAsync("https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t_weather=true")
.GetAwaiter()
.GetResult();
response.EnsureSuccessStatusCode();
var responseBody = response.Content.ReadAsStringAsync()
.GetAwaiter()
.GetResult();
var result = Kit.Convert.Json.To<WeatherData>(responseBody);
return new {
Temperature = result.Current.Temperature,
WindSpeed = result.Current.WindSpeed,
WindDirection = result.Current.WindDirection,
};
});
}
}
// Helper classes for JSON deserialization
// Note that we're not using most of the properties, but we have them for completeness of the tutorial
public class WeatherData
{
public double Latitude { get; set; }
public double Longitude { get; set; }
[JsonPropertyName("generationtime_ms")]
public double GenerationTimeMs { get; set; }
[JsonPropertyName("utc_offset_seconds")]
public int UtcOffsetSeconds { get; set; }
public string Timezone { get; set; }
[JsonPropertyName("timezone_abbreviation")]
public string TimezoneAbbreviation { get; set; }
public double Elevation { get; set; }
[JsonPropertyName("current_weather")]
public CurrentWeather Current { get; set; }
}
public class CurrentWeather
{
public double Temperature { get; set; }
public double WindSpeed { get; set; }
public double WindDirection { get; set; }
public int WeatherCode { get; set; }
public int Is_Day { get; set; }
public string Time { get; set; }
}
@{
// Create the Dynamic DataSource with the name "DataFromWebService"
var weather = Kit.Data.GetSource(name: "DataFromWebService");
}
<h3>Data in the DataSource (@weather.List.Count())</h3>
<ul>
@foreach (var item in AsList(weather)) {
<li>Current Temperature: @item.Temperature</li>
<li>WindSpeed: @item.WindSpeed</li>
<li>WindDirection: @item.WindDirection</li>
}
</ul>
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 - Lists</h2>
<div @Sys.PageParts.InfoWrapper()>
@Html.Partial("../shared/DefaultInfoSection.cshtml")
<div @Sys.PageParts.InfoIntro()>
<p>
The previous example just returned one item/entity.
But of course you can also return a list of items.
This is the more common case.
Our sample basically will just return data it generated, but your code will usually get data from elsewhere and provide it as a list.
</p>
</div>
</div>
Basic List Return a list of items... <!-- unimportant stuff, hidden -->
@{
// Create the Dynamic DataSource with the name "Basic101"
var basic101 = Kit.Data.GetSource(name: "ListBasic");
}
<h3>List of Data in the DataSource (@basic101.List.Count())</h3>
<ul>
@foreach (var item in AsList(basic101)) {
<li>
<strong>@item.Title</strong> (@item.EntityId / @item.EntityGuid) - Fav Number: @item.FavoriteNumber
</li>
}
</ul>
<h2>Advanced List</h2>
@{
// Create the Dynamic DataSource with the name "DataFromWebService"
var weather = Kit.Data.GetSource(name: "DataFromWebService");
}
<h3>Data in the DataSource (@weather.List.Count())</h3>
<ul>
@foreach (var item in AsList(weather)) {
<li>Current Temperature: @item.Temperature</li>
<li>WindSpeed: @item.WindSpeed</li>
<li>WindDirection: @item.WindDirection</li>
}
</ul>
@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })
An error has occurred. This application may no longer respond until reloaded.
Reload🗙