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.

DownloadLibrary

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) .

DD4TTemplateInstaller

When we finish we will be able to add the following in our template.

 MakeDinamycComponentTemplate

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.

ComponentTemplate

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.

 TakeComponents

We have only one item so the result will be:

ResultWithComponents

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[0].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.

 ViewModel

And now the code in our project:

 TransofrToViewModels

Repeater

And the result:

ResultWithViewModels
Now we can work with objects and bind them to our controllers, and make our queries based on linq or lambda extensions, and make our work easier and more pleasant.

 

Author: Stoian Tankovski