Skip to main content
Logo

WebAPI dynamic properties


In most website deployments, each page within your Contensis powered website will contain Metadata or specific Structured Content data. The Data property on each ContentNode gives you access to retrieve the information stored in the database.

The Data property returns a DynamicObject data type which was introduced in Microsoft .NET 4.0. The DynamicObject was chosen because it provides a simple method to return database data in a way that makes in very convenient to use within a page.

To preserve performance, no data is returned from the database until a single call is made to the Data object on a ContentNode. Similarly, once a single call has been made to the Data object on a given ContentNode, all the database data for that object is returned. So, if we were to access the ContentNode object for a News Article page on our website which has 15 Metadata fields and 11 Structured Content data fields associated with it, then the Data property will return a DynamicObject with 26 fields on it (e.g. 15 metadata fields + 11 structured content fields = 26).

This can be quite a new concept to understand, so lets demonstrate this in action by using some examples.

Usage examples - convenience

In this example, we are simply outputting the the Description (or MD_Description) in a P tag on a given page.

@using Contensis.Framework.Web
 
<p>@CurrentNode.Data.description</p>

You'll notice that in the above example, the property accessor on the Data object is referred to in lower case (e.g. "description").  This was intentional and has been included to show that conveniently we have chosen to implement our DynamicObject to be case insensitive.  This means that the following examples will all achieve the same end result.

@using Contensis.Framework.Web
 
<p>@CurrentNode.Data.description</p>
 
<p>@CurrentNode.Data.DESCRIPTION</p>
 
<p>@CurrentNode.Data.deSCRIPTion</p>

We have also chosen to implement shortened property accessors on the Data object as well.  This means, that instead of you having to specify "MD_Description" as per your database schema, or likewise "PROPERTY_Title".  Of course, you can access them using the exact same name as the fields in your database, but we thought we'd simplify this and just allow for the short-hand properties.  So the following example will all produce the same results.

@using Contensis.Framework.Web
 
<p>@CurrentNode.Data.title</p>
 
<p>@CurrentNode.Data.PROPERTY_Title</p>

Usage examples - native data types

Another benefit of using DynamicObject is that we can return the ContentNode's data in the same types as they are in the database. As an example, rather than returning a collection of System.Object and forcing designers and developers to Cast between types and check for Type before performing an operation, we can simply expose the data using the same Types as they are stored in the database. 

So, you know that if you have a PROPERTY_DateCreated field in the database, and its Database Type is type DateTime, then by accessing ContentNode.Data.DateCreated will return you a System.DateTime type. Again, the simplicity of this method is best shown in an example.  The following example shows to to format the Date Created of a given webpage.  In fact, in this particular example, we will use a ContentNode of an Event Record - just so we can demonstrate that accessing Structured Content data is exactly the same as accessing Metadata.

@using Contensis.Framework.Web
 
<dl>
    <dt>Start date:</dt>
    <dd>@CurrentNode.Data.startDate.ToString("dddd dd MM yyyy h:mm")</dd>
    <dt>End date:</dt>
    <dd>@CurrentNode.Data.Enddate.ToString("dddd dd MM yyyy h:mm")</dd>
</dl>

This examples shows that DateTimes are returned to you as System.DateTime types allowing you to use familiar ToString() methods on them for custom formatting. Of course, you could also perform specific DateTime calculations on the outputs also - no casting required.

Taxonomies

Special consideration has also been given to Data that is bound to a Contensis Taxonomy.  Another fundamental goal of the Contensis WebAPI was to simplify the usage of taxonomy based information so that it can be consumed and displayed in web pages with minimal fuss.

In fact, it is so easy to do Taxonomies in Razor views, that for simple Taxonomy fields (e.g. those taxonomy fields that only store Single Taxonomy data a.k.a Single Selects), the process of outputting the actual Localised text for that Taxonomy is exactly the same as if it were a String field.  Taxonomy fields defined as Multi-select are also very simple to output - by default you get a comma-separated list of values.

@using Contensis.Framework.Web
 
<dl>
    <dt>Categories:</dt>
    <dd>@CurrentNode.Data.categories</dd> 
</dl>

More advanced examples of using the TaxonomyField class to perform additional lookups of data, or by outputting a list of hyperlinks for a Multi-select Taxonomy field will follow over the next few weeks.