This content is archived. See latest version here


EPiServer.Framework provides the number of classes and interfaces that can be used to define available client resources, to require client resource depending on the current context and to render client resources on the page. This information will be interesting mostly for module and add-on developers who need to render or inject client resources on the pages without modifying the page templates.

EPiServer.Framework.Web.Resources.ClientResource class defines client resource that can be used on site pages. Please see class reference for more information.

Client resource providers

Client resource provider returns definitions of available client resources that can be requested by name and added on the page.

Default client resource provider for modules

There is client resource provider that finds and returns all client resources that are defined in manifests of public and protected modules and add-ons. So there is no need to implement custom client resource provider if your resources are defined in module.config files.

The following example shows client resource definitions in a module manifest:

<?xml version="1.0" encoding="utf-8" ?>
    <add name="epi.samples.Module.Styles" path="ClientResources/Styles.css" resourceType="Style"/>

See Configuring module.config document for detailed information.

Implementing the custom client resource provider

The provider class must implement EPiServer.Framework.Web.Resources.IClientResourceProvider interface. Also it should be decorated with EPiServer.Framework.Web.Resources.ClientResourceProviderAttribute to register this implementation automatically.



public class ClientResourceProvider : IClientResourceProvider
    public IEnumerable<ClientResource> GetClientResources()
        return new[] 
            new ClientResource
                Name = "epi.samples.Module.FormHandler",
                Path = Paths.ToClientResource("SampleModuleName", "ClientResources/FormHandler.js"),
                ResourceType = ClientResourceType.Script,
                Dependencies = new List<string> { "OtherResourceName" }

This sample provider returns one script resource which depends on another client resource. Note that the full path to the script file in module directory is resolved by module name.

Requiring client resources

There is a couple of ways how developer can require certain client resource to be rendered on the page without accessing and changing page templates. Often it is required when developing some kind of add-on or plug-in.

Implementing client resource register

One way is implementing EPiServer.Framework.Web.Resources.IClientResourceRegister interface. Client resource register requires client resources for specific rendering area and depending on the current context.

Client resource registers must be executed before page rendering. Registers are called automatically if templates inherit base classes for page and controller that are defined in EPiServer CMS (EPiServer.PageBase and EPiServer.Web.Mvc.PageController). Also there is base register class that can be inherited when requesting resources for CMS page templates. See CMS SDK documentation for more information.

Register implementation class should be decorated with EPiServer.Framework.Web.Resources.ClientResourceRegisterAttribute to make it discoverable automatically.

public class ClientResourceRegister : IClientResourceRegister
    public void RegisterResources(IRequiredClientResourceList requiredResources, HttpContextBase context)

RegisterResources method takes 2 arguments: the list of required client resources and current HTTP context.

Information from current HTTP context can be used to decide which resources are required for this context.

The list of required client resources provides the number of Require methods to register required client resources.

Client resource can be required by name if this resource is known and it was defined in any module manifest or it is returned by custom resource provider. Note that sample register requires 2 resources by name; these resources were defined in sample module manifest and sample client resource provider.

Also it is possible to require JavaScript, CSS or HTML resource by specifying its URL or inline content that should be injected on the page. Please see EPiServer.Framework.Web.Resources.IRequiredClientResourceList members for more information.

The third resource was not defined in module.config or provider and this resource is required using its URL in sample register implementation.

Client resource register for CMS pages

EPiServer CMS provides base class EPiServer.Web.Resources.PageClientResourceRegister that you can inherit when implementing client resource register for CMS pages.

Client resource settings

Each Require method returns settings for required client resource that can be used to specify area where resource should be rendered and filter resources by type.

Rendering Areas

Rendering area is a string that identifies the place in templates where resource should be rendered.

There are 2 default rendering areas that are required for all templates:

  • Header: for resources that should be added inside the <head> tag. Styles preferably should be required for this area.
  • Footer: for resources that should be added in the bottom of the page, before closing </body> tag. Scripts preferably should be put in this area.

Rendering area can be set for resource by using AtArea(areaName), AtHeader(), AtFooter() methods of client resource settings. Header is default option if rendering area is not specified explicitly.

Sample register requires style resource without setting the area, so this style is going to be rendered in Header area. Two other script resources are required for Footer area explicitly.

Filter Resources by Type

Sometimes several resources of different types are grouped and have the same name. It is possible to request only resources of certain type when requesting resources by name.

For example, all jQuery UI scripts and CSS files can be defined using the one name jquery.ui. Then jQuery UI style resource can be required in Header area and scripts can be required in Footer:


Requesting client resources using ClientResources class

Client resource register implementation is required mostly when developing module and add-on that needs some injections.

It is also possible to require client resource by using EPiServer.Framework.Web.Resources.ClientResources static class. It provides Require methods with the same signatures as methods in IRequiredClientResourceList interface.

This is more handy way if you need to request client resources in code behind or directly in templates. Make sure that resource is requested before rendering of required resources for corresponding area.

Rendering the required client resources

Required client resources can be rendered for corresponding area by using web control or HTML helper.

Any template must render required client resources at least for 2 default areas. Resources for Header area should be rendered inside <head> tag. Resources for Footer area should be rendered in the bottom of the page, before closing </body> tag.

Web Forms control

Use EPiServer.Framework.Web.WebControls.RequiredClientResources control to render required resources in Web Forms templates. The following code renders client resources required for Header area:

<head runat="server">
    <EPiServer:RequiredClientResources RenderingArea="Header" ID="RequiredResourcesHeader" runat="server" />

MVC Helper

Use HTML helper extension to render required resources in MVC templates. The following code renders client resources required for the Footer area:

    <%= Html.RequiredClientResources(RenderingArea.Footerbody>

Last updated: Feb 23, 2015