Table of Contents

you are here (click to zoom) - discover the stack

Dynamic Entity Properties

Whenever you create a content-type - like Person - and want to work with the data in your C# Razor templates, you'll be working with a Dynamic Entity. You can get any value using one of the following:

  • the dot-notation like Content.FirstName
  • the Get() method like Content.Get("FirstName")
  • the Get() method with a fallback value like Content.Get("FirstName", fallback: "John")
  • the typed Get<T>() method like Content.Get<string>("FirstName")

👉 also read about AsDynamic(...)

Code example using a Dynamic Entity

We'll assume we have a Content Type called Book with the following properties:

  • Title (text, 1-line)
  • Teaser (text, multi-line)
  • Description (text, html)
  • ReleaseDate (date)
  • Author (entity - another content item)

Here's a code example in a C# Razor template:

<!--
  The default variable for the current item is Content, 
  we'll just use another name for this sample
  note that .Title is automatically provided, 
  because the content-type has the property title. 
-->
<h1>@Content.Title</h1>
<div>@Content.Description</div>
<div>Author: @Content.Author.FullName</div>

So basically all properties of this book can be shown using [Object].[PropertyName] - for example Content.ReleaseDate.

What Dynamic Entity really does - and how

Technically the dynamic entity object is like a read-helper for the more complex IEntity. So actually the dynamic entity object will keep a reference to the underlying read-only IEntity item on a property Entity, and whenever your code accesses a property, the dynamic entity will query it from the underlying Entity.

The main things that the dynamic entity does for you, are

  1. Give you a nice, short syntax to access a property - so Content.FirstName instead of Object.Attributes["FirstName"]["en"] which would be necessary using the more advanced IEntity object
  2. Ensure that the language used in retrieving a value is the current user language
  3. Give conveniant access to related information like the Presentation object
  4. Automatically handle some data-not-found errors
  5. Automatically do conversions, like convert related entities (like .Children) into dynamic objects to make your code more consistant

How the Property Lookup Works

Internally there are a few things that can returned if you do something like Content.SomeProperty

  1. If the SomeProperty is one of the internal properties like EntityId etc. (see below) this will be returned
  2. Next is a simple property of the underlying Entity,
    1. like FirstName which would be a string
    2. or a relationship property like Tags which will return a special DynamicEntity that behaves as a list (see below)
  3. Last but not least - if nothing matches, it's null

Properties of a Dynamic Entity

Read the API docs in the IDynamicEntity.

Additional properties that work (they are dynamic, so don't appear in the code)

  1. EntityId int
  2. EntityGuid Guid
  3. EntityType string - the type name like Person
  4. IsPublished bool - true/false if this item is currently published
  5. AnyProperty dynamic, but actually bool | string | decimal | datetime | List any normal property of the content-item can be accessd directly. It's correctly .net typed (string, etc.)
Tip

In 2sxc 10.27 any property that returns a List<DynamicEntity> now returns a IDynamicEntity containing a list. This means that if you expect the list to just return one item, you can directly access its properties like this:
Content.Author.FirstName.
To otherwise enumerate the items, we recommend AsList(object) so AsList(Content.Tags)

Discover More in the Razor Tutorials

We have an rich series of Razor tutorials. You should really check them out 👍.


Appendix

The following properties/methods exist, but shouldn't be used. They are documented here so that you know that they are not meant for public use:

  1. Created - the created date
  2. Modified - the modified date
  3. Owner - the current owner of the item, usually the author
  4. Metadadata - currently use AsEntity(theObject).Metadata
  5. Permissions - permissions of the current item (if any are defined)

History

  1. Introduced in 2sxc 01.00
  2. Changed to use interface IDynamicEntity in 6.x
  3. Draft/Published introduced in 2sxc 7.x
  4. Presentation introduced in 2sxc 7.x
  5. Modified introduced in 2sxc 8.x
  6. Implemented .net equality comparer in 2sxc 9.42
  7. Parents added in 2sxc 9.42
  8. Get added in 2sxc 9.42 and added to interface IDynamicEntity in 10.07
  9. Parents introduced in 2sxc 9.42, and added to interface IDynamicEntity in 10.07
  10. IsDemoItem property added in 2sxc 10.06
  11. Changed dynamic access to a property to return a DynamicEntity which is enumerable in 10.27