SharePoint 2013 - jCarousel Using CSR

Introduction

This article deals with integrating a simple jQuery carousel plugin called iCarousel into ListView webpart using Client-side Rendering Model.

You should have prior knowledge of CSR basics before you go through this article.

Step 1

Place all the necessary JavaScript files and CSS files into Site Assets library.

  • Navigate to the Site Assets library in your site collection.
  • Create a folder named JS. This is to place all the JS files.
  • Create another folder named Style. This is to place all the stylesheet files.
  • Create another folder named Images. This is to place all the images.

For this example, I have used jCarousel plugin.

The JavaScript files required for this carousel are

  • Jquery.min.js
  • JCarousel.Basic.js
  • Jquery.JCarousel.min.js

The stylsheets required are,

  • JCarousel.Basic.css

Place the files in the respective folders within the Site Assets library.

Step 2

Create the List with the content for which the carousel needs to be shown. I have created a Cars List as below with 4 columns.

  • CarName(Single Line of Text Column)
  • ShortDescription(Single Line of Text Column)
  • LongDescription(Multiple Line of Text Column)
  • CarImage(Hyperlink or Picture column)
     
Step 3

Create a new page and add the above Cars List as a web part to the page.

  • Click on Edit
  • Click on Insert Tab and Click on web part
  • Select the Cars list from the Apps Category
  • And Add
  • Click on Save

Now, the page will look as below which is the default rendering of the ListView webpart.

 
Step 4

Now, create the JavaScript for rendering the above ListView webpart as a carousel. In order for the list to be converted to a carousel, we need to override the

  • Header
  • Item
  • Footer

Before we override, we need to check if all the necessary files in Step 1 have been included in the page where the Cars List web part has been placed.

If not, we can do the same in this JavaScript file before the override scripts.

As a best practice, always create a namespace in JavaScript so that it acts as a module for related functions.

To load common files, I have created a namespace called commonFiles which has 2 functions - one for creating a script tag and the other for creating a link tag.

  1. var commonFiles = commonFiles || {};  
  2. //CommonFiles Namespace  
  3. commonFiles.LoadFiles = {  
  4.     LoadJs: function(filename) {  
  5.         var scriptElement = document.createElement('script');  
  6.         scriptElement.setAttribute("type""text/javascript");  
  7.         scriptElement.setAttribute("src", filename);  
  8.         document.getElementsByTagName("head")[0].appendChild(scriptElement)  
  9.     },  
  10.     LoadCss: function(filename) {  
  11.         var linkElement = document.createElement('link');  
  12.         linkElement.setAttribute("rel""stylesheet");  
  13.         linkElement.setAttribute("type""text/css");  
  14.         linkElement.setAttribute("href", filename);  
  15.         document.getElementsByTagName("head")[0].appendChild(linkElement)  
  16.         //return linkElement;  
  17.     }  
  18. };  
The above method will be used in the self executing function which holds the override functionality for CSR.

Here, I am loading the files present in Step 1.

  1. //CSR Script for car list  
  2. (function() {  
  3.     commonFiles.LoadFiles.LoadCss("SiteAssets/Style/jcarousel.css");  
  4.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jquery.js");  
  5.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jcarousel.js");  
  6.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jcarousel.basic.js");  
  7.     var overrideCtx = {};  
  8.     overrideCtx.Templates = {};  
  9.     overrideCtx.Templates.Header = carCarousel.car.carHeaderHtml;  
  10.     overrideCtx.Templates.Item = carCarousel.car.carItemHtml;  
  11.     overrideCtx.Templates.Footer = carCarousel.car.carFooterHtml;  
  12.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);  
  13. })();  
Once the necessary files are loaded for the carousel, we will override the header, item, and footer of the webpart.

Here again, I have used a namespace for carCarousel in which 3 functions are present, namely

  • carHeaderHtml
  • carItemHtml
  • carFooterHtml
  1. //CarCarousel Namespace  
  2. var carCarousel = carCarousel || {};  
  3. carCarousel.car = {  
  4.     carItemHtml: function(ctx) {  
  5.         var cars;  
  6.         //var html = "Car Name: <b>" + ctx.CurrentItem.CarName + "</b>";  
  7.         var html = "<li><div> <img src='" + ctx.CurrentItem.CarImage + "' width='1000px' height='400px' /><div style='border:0px solid green;height:20px;width:250px;position:relative;top:-90px;left:650px;color:#FFF;background- color:#000;opacity:0.7;border-radius:3px;text-align:center;font-weight:bold;'>" + ctx.CurrentItem.ShortDescription + "</div> </div><div style='width:600px'>" + ctx.CurrentItem.LongDescription + "</div></li>";  
  8.         //html += "<br> Car Description: <b>" + ctx.CurrentItem.ShortDescription + "</b><br><br>";  
  9.         return html;  
  10.     },  
  11.     carHeaderHtml: function() {  
  12.         var carHeaderHtml = "<p style='font-size:30px;font-weight:bold;'>Latest cars of 2017</p><div class='jcarousel-wrapper'><div class='jcarousel'><ul>";  
  13.         return carHeaderHtml;  
  14.     },  
  15.     carFooterHtml: function() {  
  16.         var carFooterHtml = "</ul></div>";  
  17.         carFooterHtml += "<a href='#' class='jcarousel-control-prev'>‹</a>";  
  18.         carFooterHtml += "<a href='#' class='jcarousel-control-next'>›</a>";  
  19.         carFooterHtml += "<p class='jcarousel-pagination'></p></div>";  
  20.         return carFooterHtml;  
  21.     }  
  22. };  

