Skip to main content
Home  › ... Razor

Use DataSources Tutorial

Tutorial HomeDataSources
#1 Data Sources Tutorial - use in C# / Razor Code

Data Sources Tutorial - use in C# / Razor Code

DataSources are the magic backbone of 2sxc. They can get data, filter it, sort it, and more. These samples show you how to use them in code (instead of in VisualQuery).

  • You can use the IDataService with Kit.Data in your C# code to get DataSource Objects. A common scenario is when you need to configure the parameters in ways that are not possible in the UI.
  • Such DataSources can also be linked together (attached) so the output of one DataSource becomes the input of another.
  • You can also provide options to give each DataSource it's configuration.

Data Sources Tutorial - Basic Use

To get started, let's look at some samples to just use a DataSource in code.

Use Kit.Data.GetSource<Csv> to create a DataSource reading CSV data. This sample shows how to:

  1. Use Kit.Data.GetSource<DataSourceName< to get the desired DataSource
  2. Get a Csv DataSource from ToSic.Eav.DataSources
  3. When creating the source, also provide settings for FilePath and Delimiter
  4. Loop through the items

⬇️ Result | Source ➡️

List of Data in the CSV DataSource (1)

  • ()
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade
@using System.Linq
@using ToSic.Eav.DataSources

@{
  // Create the data source and provide configuration in the options
  var products = Kit.Data.GetSource<Csv>(parameters: new {
    FilePath = App.RelativePath + "/asstes/data/products.csv.txt",
    Delimiter = ";",
  });
}
<h3>List of Data in the CSV DataSource (@products.List.Count())</h3>
<ul>
  @foreach (var product in AsList(products)) {
    <li>
      @product.Name (@product.Description)
    </li>
  }
</ul>

Source Code of products.csv.txt

Id;Name;Description;License;Created;Link
1;2sxc;A neat CMS extension for DNN;MIT;01.01.2012;https://2sxc.org/
2;Koi;System to coordinate the CSS-Framework between theme and modules. ;MIT;01.06.2018;https://connect-koi.net/
3;Razor Blade;Helpers for common Razor task;MIT;01.02.2019;https://github.com/DNN-Connect/razor-blade
4;Image Resizer;Automatic, amazing image Resizer;MIT;06.05.2013;https://2sxc.org/learn-extensions/ImageResizer

Use Kit.Data.GetAppSource and Kit.Data.GetSource<T> to create data sources. This sample shows how to:

  1. First get all the data from the current App using GetAppSource()
  2. Then get a EntityTypeFilter from ToSic.Eav.DataSources
  3. When creating the filter, also attach the initial app DataSource...
  4. ...and give the filter the parameter TypeName = "Books"
  5. Count the data in both DataSources
  6. Loop through the final items in the filter DataSource

⬇️ Result | Source ➡️

Statistics

  • App Item Count (unfiltered): 1620
  • Books Item Count (filtered): 4

List of Persons from the DataSource

  • Hitchhikers Guide to the Galaxy
  • Good Omens
  • Phishing for Phools
  • The Last Continent
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade
@using System.Linq
@using ToSic.Eav.DataSources

@{
  // First get the root data source - the App with all data
  // Since we're in a Razor file it picks the current App based on the context
  var appDs = Kit.Data.GetAppSource();

  // Now create a Type-Filter and tell it to only keep Books
  // * attach: Attach the App containing everything
  // * parameters: Set the TypeName of the filter to only keep Books
  var books = Kit.Data.GetSource<EntityTypeFilter>(attach: appDs, parameters: new { TypeName = "Books" });
}
<h3>Statistics</h3>
<ul>
  <li>App Item Count (unfiltered): @appDs.List.Count()</li>
  <li>Books Item Count (filtered): @books.List.Count()</li>
</ul>

<h3>List of Persons from the DataSource</h3>
<ul>
  @foreach (var book in AsList(books)) {
    <li>
      @book.Title
    </li>
  }
</ul>

Often you may want to attach multiple DataSources. This sample shows how to:

  1. First get all the data from the current App using GetAppSource()
  2. Then get a EntityTypeFilter from ToSic.Eav.DataSources
  3. When creating the filter, also attach the initial app DataSource...
  4. ...and give the filter the parameter TypeName = "Books"
  5. Count the data in both DataSources
  6. Loop through the final items in the filter DataSource

