Built-In Data Object Differences in APIs
2sxc has changed a lot over time. So you'll find old code snippets and new ones, and it helps to see which one is being used. Sometimes you'll also want to convert old code to new code, and this page should help you with that.
Tip
This is about Data Objects which are automatically available on each Razor / C# file.
So Content (old), MyItem (new), Data (old), MyData (new) etc.
It's also about converting between IEntity
to the ITypedItem
and dynamic
objects.
Tip
🎓 Best check out the tutorial QuickRef which shows all this!
What are Built-In Data Objects?
These objects/commands get data belonging to this block/instance. So when the Razor code loads, these objects are ready and filled with the data of the current block.
Note
It's important to understand how data-items can belong to the current block. Basically any data an editor adds to the current block belongs to it. This is different from queried data which doesn't belong to anything.
Example: The images in a gallery-block belong to that gallery instance. But the list of all tags don't belong to the gallery instance.
Overview of Data Objects
Purpose | Dynamic | Typed | Strong Typed |
---|---|---|---|
Get-Current-Data | Content |
MyItem |
MyItem |
Get-Current-List | List |
MyItems |
MyItems |
Get-Current-Header | Header |
MyHeader |
MyHeader |
Get-Current-Data | Data |
MyData |
MyData |
App Object | App |
App |
App |
App Query | App.Query |
App.Query |
App.Query |
App Data | App.Data |
App.Data |
App.Data |
App Resources | App.Resources |
App.Resources |
App.Resources |
App Settings | App.Settings |
App.Settings |
App.Settings |
All Settings | Settings |
AllSettings |
AllSettings |
All Resources | Resources |
AllResources |
AllResources |
Parameters | DynamicModel |
MyModel |
MyModel / Model |
Basic Get-Current-Data
Dynamic | Typed | Comments / Differences |
---|---|---|
Content ( dynamic ) |
MyItem (ITypedItem) |
First/main item |
List ( IEnumerable<dynamic> ) |
MyItems (IEnumerable<ITypedItem>) |
List of all items |
Header ( dynamic ) |
MyHeader (ITypedItem) |
Header item |
Important
The pairs such as Content
/MyItem
will often contain the same data - but not always.
In the old dynamic APIs, Content
contained the item belonging to the block,
but only if the View was not using a Query.
If the view used a query, Content
would contain the first item returned by the query.
MyItem
is different - it always contains the first item belonging to the block.
The same difference applies to List
vs MyItems
.
Advanced Get-Current-Data
The MyData
(previously Data
) object contains all data which was prepared for the current block.
In very basic scenarios you don't need it, as it contain the same data as the MyItem
(previously Content
) object and others.
As soon as the View is configured to use a Query, it will instead contain the data returned by the query.
Dynamic | Typed | Comments / Differences |
---|---|---|
Data (IContextData) |
MyData (IContextData) |
DataSource of query data |
Data["Tags"] (IDataStream) |
MyData["Tags"] (IDataStream) |
Tags Stream of query data |
Data["Tags"] (IDataStream) |
MyData.GetStream("Tags") (IDataStream) |
Recommended way to get stream |
DataSources and Streams return IEntity
objects, and you will usually want to convert them to ITypedItem
(or dynamic
) objects.
This is done with these APIs:
Dynamic | Typed | Comments / Differences |
---|---|---|
AsDynamic(Data) |
AsItems(MyData) |
Get list of items in Default stream |
AsDynamic(Data["Tags"]) |
AsItems(MyData.GetStream("Tags")) |
Get list of items in Tags stream |
AsDynamic(Data["Tags"].List.First()) |
AsItem(MyData.GetStream("Tags")) |
Get a single item |