The above methods will be called in the self-executing function after the LoadFiles methods.

  1. //CSR Script for car list  
  2. (function() {  
  3.     commonFiles.LoadFiles.LoadCss("SiteAssets/Style/jcarousel.css");  
  4.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jquery.js");  
  5.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jcarousel.js");  
  6.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jcarousel.basic.js");  
  7.     var overrideCtx = {};  
  8.     overrideCtx.Templates = {};  
  9.     overrideCtx.Templates.Header = carCarousel.car.carHeaderHtml;  
  10.     overrideCtx.Templates.Item = carCarousel.car.carItemHtml;  
  11.     overrideCtx.Templates.Footer = carCarousel.car.carFooterHtml;  
  12.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);  
  13. })();   
The complete JavaScript code for car carousel is given below.
  1. var commonFiles = commonFiles || {};  
  2. //CommonFiles Namespace  
  3. commonFiles.LoadFiles = {  
  4.     LoadJs: function(filename) {  
  5.         var scriptElement = document.createElement('script');  
  6.         scriptElement.setAttribute("type""text/javascript");  
  7.         scriptElement.setAttribute("src", filename);  
  8.         document.getElementsByTagName("head")[0].appendChild(scriptElement)  
  9.         //return scriptElement;  
  10.     },  
  11.     LoadCss: function(filename) {  
  12.         var linkElement = document.createElement('link');  
  13.         linkElement.setAttribute("rel""stylesheet");  
  14.         linkElement.setAttribute("type""text/css");  
  15.         linkElement.setAttribute("href", filename);  
  16.         document.getElementsByTagName("head")[0].appendChild(linkElement)  
  17.         //return linkElement;  
  18.     }  
  19. };  
  20. //CarCarousel Namespace  
  21. var carCarousel = carCarousel || {};  
  22. carCarousel.car = {  
  23.     carItemHtml: function(ctx) {  
  24.         var cars;  
  25.         //var html = "Car Name: <b>" + ctx.CurrentItem.CarName + "</b>";  
  26.         var html = "<li><div> <img src='" + ctx.CurrentItem.CarImage + "' width='1000px' height='400px' /><div style='border:0px solid green;height:20px;width:250px;position:relative;top:-90px;left:650px;color:#FFF;background- color:#000;opacity:0.7;border-radius:3px;text-align:center;font-weight:bold;'>" + ctx.CurrentItem.ShortDescription + "</div> </div><div style='width:600px'>" + ctx.CurrentItem.LongDescription + "</div></li>";  
  27.         //html += "<br> Car Description: <b>" + ctx.CurrentItem.ShortDescription + "</b><br><br>";  
  28.         return html;  
  29.     },  
  30.     carHeaderHtml: function() {  
  31.         var carHeaderHtml = "<p style='font-size:30px;font-weight:bold;'>Latest cars of 2017</p><div class='jcarousel-wrapper'><div class='jcarousel'><ul>";  
  32.         return carHeaderHtml;  
  33.     },  
  34.     carFooterHtml: function() {  
  35.         var carFooterHtml = "</ul></div>";  
  36.         carFooterHtml += "<a href='#' class='jcarousel-control-prev'>‹</a>";  
  37.         carFooterHtml += "<a href='#' class='jcarousel-control-next'>›</a>";  
  38.         carFooterHtml += "<p class='jcarousel-pagination'></p></div>";  
  39.         return carFooterHtml;  
  40.     }  
  41. };  
  42. //CSR Script for car list  
  43. (function() {  
  44.     commonFiles.LoadFiles.LoadCss("SiteAssets/Style/jcarousel.css");  
  45.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jquery.js");  
  46.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jcarousel.js");  
  47.     commonFiles.LoadFiles.LoadJs("SiteAssets/JS/jcarousel.basic.js");  
  48.     var overrideCtx = {};  
  49.     overrideCtx.Templates = {};  
  50.     overrideCtx.Templates.Header = carCarousel.car.carHeaderHtml;  
  51.     overrideCtx.Templates.Item = carCarousel.car.carItemHtml;  
  52.     overrideCtx.Templates.Footer = carCarousel.car.carFooterHtml;  
  53.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);  
  54. })();   

Step 5

Place the created JavaScript file into the Site Assets folder where all the JS files are present.

Step 6

After placing the JavaScript file, this needs to be placed in the ListView webpart added into the page.

  • Edit the page.
  • Click on Edit Webpart
  • Expand Miscellaneous
  • Give the path to the javascript file in the JSLink.
  • Click OK.

     
You will see the below after you click OK in the Edit webpart section.



Summary

CSR is an easy way to change the look and feel of the List View webpart.

Prior to Sharepoint 2013, rendering was done by XSLT but in SharePoint 2013, it has moved to client side rendering using JSLink webpart property.