CreateSource<T>() Command
If you need to use a DataSource
object in your code use CreateSource<T>()
⚡ The official API docs.
Discover More in the Razor Tutorials
We have an rich series of Razor tutorials. You should really check them out 👍.
Simple Example
@inherits Custom.Hybrid.Razor12
@{
// A source which can filter by Content-Type (EntityType)
var allAuthors = CreateSource<ToSic.Eav.DataSources.EntityTypeFilter>();
allAuthors.TypeName = "Authors";
// access the data and automatically apply the filter/config
var authors = allAuthors["Default"];
}
Example Chaining DataSources
@inherits Custom.Hybrid.Razor12
@{
// A source which can filter by Content-Type (EntityType)
var allAuthors = CreateSource<ToSic.Eav.DataSources.EntityTypeFilter>();
allAuthors.TypeName = "Authors";
// Sort by Fullname descending
var sortedAuthorsDesc = CreateSource<ValueSort>(allAuthors);
sortedAuthorsDesc.Attributes = "FullName";
sortedAuthorsDesc.Directions = "desc";
// access the data and automatically apply the filter/config
var authors = sortedAuthorsDesc["Default"];
}
Read more about this in Data Sources (IDataSource)
Note
The type T
mentioned above must be an IDataSource. The built in ones are usually in these namespaces:
You can also create and compile your own DataSources, and then deploy the DLL to use in your code.
Tip
In most cases you will prefer to use VisualQuery to work with data, but sometimes this approach is neecssary.
We suggest you check out some apps - almost all use this.
Also Read
Discover More in the Razor Tutorials
We have an rich series of Razor tutorials. You should really check them out 👍.
History
- General Tokens introduced in 2sxc 8.0
- Added extra security switch in 2sxc 9.32