Implement KPI In Office 365 And SharePoint 2016 List View Using Client Side Rendering

Client side rendering was introduced in SharePoint 2013. The primary purpose of CSR is to provide the conditional formatting of the data present within the list views. Prior to SharePoint 2013, XSLT formatting was the way to go in order to implement the conditional formatting of the list views. XSLT formatting requires an 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

Let’s see how to implement CSR in the list view to create a Key Performance Indicator (KPI).

Requirement

Assign KPI to a list column, based on a condition; i.e., comparing two other columns in the same list.

target status

The main purpose of the Client side rendering is to tap in to the rendering process. The entry point is the function, shown below:

  1. 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 the values to each row in the list, we will be overriding the ‘fields’ property.
  1. (function()  
  2. {  
  3.     varoverrideCurrentContext = {};  
  4.     overrideCurrentContext.Templates = {};  
  5.     overrideCurrentContext.Templates.Fields = {  
  6.         'Target_x0020_Status':  
  7.         {  
  8.             'View': TargetStatus  
  9.         }  
  10.     };  
  11.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);  
  12. })();  
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 an HTML image element, which will be assigned to the column target status.

Now, let’s have a look at the overriding function TargetStatus.
  1. function TargetStatus(ctx)  
  2. {  
  3.     vartotalSalesVal = ctx.CurrentItem.Total_x0020_Sales.replace(",""");  
  4.     vartargetSalesVal = ctx.CurrentItem.Sales_x0020_Target.replace(",""");  
  5.     if (parseInt(totalSalesVal) > parseInt(targetSalesVal))  
  6.     {  
  7.         returnvalue = "<span><imgsrc='/sites/Playground/SiteAssets/TargetMet.jpg' width='20px' height='20px'style='border:0px;'></span>";  
  8.         return returnvalue;  
  9.     }  
  10.     else if (parseInt(targetSalesVal) - parseInt(totalSalesVal) <= 1000)  
  11.     {  
  12.         returnvalue = "<span><imgsrc='/sites/Playground/SiteAssets/Reaching.jpg' width='20px' height='20px'style='border:0px;'></span>";  
  13.         return returnvalue;  
  14.     }  
  15.     else  
  16.     {  
  17.         returnvalue = "<span><imgsrc='/sites/Playground/SiteAssets/LongWay.jpg' width='20px' height='20px'style='border:0px;'></span>";  
  18.         return returnvalue;  
  19.     }  
  20. }  
Here, we will compare the two column values (Total sales and Sales Target) and based on the result, we will assign the third column (Target Status) an HTML image element, which will correspond to the related KPI .The image element will have the src attribute, pointing to an image, that is uploaded to SharePoint location, say: ‘Site Assets’. We will be using the relative URL of the image.

Ctx.CurrentItem.ColumnName will get the value of the column. Since it is a number field, it has an inherent comma for the values greater than 1000. To do a numerical comparison, we have to remove comma, which is done, as shown below:
  1. vartotalSalesVal =ctx.CurrentItem.Total_x0020_Sales.replace(",""");  
Once we have comma free value, we will compare the two values and assign Target Status; 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. The column values in each row will be compared and based on the result whereas the third column will have a new value. Thus, we have returned an HTML image element, that will be assigned to the destination column for each row during the rendering process.

Note

In order to implement a rendering logic to each row in the list, override the fields property.

Full Code
  1. (function()  
  2. {  
  3.     varoverrideCurrentContext = {};  
  4.     overrideCurrentContext.Templates = {};  
  5.     overrideCurrentContext.Templates.Fields = {  
  6.         'Target_x0020_Status':  
  7.         {  
  8.             'View': TargetStatus  
  9.         }  
  10.     };  
  11.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCurrentContext);  
  12. })();  
  13.   
  14. function TargetStatus(ctx)  
  15. {  
  16.     vartotalSalesVal = ctx.CurrentItem.Total_x0020_Sales.replace(",""");  
  17.     vartargetSalesVal = ctx.CurrentItem.Sales_x0020_Target.replace(",""");  
  18.     if (parseInt(totalSalesVal) > parseInt(targetSalesVal))  
  19.     {  
  20.         returnvalue = "<span><imgsrc='/sites/Playground/SiteAssets/TargetMet.jpg' width='20px' height='20px'style='border:0px;'></span>";  
  21.         return returnvalue;  
  22.     }  
  23.     else if (parseInt(targetSalesVal) - parseInt(totalSalesVal) <= 1000)  
  24.     {  
  25.         returnvalue = "<span><imgsrc='/sites/Playground/SiteAssets/Reaching.jpg' width='20px' height='20px'style='border:0px;'></span>";  
  26.         return returnvalue;  
  27.     }  
  28.     else  
  29.     {  
  30.         returnvalue = "<span><imgsrc='/sites/Playground/SiteAssets/LongWay.jpg' width='20px' height='20px'style='border:0px;'></span>";  
  31.         return returnvalue;  
  32.     }  
  33. }  
Let’s see how to add the Code as 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 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.

JSLink

Click Apply. This will apply JSLink to the list view.

Output

Output

Thus, we have seen how we can compare the two column values and assign a KPI image to the third column, using JSLink and Client Side Rendering.