Darren Sunderland
Sep 28, 2018
(0 votes)

Lazy-Loading blocks in an EpiServer Page

In one of our sites we were requested to create a new page type, which the content editors could use to host a list of hints/tips to maximise what is available through the website.  The initial concept is a simple task to achieve in EpiServer, but we realised fairly quickly that there could be a potential issue with page load time when the editors informed us that there would be over 100 hints/tips added to the page.  This led us to look into the option of lazy-loading blocks into the page to speed up the load time, and also to improve the user experience.

Detailed below is a working version of the lazy-load solution, based on the Alloy template solution.



This is a standard block type, which was created to allow the editors to manage the hints for the page.

Model (HintBlock.cs)

    public class HintBlock : BlockData
        [Display(GroupName = SystemTabNames.Content, Order = 1, Name = "Hint Title")]
        public virtual string HintTitle { get; set; }

        [Display(GroupName = SystemTabNames.Content, Order = 2, Name = "Hint Content")]
        public virtual XhtmlString HintContent { get; set; }

        [Display(GroupName = SystemTabNames.Content, Order = 3, Name = "Hint Image")]
        public virtual ContentReference HintImage { get; set; }

View (HintBlock.cshtml)

@model HintBlock

        <p>@Html.DisplayFor(m => m.HintContent)</p>


This is a new block type, which was created to allow the editors to organise the hints to display on the page.  Notice the last property of the model (HintsForDisplay), this is to allow the view to display the required number of blocks during the initial page load as opposed to loading all blocks in the content area - this property is populated within the controller for the page.

Model (HintInformationPage)

    public class HintListingPage : SitePageData
        [Display(GroupName = SystemTabNames.Content, Order = 1, Name = "Page Title")]
        public virtual String PageTitle { get; set; }

        [Display(GroupName = SystemTabNames.Content, Order = 2, Name = "Page Introduction Text")]
        public virtual XhtmlString PageSubText { get; set; }

        [Display(GroupName = SystemTabNames.Content, Order = 3, Name = "Page Hint Content")]
        public virtual ContentArea HintContentArea { get; set; }

        [Display(GroupName = SystemTabNames.Content, Order = 4, Name = "Hints Per Page")]
        public virtual int HintsPerPage { get; set; }

        public virtual List<HintBlock> HintsForDisplay { get; set; }

View (Index.cshtml)

@model PageViewModel<HintListingPage>

@{ Layout = "~/Views/Shared/Layouts/_LeftNavigation.cshtml"; }

<h2>@Html.DisplayFor(t => t.CurrentPage.PageTitle)</h2>
<p>@Html.DisplayFor(t => t.CurrentPage.PageSubText)</p>

<div id="hints__blockarea">
        foreach (var item in Model.CurrentPage.HintsForDisplay)
            Html.RenderPartial("HintBlock", item);
<br />
    <div id="loading">
        <p id="loading__message" style="color: red;"><b>Loading Next Hints...</b></p>
    <button id="load__more__elements" onclick="buttonHandler();">Show More</button>
<label id="lbl_finished">No more records to load.</label>

<script type="text/javascript">
    var pageID = @Model.CurrentPage.ContentLink.ID.ToString();
    var url = '@Request.Url.ToString()' + "LazyLoad";
    document.getElementById("loading__message").style.display = "NONE";
    document.getElementById("lbl_finished").style.display = "NONE";
<script src="~/Static/js/lazyLoading_HintInfo.js"></script>

Controller (HintListingPageController.cs)

This contains two regions - the "Rendering Methods" and the "Content Area Helper Methods" - in a production scenario the "Content Area Helper Methods" should be moved into an extension method to allow for use across all page types.

The GetHintBlocks method uses Linq to select the blocks, from the content area, which are required for the current display of the page. 

The standard Index method loads the initial version of the page, with the number of blocks required.

