ASP.NET WebForms, DD4T and ViewModels
Make Tridion ASP.NET WebForms project more dynamic using DD4T components and building ViewModels from them.
Recently, the use of DD4T became one of the most advanced approaches to developing Tridion applications.
DD4T is short for Dynamic Delivery for Tridion. It is a thin layer of functionality on top of the Tridion APIs that exposes the Tridion object model to dynamic web applications. The goal of DD4T is to allow you to develop a Tridion-based web application directly, without having to code templates. ( more inf here).
One of the classes from which I think we can have the greatest benefit is the ComponentFactory. By using it, if we know the Id of a component, we can convert it to an object of type Component. With a Query and using appropriate criterias, we can get the IDs of the components that we need, convert them into objects with the ComponentFactory, and access their fields and visualize them in our code in the way we want to.
Usually DD4T is used in MVC projects, but why not use it in WebForms projects also? We will have a good use of objects that we can bind to our controllers, and to make our lives easier – we can even transform them into ViewModels. And this is done simply by including several dll’s to our WebForm project and installing DD4T TBBs in our Tridion Project.
In just a few steps:
First we must have WebForms project with all the dlls needed for work with Tridion.
After this we must install DD4T for Tridion from the Nuget.
To be able to render your components as objects DD4T needs some specific fields in the template. To provide them we must download DD4T-Template Installer(click) After this we must create a folder in the publication where we create our templates. Install our DD4T-Template Installer and in the field for the folder put your folder Id, and your Tridion URL in the other field(in my case http://localhost) .
When we finish we will be able to add the following in our template.
And also our field “Component Presentations based on this Component Template will be:” must be marked as “Published as a Dynamic Component”. Also, link the Template to the Schemas and then any Component using that Schema will be published together with this Dynamic template each time the Component is later published.
When we publish our page the component in the page must use this template.
If everything is ok, when we publish our Pages using this new DD4T Component Template and Page Template we will be able to have our components as objects and access their fields.
The best way to be sure that we will have only components with dynamic content is to make AndCriteria in our query that contains ItemSchemaCriteria and ItemTemplateCriteria
Now we can use the components in our code as we like.
We have only one item so the result will be:
It’s not bad but every time when we don’t have the required field we must check if the field exists, and accessing the fields is not very easy either. Especially if we have code such as: Images(“component.Fields[“Image”].LinkedComponentValues.Multimedia.Url”).
I think it will be a lot easier if we make view models and use them. We can make a class with properties for the fields that we have in our component, and to make good reusable code we can make lambda expression in our class to “transform” our fields into the class articles.
And now the code in our project:
And the result:
Author: Stoian Tankovski
We would like to help you in developing your SDL Tridion websites with our extensive knowledge of the Tridion Architecture, APIs and Tridion Upgrades. Robert Curlette leads our team in Sofia, Bulgaria and is a 5 time SDL Tridion MVP award winner.
We believe in the strong Tridion community and organized the first Tridion Developer Summit, teach courses on SDL Tridion, and regularly blog about SDL Tridion solutions.
Contact us to find out how we can help you today.