Note that you can do much more, best check out the docs.

⬇️ Result | Source ➡️

Statistics

  • App Item Count (unfiltered): 1620
  • Books Item Count (filtered): 4
  • Authors Item Count (filtered): 6

List of Persons from the DataSource

  • Hitchhikers Guide to the Galaxy (Books)
  • Good Omens (Books)
  • Phishing for Phools (Books)
  • The Last Continent (Books)
  • Douglas (Persons)
  • Terry (Persons)
  • Neil (Persons)
  • George (Persons)
  • Raphael (Persons)
  • Ed (Persons)
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade
@using System.Linq
@using ToSic.Eav.DataSources

@{
  // First get the root data source - the App with all data
  var appDs2 = Kit.Data.GetAppSource();

  // Now create a Type-Filter and tell it to only keep Books / Authors
  var books2 = Kit.Data.GetSource<EntityTypeFilter>(attach: appDs2, parameters: new { TypeName = "Books" });
  var authors2 = Kit.Data.GetSource<EntityTypeFilter>(attach: appDs2, parameters: new { TypeName = "Persons" });

  // The following lines are just spread for easier documentation
  // Usually you would write in inline
  var dataSourceLinks2 = books2.Link    // Get the link to the books
    .Add(authors2.Link              // Add the author2 link
      .Rename(inName: "Stream2"     // but rename the link so it's attached as "Stream2"
    )
  );
  var merged2 = Kit.Data.GetSource<StreamMerge>(attach: dataSourceLinks2);
}
<h3>Statistics</h3>
<ul>
  <li>App Item Count (unfiltered): @appDs2.List.Count()</li>
  <li>Books Item Count (filtered): @books2.List.Count()</li>
  <li>Authors Item Count (filtered): @authors2.List.Count()</li>
</ul>

<h3>List of Persons from the DataSource</h3>
<ul>
  @foreach (var item in AsList(merged2)) {
    <li>
      @item.EntityTitle (@item.EntityType)
    </li>
  }
</ul>

Data Sources Tutorial - Use GetQuery

Kit.Data.GetQuery() should be used to retrieve a query. It also allows us to pass in parameters if the query supports it. The following demos all use this query. TODO: PIC of query

Use Kit.Data.GetQuery("QueryName") to get a Query DataSource. This first sample does not use parameters so the sorting happens on whatever was set as default.

⬇️ Result | Source ➡️

List of Persons from the DataSource

  • Douglas Adams
  • George Akerlof
  • Neil Gaiman
  • Ed Hardy
  • Raphael Müller (not an author)
  • Terry Pratchett
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade
@using System.Linq
@using ToSic.Eav.DataSources

@{
  var queryNoParams = Kit.Data.GetQuery("GetQueryDemoAuthorsSorted");
}
<h3>List of Persons from the DataSource</h3>
<ul>
  @foreach (var person in AsList(queryNoParams)) {
    <li>
      @person.FirstName @person.LastName
    </li>
  }
</ul>

@* TODO:: @2dm Query Img *@

View Configuration

This is how this view would be configured for this sample.

  • Query: GetQueryDemoAuthorsSortedDnn
Details for Authors Sorted

Get Persons form the DataSource and Sorted

This will also provide parameters to change how it is sorted. Note that these parameters only work, because the Query expects these.

⬇️ Result | Source ➡️

List of Persons from the DataSource

  • Terry Pratchett
  • Raphael Müller (not an author)
  • Neil Gaiman
  • George Akerlof
  • Ed Hardy
  • Douglas Adams
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade
@using System.Linq
@using ToSic.Eav.DataSources

@{
  var queryFirstNameDesc = Kit.Data.GetQuery("GetQueryDemoAuthorsSorted", parameters: new {
    Sort = "Desc",
    Field = "FirstName"
  });
}
<h3>List of Persons from the DataSource</h3>
<ul>
  @foreach (var person in AsList(queryFirstNameDesc)) {
    <li>
      @person.FirstName @person.LastName
    </li>
  }
</ul>

@* TODO:: @2dm Query Img *@

View Configuration

This is how this view would be configured for this sample.

  • Query: GetQueryDemoAuthorsSortedDnn
Details for Authors Sorted

Get Persons form the DataSource and Sorted

 

 

#1 Data Sources Tutorial - use in C# / Razor Code