Client Side Rendering was introduced in SharePoint 2013. The primary purpose of CSR is to provide conditional formatting of data present within the List Views. Prior to SharePoint 2013 XSLT formatting was the way to go in order to implement conditional formatting of List Views. XSLT formatting required in depth knowledge of working with XSL and debugging was also cumbersome. However with CSR we can tap in to the rendering process and override some of the default properties of the list view using JavaScript. Some of the properties that can be overridden are,
- OnPreRender
- OnPostRender
- View
- Body
- Item
- Fields
- Header
- Footer
OnPrerender allows us to write some logic even before the list view is rendered while OnPostrender allows us to modify the list view once the view has been rendered. Similarly each of the properties can be over ridden during run time to accomplish different list view modifications and at different list view locations.
Let’s implement a practical scenario using CSR.
Requirement
Assign value to Sales Progress Column during run time rendering process by comparing Total Sales and Sales Target.

The main purpose of Client Side Rendering is to tap in to the rendering process. The entry point is the below function.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);
- (function()
- {
- varoverrideCurrentContext = {};
- overrideCurrentContext.Templates = {};
- overrideCurrentContext.Templates.Fields = {
- 'Sales_x0020_Progress':
- {
- 'View': TargetProgress
- }
- };
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);
- })();
Now let’s have a look at the overriding function TargetProgress,
- functionTargetProgress(ctx)
- {
- vartotalSalesVal = ctx.CurrentItem.Total_x0020_Sales.replace(",", "");
- vartargetSalesVal = ctx.CurrentItem.Sales_x0020_Target.replace(",", "");
- if (parseInt(totalSalesVal) > parseInt(targetSalesVal))
- {
- return "On Target";
- }
- else if (parseInt(targetSalesVal) - parseInt(totalSalesVal) <= 1000)
- {
- return "Reaching Target";
- }
- else
- {
- return "Long way to target !";
- }
- }
Ctx.CurrentItem.ColumnName will get the value of the column. Since it is a number field, it has an inherent comma for values greater than 1000. To do numerical comparison we have to remove the comma which is done as below,
vartotalSalesVal =ctx.CurrentItem.Total_x0020_Sales.replace(",", "");
Once we have a comma free value, we will compare two values and assign Sales Progress a return value based on the result.
Since we have overridden the Field Property, the overriding method will run for each row in the list. Thus the column values in each row will be compared and based on the result the third column will have a new value.
Note
In order to implement a rendering logic to each row in the list, override the Fields property.
- (function()
- {
- varoverrideCurrentContext = {};
- overrideCurrentContext.Templates = {};
- overrideCurrentContext.Templates.Fields = {
- 'Sales_x0020_Progress':
- {
- 'View': TargetProgress
- }
- };
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);
- })();
- functionTargetProgress(ctx)
- {
- vartotalSalesVal = ctx.CurrentItem.Total_x0020_Sales.replace(",", "");
- vartargetSalesVal = ctx.CurrentItem.Sales_x0020_Target.replace(",", "");
- if (parseInt(totalSalesVal) > parseInt(targetSalesVal))
- {
- return "On Target";
- }
- else if (parseInt(targetSalesVal) - parseInt(totalSalesVal) <= 1000)
- {
- return "Reaching Target";
- }
- else
- {
- return "Long way to target !";
- }
- }
- Let’s see how to add the Code as a JSLink to the List view. Save the code as a js file and upload it to one of the Libraries, say: Site Assets.
- Go to the edit page of the list view by appending the url“? ToolpaneView=2” at the end of the list view’s .aspxURL.
- Click on Edit Web part for the list view. Under Miscellaneous section there is a text box for entering JSLink.
- Add the relative url of the js file after ~site or ~sitecollection depending upon the location of the Site Assets Library (or any other repository) where you have uploaded the js file. I have uploaded to site level Site Assets ,hence using the token ~site.
Click on Apply. This will apply JSLink to the list view.
Output

Thus we have populated the Sales Progress column during run time. It was initially empty and on run time it has generated values dynamically by comparing two other column values. CSR is highly useful in tapping into the rendering process of list view. We will cover more of CSR practical uses in the upcoming articles.

Steve APosted Mar 10, 2020, 2:18 PM
Nice... now i want the values to be stored. in my case they are colors...CSR does not store these values... how do i get them to be stored in the list item?
Santhakumar MunuswamyPosted Jun 25, 2016, 1:10 AM
Thank you for nice article
RajaPosted Jun 20, 2016, 2:26 AM
Nice..
Debasis SahaPosted Jun 20, 2016, 12:51 AM
Good One..
Vignesh ManiPosted Jun 18, 2016, 3:16 PM
NIce