The LazyLoad method is called by an Ajax call from the view, and identifies the next batch of blocks which need to be added into the page. The return of this is the HTML required to render the blocks for the end page.

    public class HintListingPageController : PageController<HintListingPage>
        private static HintListingPage _currentPage = new HintListingPage();

        #region Page Rendering Methods
        public ActionResult Index(HintListingPage currentPage)
            _currentPage = currentPage;
            var model = PageViewModel.Create(currentPage);
            model.CurrentPage.HintsForDisplay = GetHintBlocks(null);
            return View(model);

        public ActionResult LazyLoad(int pageID, int? pageNum)
            _currentPage = (HintListingPage)DataFactory.Instance.GetPage(new PageReference(pageID));

            var blocks = GetHintBlocks(pageNum);
            return PartialView("HintList", blocks);

        public List<HintBlock> GetHintBlocks(int? pageNum)
            int lazyLoadBlocks = 5;
                lazyLoadBlocks = _currentPage.HintsPerPage == 0 ? lazyLoadBlocks : _currentPage.HintsPerPage;
            catch (Exception) { }

            List<HintBlock> rawItems = GetFilteredItemsOfType<HintBlock>(_currentPage.HintContentArea);
            var displayItems = from ri in rawItems
                               select ri;

            pageNum = pageNum ?? 1;
            int firstElement = ((int)pageNum - 1) * lazyLoadBlocks;

            return displayItems.Skip(firstElement).Take(lazyLoadBlocks).ToList();

        #region Content Area Helper Methods
        public static bool AreaIsNullOrEmpty(ContentArea contentArea)
            return contentArea == null || !contentArea.FilteredItems.Any();

        public static List<HintBlock> GetFilteredItemsOfType<HintBlock>(ContentArea contentArea)
            var items = new List<HintBlock>();

            if (AreaIsNullOrEmpty(contentArea))
                return items;

            var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

            foreach (var contentAreaItem in contentArea.FilteredItems)
                IContentData item;
                if (!contentLoader.TryGet(contentAreaItem.ContentLink, out item))
                if (item is HintBlock)

            return items;


Partial View (HintList.cshtml)

This is the partial HTML to display a list of the blocks.  This is used by the lazy-load method to return the formatted code to be injected into the page.

@model List<HintBlock>

@foreach (var block in Model)
    Html.RenderPartial("HintBlock", block);

Javascript (lazyLoading_HintInfo.js)

This Javascript handles the button click, from the end page, to load additional blocks for display.  This makes an Ajax call to the LazyLoad method of the controller, and injects the returned content into the div element of the page where the blocks are rendered.  There is also logic in place to show a loading message whilst the work is taking place and there is also logic to show/hide the load more hints button OR a message stating all hints are loaded.

var page = 1;
var isCallback = false;

function buttonHandler() {

function loadHintData(loadMoreRowsUrl) {
    if (page > -1 && !isCallback) {
        isCallback = true;
        isCallback = false;

        document.getElementById("loading__message").style.display = "BLOCK";

        dataUrl = loadMoreRowsUrl + "?pageID=" + pageID + "&pageNum=" + page;
        dataUrl = dataUrl.replace("/?", "?");
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState !== 4) return;

            if (xhttp.status === 200) {
                var web_response = xhttp.responseText;
                if (web_response.toString().trim().length !== 0) {
                    var main = document.getElementById("hints__blockarea");
                    main.insertAdjacentHTML('beforeend', web_response);
                    document.getElementById("load__more__elements").style.display = "BLOCK";
                else {
                    document.getElementById("load__more__elements").style.display = "NONE";
                    document.getElementById("lbl_finished").style.display = "BLOCK";

                document.getElementById("loading__message").style.display = "NONE";
            else {
                page = -1;
                document.getElementById("loading__message").style.display = "NONE";
                document.getElementById("load__more__elements").style.display = "NONE";
                document.getElementById("lbl_finished").style.display = "BLOCK";
        xhttp.open("GET", dataUrl, true);

Sep 28, 2018


Casper.Rasmussen Sep 28, 2018 12:23 PM

Hi Darren.

Thanks for the inspiration. I like the thinking!

Blogged about something similar a while back, which used ASP.NET WebApi to render the blocks. It might be worth for you to look at, to get the benefits of being more RESTfull in your approach. Have a look!


/Casper Aagaard Rasmussen 

Please login to comment.
Latest blogs
How to fix scheduled job 'Remove Abandoned BLOBs' if it keeps failing

Optimizely inlcudes a job named 'Remove Abandoned BLOBs'. This post will help you fix it if it's no longer working.

Henning Sjørbotten | Sep 26, 2023 | Syndicated blog

Optimizely Web Experimentation Metrics

Within the domain of Optimizely Web Experimentation Metrics, the emphasis is on objective key performance indicators (KPIs) selected to assess an...

Matthew Dunn | Sep 23, 2023 | Syndicated blog

Optimizely For you Intranet

Having been at Optimizely and in the CMS industry for nearly 16 years I have seen clients’ intranet requirements go from a simple site just to hous...

Robert Folan | Sep 22, 2023

Vulnerability in EPiServer.GoogleAnalytics v3 and v4

Introduction A potential security vulnerability was detected for Optimizely Google Analytics addon (including EPiServer.GoogleAnalytics and...

Bien Nguyen | Sep 20, 2023

Overriding Optimizely’s Content Recommendations Block to Implement Custom Recommendations

Introduction The Content Recommendations add-on for Optimizely CMS dynamically recommends content from your site tailored to the interests of each...

abritt | Sep 13, 2023 | Syndicated blog

Developer contest! Install the AI Assistant and Win Bose QC45 Headphones!

We are thrilled to announce a developer contest where you have the chance to win a pair of Bose Headphones. The goal is to be the first developer t...

Luc Gosso (MVP) | Sep 7, 2023 | Syndicated blog