Category Archives: DD4T

Debugging DD4T with IIS Express

I’ve always thought the only way to debug a DD4T application is to run it on IIS. This is annoying, because the default option that Visual Studio offers is IIS Express or the local development server. So every time I set up a new DD4T project, I created a site in IIS, and changed the project properties accordingly. I must have wasted hours of my life doing this!

The reason why I went through all that trouble, is that it was the only way to run with Tridion’s 64-bit DLLs. Or SO I THOUGHT!

Turns out I have been wrong all along. You can run IIS Express in 64-bit mode by simply changing one registry setting.

  • Open regedit and navigate to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\WebProjects
  • Modify or create theREG_DWORD value named “Use64BitIISExpress”
  • Change the value to 1

Now you can run your DD4T applications on your IIS Express, which is the default for Visual Studio 2013. Saves a lot of hassle.

Thanks a lot to Robert Bernstein (http://blogs.msdn.com/b/rob/archive/2013/11/14/debugging-vs2013-websites-using-64-bit-iis-express.aspx).

 

ASP.NET MVC 5 support for DD4T

It has been around for a while, but since today the ASP.NET MVC 5 version of DD4T is available through NuGet. The name of the package is DD4T-MVC5.

All you need to do is create an MVC 5 application. This is easy with Visual Studio 2013, but is also possible with Visual Studio 2012.

Once you have created the web application, go to the package manager (Tools\Nuget Package Manager\Package Manager Console) and type:

Install-Package DD4T-MVC5

That’s it. Happy coding.

Improving link resolving in DD4T

When you use DD4T to create a web site, there are two ways to handle links. One is to write out a hyperlink. This is done by calling the LinkFactory and passing it the current component. For this purpose, all you need to know is the component’s URI.

The other option is to write out content from the linked component itself. In this case, it helps if you have the complete linked component at your disposal, URI, title, fields, metadata, etc.

It’s very neat to have the linked component inside your model, without having to retrieve it dynamically. However, there is a price to pay: the XML that you publish becomes much bigger because it incorporates the linked components as well.  This slows down publishing and may slow down your broker database.

Also, it does not always stop at one level of links. Sometimes, you may feel the need to follow a chain of links, for 2, 3 or more levels. To make this manageable, DD4T uses a mechanism called ‘LinkLevels’: by setting a parameter ‘LinkLevels’, you tell the DD4T templates how deep they should go when following links.

This is a rather rough principle though. In reality, there are some fields which you never want to follow (for example if you know up front that you will write them out as hyperlinks anyway). The LinkLevels mechanism however does not distinguish between fields, so you will get these linked components in the broker anyway!

This diagram shows how it works:

link levels - old

 

With link level set to two, a total of 7 components are published. Only 2 are needed completely, and for 3 we only need the URI. So most of the content is rendered to XML, published and stored in the broker for no reason at all.

Configuring links per field

To do anything about this, we need to have a way to configure link resolving behaviour on a per-field basis. At the recent MVP retreat I had the pleasure of talking to Jaime Santos Alcón. Jaime is a real GUI extension guru. He showed me how easy it is to add custom properties to a schema field in Tridion (at least, he made it look easy!).

With this in mind, and a lot of helpful code by Jaime to get met started (see http://jaimesantosalcon.blogspot.nl/2013/10/adding-extended-information-to-schema_28.html), I created a GUI extension of my own: the Trivident DD4T Editor. This editor adds one little checkbox to the schema definition screen:

dd4t editor

 

When the property ‘Follow link when rendering XML (dd4t)’ is checked, this information is stored in the ExtensionXml of the schema field. It is then picked up by the DD4T template code to determine whether or not it should follow a link.

 

Now, the diagram would look a bit different:

link levels

 

As you can see, there are a lot less components in the broker now, which will improve publisher performance and reduce the amount of data in the broker database.

Installing and configuring the new solution

To install, download trivident.dd4t.editor 1.0.1 and follow the instructions in the README.txt.

 

After the installation you should first enable one or more schema fields to ‘Follow link when rendering XML’. Note that you will only see the checkbox for component link and multimeda link fields!

Next, open your existing templates (component + page) in the template builder and set the parameter ‘Use field setting to determine whether or not to follow links’ to ‘yes’ (okay, the name is a bit too long, please suggest a better one in a comment!).

follow link levels per field

You’ll notice that in the example, the LinkLevels are set to 3. Note that the LinkLevels parameter is not obsolete in this new solution. Rather, it is treated as a ‘Max Link Level’. This is necessary because otherwise you may still acumulate too much XML by following links.

The configuration above means: “follow fields which are configured to be followed, but stop when you reach level 3”.

 

Download

Installation instructions for the GUI extension are in the README.txt inside the zip.

To download the latest DD4T templates, go to https://github.com/dd4t/DD4T.TridionTemplates/releases.

Thanks to Jaime Santos Alcón.

 

 

 

 

Inside DD4T: Resizing images on the fly

In a previous post, I explained how you can use the DD4T.Web library to serve binary files (like images, documents, etc) directly from the broker database. This was done with the BinaryDistributionModule.

There is an additional benefit to reap from this: the BinaryDistributionModule is able to resize your images on the fly. Here’s how.

Let’s say that you have a JPG image published to the broker database, with the URL  /images/ouroffice_tcm3-4954.jpg. If you use a browser to request this image, the binary data is extracted from the broker database by the module, and stored on the file system. You should see something like this:

ouroffice

 

Now when you request the URL /images/ouroffice_tcm3-4954_w150.jpg, this is what you get:

ouroffice_w150

 

You may be thinking “that’s easy, they simply uploaded a smaller version into Tridion”. But a look at the URL reveals that this cannot be the case: the URI (the ‘3-4954’ bit) is the same, which means that there is only one image in the CMS. Actually, all that’s different between those URLs is the string “_w150” at the end of the filename, right before the file extension.

Substitute for variants

The traditional approach to having thumbnail versions of images is by creating a variant in your template code. But that doesn’t make much sense with DD4T. An implementation using this framework hardly touches Tridion templates at all, and instead focuses completely on the web application. Hence it made sense to implement ‘thumbnailing’ on the web application side as well.

So how does this work in a Razor view? Let’s first look at the typical way to display an image:

<img src="@Model.Fields["image"].LinkedComponentValues[0].Multimedia.Url" />

The Multimedia.Url property contains the (local) url of the image you’re trying to show, e.g.  /images/ouroffice_tcm3-4954.jpg. Somehow we need to insert this ‘_w150’ string into this. Fortunately DD4T offers a helper method which comes to the rescue:

<img src="@Model.Fields["image"].LinkedComponentValues[0].Multimedia.Url.ResizeToWidth(150)" />

When this snippet is requested by the browser, it looks like this:

<img src=" /images/ouroffice_tcm3-4954_w150.jpg" />

Besides ResizeToWidth there is also a ResizeToHeight and even ResizeToWidthAndHeight, which takes two integers as parameters.

 

 

 

Inside DD4T: Handling Binary Files

A new feature in DD4T is the Web library (DD4T.Web.dll). It contains some functions that are useful in any .NET web site, whether they use MVC or not. It offers – for example – a way to serve binaries (images, PDFs, etc) straight out of the broker database.

Why would you want to do that, might you ask? Of course, Tridion has been used for ages to deliver binaries directly to the file system. Binaries are rarely dynamic in nature, so storing them as static files is actually a good idea!

Well, yes and no. Yes, storing binaries on the file system is great for performance, but it has a big downside: you cannot just plug in a new server into your web farm anymore, since it would not contain these binary files. Also, your developers who are so used to running the entire web app from within their IDE, would miss out on the images if they are only published to a central presentation environment.

If you work with DD4T, that does not mean you MUST serve the binary files from the broker database. It is okay  to serve them from the file system as well!

 

Serving binaries with the BinaryDistributionModule

The DD4T.Web library contains a BinaryDistributionModule. Purpose of this HttpModule is to make sure the requested binary is available on the file system, so IIS can serve it. Here’s how it works:

  • If the binary is not on the file system, it is retrieved from the broker database and stored as a file
  • If the binary is already on the file system, the timestamp of the file is compared against the last publish date of the binary in the broker database. If the file is stale, it is replaced. If the binary is no longer present in the broker, the file is removed (resulting in a 404, which is what you would expect if you attempt to view a file which has been unpublished).

To configure the BinaryDistributionModule, add the following XML code to the system.webServer node in your Web.config:

<modules runAllManagedModulesForAllRequests="true">
  <add name="BinaryModule" 
       type="DD4T.Web.Binaries.BinaryDistributionModule" />
 </modules>