Per Magne Skuseth
Mar 20, 2013
(6 votes)

Is your site Add-On friendly?

A couple of weeks ago, I got a question from a developer who was wondering what the RequiredClientResources web controls in the Alloy templates were used for. If you take a look Alloy’s MasterPage, you’ll find one inside the head tag, and one at the bottom. Notice how they are marked with different values for the RenderingArea property:

   1: <EPiServer:RequiredClientResources RenderingArea="Header"
   2:     ID="RequiredResourcesHeader" runat="server" />
   3: <EPiServer:RequiredClientResources RenderingArea="Footer"
   4:     ID="RequiredResourcesFooter" runat="server" />

These can be used to render client resources(javascript for example), from other places, such as user controls and page templates:

   1: ClientResources.RequireScript("Scripts/MyScript.js"); // Will render in the header area (default)
   2: ClientResources.RequireScript("Scripts/MyScript.js").AtFooter(); // Will render in the footer area

This is useful if you want to keep your client resource rendering clean and tidy. But there are other uses as well. Add-ons and modules are not directly linked to your templates, but some of them might require some client resources to be rendered on your pages. As an example, the google analytics add-on uses this to add a tracking script to your page.

If you are developing your own add-on and want to render client resources, you can try this code as an example:

   1: [ClientResourceRegister]
   2: public class MyModuleResourceRegister : PageClientResourceRegister
   3: {
   4:     protected override void RegisterResources(IRequiredClientResourceList requiredResources, 
   5:         HttpContextBase context, PageData pageData)
   6:     {
   7:         requiredResources.RequireScript(Paths.ToClientResource(this.GetType(), 
   8:             "Scripts/MyNeatScript.js")).AtFooter();
   9:     }
  10: }

Short walkthrough: Add a ClientResourceRegister attribute to you class, and inherit from the PageClientResourceRegister. By overriding the RegisterRosource method, we are able to add our own client resources. In this case – a JavaScript file called MyNeatScript. Inline JavaScript could also be added by using RequireScriptInline.We could also add other client resources, such as a css file, or html.

Note: you also have a PageData object available, which will give you the current page being rendered, which can be useful if you need to affect the rendering based on the current page.

Note 2: Since this is a (hypothetical ) add-on, I use Paths.ToClientResource to make sure that the path to the script points to the right place – add-on locations may differ.

So how do I make my site add-on friendly?
Implement the client resource rendering with both Header and Footer area and you should be fine. It should only take you a few minutes. This is also marked as a step for upgrading your site from 6 to 7 in the SDK so you should consider it kinda important.

For the curious ones out there, the client resources are registered just before the Render event in Web Forms (OnPreRenderComplete).

What if I’m using MVC?
If you’re using MVC, you can use the following (you’ll find the code for this in the MVC templates as well):

   1: @Html.RequiredClientResources(RenderingTags.Footer)

The EPiServer PageController has a RequireClientResources action filter to allow the register process to run for your views.

Mar 20, 2013


Marcus Granström
Marcus Granström Mar 21, 2013 11:23 AM

Yes this is something we at EPiServer consider to be best practice. Enables site owners to install addons without having developers help.

Mar 21, 2013 04:34 PM

Really interesting post.

Is there a list of full template best practices to be add-on friendly?

1) Including RequiredClientResources
2) Correctly referencing dependencies using Nuget - something the Alloy templates don't do!
3) ......................................

David Sandeberg
David Sandeberg Mar 21, 2013 09:49 PM

Good to know. Thanks for bloggning about it!

Mar 22, 2013 10:49 AM

@Mark, take a look at this SDK article:
It describes basic requirements and best practices for managing client resources when developing CMS templates.

Please login to comment.
Latest blogs
Breaking Language Barriers with Multilingual Text Translation

In today's globalized world, effective communication across languages is essential. The Epicweb.Optimizely.AIAssistant Addon makes language...

Luc Gosso (MVP) | Jun 6, 2023 | Syndicated blog

Optimizely Search & Navigation - Get autocomplete suggestions in right language

When you are using Optimizely Search & Navigation (Find) to help you generate autocomplete suggestions server side in a multi-language scenario it...

Allan Thraen | Jun 5, 2023 | Syndicated blog

Easy property inheritance in Optimizely CMS

Ever needed a way to inherit property values down through the page hierarchy in Optimizely CMS 12? Now there is an easy way to enable that.

Allan Thraen | Jun 5, 2023 | Syndicated blog

Content Bulk Edit

Bulk Edit is a plugin that allows to make changes to more than one Content at the same time. This can save time by simultaneously updating multiple...

Grzegorz Wiecheć | Jun 5, 2023 | Syndicated blog

Caching & Rendering of metadata from the DAM

For the EPiServer.Cms.WelcomeIntegration.UI 1.2.0 release, we have facilitated the ability to cache and render metadata from assets within the DAM....

Matthew Slim | Jun 2, 2023

Conversion Rate Optimization: Maximizing ROI through Data-Driven Decisions

In today's digital landscape, businesses are constantly looking for ways to improve their online presence and drive more conversions. One effective...

JessWade | Jun 1, 2023