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);
As you can see the parameter that is passed is the client context. Before calling the override function we have to instantiate the client context and set the property to override. In order to assign values to each row in the list we will be overriding the Fields Property.
- (function()
- {
- varoverrideCurrentContext = {};
- overrideCurrentContext.Templates = {};
- overrideCurrentContext.Templates.Fields = {
- 'Sales_x0020_Progress':
- {
- 'View': TargetProgress
- }
- };
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);
- })();
Thus we have assigned the Fields Property with an override. Here we assigned a method which will be called for each of the row within the list. The return value will be assigned to the Column - Sales Progress.
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 !";
- }
- }
Here we will compare two column values (total sales and target sales) and based on the result we will assign the third column (Sales Progress) a value.
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.