Published: 27 September 2017
Essentially CSR with JSLink taps into list view rendering and overrides properties that govern the list view look and feel. Some of the properties that can be over ridden are:
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 we can override the Item property if we want some logic to be implemented for each row of the List View and we can over ride the Fields property to update the rendering of the List Column. Header and Footer as the name suggests can be used to add Header and Footer Customization to the List.
In this article we will implement CSR in list view to create Key Performance Indicator (KPI).
Lets see how to assign KPI to a List column based on a condition i.e.: comparing two other columns in the same list.
After applying client side rendering to the above list view, we will generate KPI in the blank column as per the predefined condition as shown below:
The main purpose of Client Side Rendering is to tap in to the rendering process. The entry point is the below function call.
As you can see the parameter that is passed is the current context. Before calling the override function we have to instantiate the context and set the property to override. In order to assign values to each list column item we will be over riding the ‘Fields’ Property.
Thus we have assigned the Fields Property with an override. Here we assigned a method which will be called for each of the list column item. 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.
Here we will compare 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 the 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 values greater than 1000. To do numerical comparison we have to remove comma which is done as below:
var totalSalesVal =ctx.CurrentItem.Total_x0020_Sales.replace(",", "");
Once we have comma free value, we will compare 2 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 column item in the list. The column values in each row will be compared and based on the result 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.
The complete code used for the demo is given below :
The above code will be saved as a JS file and can be uploaded to the site assets library of SharePoint. In order to override the specific List View, append “?toolpaneview=2” at the end of the List View URL to go to the edit mode of the page.
Specify the URL of the JS File in the JS Link section as shown above. While specifying the JSLink file URL we have to take a special note that we will be specifying site/site collection relative URL using the below tokens.
Once you click on Apply, the Client Side Rendering will modify the list view and add KPI to the list column as shown below :
Thus we have seen how we can compare two column values and assign a KPI image to the third column using JSLink and Client Side Rendering.We will see how to implement business intelligence bar charts using CSR and ChartJS in the upcoming article.
Priyaranjan is a SharePoint Consultant with 7+ years experience in developing and deploying SharePoint Applications.He has worked on various SharePoint iterations starting from MOSS 2007 through SharePoint 2016 and Office 365. He is a frequent contributor at Microsoft TechNet and has won 33 Gold Medals in various TechNet Wiki Guru Monthly Competitions.As a token of appreciation for the TechNet community activities, he was interviewed by Microsoft Program Manager,Ed Price. He has also published 300 Articles and 4 SharePoint Ebooks in different technical communities.You can find his Microsoft TechNet contributions here