SharePoint Pals
 | Sign In
How to use Client Side Rendering (JSLink) in a SharePoint Hosted APP
by Krishna KV 18 Sep 2016
Author
Team Leader, Aspire Systems
Visits   
Today  :   21     Total  :    3692

JSLink is a new feature that is introduced in SharePoint 2013. It is a combination of Html, javascript and css to customize the rendering of SharePoint List views and forms. It can be called as Client Side Rendering (CSR rather than writing a XSLT)

· The rendering happens in the client side, so the pages can load faster with CSR.

· Compare to XSLT, JavaScript will be easier to development and debugged.

· We can customize specific fields, header, body and footer.

Create a SharePoint hosted app.

clip_image002

clip_image004

clip_image006

Adding columns in the list.

clip_image008

Adding custom view to the list can be provide any name and select the columns to view.

clip_image010

Schema.xml under view 2 include the custom script.

 <View BaseViewID="2" Name="2757a7d6-f368-47e7-bbda-5bcfdb318b60" DisplayName="NewView" Type="HTML" WebPartZoneID="Main" SetupPath="pages\viewpage.aspx" Url="NewView.aspx">
 <ViewFields>
     <FieldRef Name="LinkTitle" />
     <FieldRef Name="StartDate" />
     <FieldRef Name="_EndDate" />
     <FieldRef Name="PercentComplete" />
     <FieldRef Name="Project" />
     <FieldRef Name="Edit" />
 </ViewFields>
 <Query />
 <Toolbar Type="Standard" />
 <XslLink Default="TRUE">main.xsl</XslLink>
 <JSLink Default="TRUE">~site/Scripts/ListRender.js</JSLink>
 </View>

Add the ListRender.js under the script folder

 (function () {
     var overrideCtx = {};
     overrideCtx.Templates = {};
     overrideCtx.Templates.Header = "<h2><#=ctx.ListTitle#></h2>" +
         "<table class='table table-striped'><tr><th>Title</th><th>Start Date</th><th>End Date</th><th>Project</th><th>Progress </th></tr>";
     // This template is assigned to the CustomItem function.
     overrideCtx.Templates.Item = customItem;
     overrideCtx.Templates.Footer = "</table>";
     overrideCtx.BaseViewID = 2;
     overrideCtx.ListTemplateType = 100;
     overrideCtx.OnPreRender = loadCss;
     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
 })();
 
 function customItem(ctx) {
     var ret = "<tr><td>" + ctx.CurrentItem.Title + "</td>";
     ret += "<td>" + ctx.CurrentItem.StartDate + "</td>";
     ret += "<td>" + ctx.CurrentItem._EndDate + "</td>";
     ret += "<td>" + ctx.CurrentItem.Project + "</td>";
     ret += "<td>" + getProgress(parseInt(ctx.CurrentItem["PercentComplete."])) + "</td></tr>"
     return ret;
 }
 
 function getProgress() {
     return  '<div class="progress">' +
             '<div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:' + value + '%">' + value + '%' +
             '</div></div>';
 }
 
 function loadCSS(ctx) {
     document.write('<link rel="Stylesheet" type="text/css" href="../../Content/bootstrap.css"/>');
 }

clip_image012

clip_image014

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation