Manually Load an Instance of a Content Type

Feb 10, 2014 at 5:54 PM
I would like to do load a specific instance of a Content Type in a template without having the user select it.

Inside the template I am looking to do something like:
    int entityId = 512;
    dynamic myColors = ToSic.Load2SexyContent(entityId);
Is this possible?
Feb 10, 2014 at 9:59 PM
Edited Feb 10, 2014 at 10:03 PM
Of course :)

BTW: if the entity is already in a list (say you have a razor template that can Show a list & a Details-item) then you can just use a simple LINQ-Query against the data in the list, something like...
var myColorSet = List.Where(e => e.Content.EntityId = entityId)); 
// result contains .Content and .Presentation
var myColor = myColorSet.Content
Now I'll assume you want something more sophisticated :)

But some Background first:
  1. Data travels from the DB to a Cache and stays there until needed.
  2. When needed, a Pipeline is assembled which connects the module to the Cache
  3. The Default Pipeline consists of the sequence Cache > ModuleDataSource >>>> Module (the last Connection having 4 channels for Content, Presentation, etc.)
In case this architecture feels like Overkill - there's a grand plan behind all this :). Most of it is documented on the old site, please have fun with the data sources concept.

Simply put: this grand architecture will allow us to create awesome functional apps without installing code on the server. Think about Admins with no Special permissions being able to install a News-App without any dangerous code being added to the System :). that's the Vision.

So where does your wish come in? The correct way to do what you want is to
  1. Create the correct Data-Source of the type ToSic.Eav.DataSources.EntityIdFilter (see this doc)
  2. hook it up to the Cache (happens almost automatically)
  3. and (nasty) give it the Parameter "entityID" (usually from the QueryString)
  4. or (better) tell it to get the paramater from the QueryString using the Property-Provider
    Note that this chain of Actions will be click-and-click once the Pipeline designer works, till then you need code, sorry :(
The code (Raphael will give a more correct Version) is approx. like this:
   var Source = new ToSic.Eav.DataSources.EntityIdFilter(...something...);
   Source.Parameters["EntityID"] = "[QueryString:id]"; // use Property-Provider
   var myContent = Source.Out["Default"][0]; // get first item on the Default out-chanel
Feb 10, 2014 at 10:28 PM
Thanks, this all makes sense.

I cannot pass the ID via the querystring. Although somewhat less flexible, I want to hard code the ID into the template.

I am having a little trouble trying to figure out the code, specifically how to set the EntityID value (which is no surprise since you say it's a little ugly). I will be looking forward to seeing how Raphael suggests getting doing this.
Feb 19, 2014 at 9:24 AM

I created some nice demos for you :)
Accessing data through the pipeline
Feb 19, 2014 at 5:44 PM
Thanks a lot. I took a look and this should be just what I need.
Feb 19, 2014 at 7:02 PM
...and if you like it, feel free to contrib. your own pipelines/filters :)
Apr 29, 2014 at 10:30 AM
need to get this for a non 6 version

where do I get the source for this one as the apge shows only demo but I dont see links to the correct razor code
Apr 29, 2014 at 2:40 PM
@nokiko - sorry, don't understand your question. Could you rephrase that?
Apr 29, 2014 at 2:47 PM

this page :

Where is the code for this one, I have been playing with apps and the new 604 version ( which is great ) but I need to do this for the current 5.5 version

Do you have code how to get access data through the pipeline for the current ( 5.5 ) version?
Apr 29, 2014 at 3:52 PM
Edited Apr 29, 2014 at 3:53 PM
The main difference is

var allAuthors = CreateSource<EntityTypeFilter>();
allAuthors.TypeName = "Author";

// access the list
@foreach (var dict in allAuthors.List) {
var allAuthors = CreateSource("ToSic.Eav.DataSources.EntityTypeFilter");
allAuthors.Configuration["TypeName"] = "Author";

// access the list
@foreach (var dict in allAuthors.Out["Default"].List) {
Also note that the newer filters like Sort or ValueFilter didn't exist in 5.x; and that Configuration Injection (where you say Typename = "[QueryString:id]") is also new.