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

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

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.

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.

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

 

 

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