Introduction
In SharePoint 2010, we use XSLT in order to customize the style of search results and view of list and list items. In SharePoint 2013 and for SharePoint Online it is recommended to use Client side rendering. Obliviously CSR is through JavaScript (of course; including CSS and HTML as well). Again, if you are familiar with XSLT you can still use it in SharePoint 2013 but note that XSLT falls under server side rendering.
Client Side rendering can be done via two methods.
Display Template: Used to style search results.
JS Link: Used to style the view of list, list items, fields, content type, List default pages (NewForm.aspx, EditForm.aspx, DispForm.aspx, AllItems.aspx ), List view web parts.
In this post I will just describe how to override a column field with our custom value and in future I will go in details with styling and all.
Advantages of Client Side rendering over using XSLT
- Avoid unnecessary loads on server: JavaScript, HTML and CSS render on client browser and they avoid unnecessary loads on the server.
- Easy to Debug: We use JavaScript for client side render. Modern browser supports debugging facility which XSLT is not providing.
- More Libraries: We can use other JavaScript libraries as we needed (such as JQuery)
- Easy to learn: SharePoint developers are becoming comfortable with JavaScript because of the trend of SharePoint hosted apps. So most developers are comfortable with JavaScript and feel easier to learn JavaScript than XSLT.
- Flexibility: We can override particular fields, header, footer or the entire view of the list as we needed.
Disadvantages
- Depends on client browser: If a user use old version of browser it may take long time to process the view; and if the user has disabled JavaScript or blocked JavaScript the rendering will not work. In case we use XSLT will work.
- SEO Problem: Crawlers may not be able to understand the content generated by JavaScript.
Now lets cover some basic topics which we need
How to add JSLink to a List
Solution 01 – Through the browser user interface
- Go to the List.
- Click on edit page
- Click on Edit Web Part
- At the bottom of the web part properties control there is a category called “Miscellaneous”. Expand it. At the bottom you can find a text box to add JS Link and there you can define the url for JS Link.
Solution 02 – Through Visual Studio
If you are trying to add a SharePoint list through visual studio declaratively this will be useful.
- Click on the schema.xml of your list
Basic Example
Now let’s create a basic JavaScript which overrides the value of our Body column of a announcement list.
Steps:
- Create an announcement list.
- Change the default view which displays the Body.
- Now create a JavaScript file in side layouts folder inside 15 hive.
- Usually the path will be C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\
- In my case I created the JavaScript file as cutombody.js
- Now copy and paste the below code
- (function () {
- // Initialize the variables for overrides objects
- var overrideCtx = {};
- overrideCtx.Templates = {};
- /*
- * Using the Fields override leaves the rest of the rendering intact, but
- * allows control over one or more specific fields in the existing view
- */
- overrideCtx.Templates.Fields = {
- 'Body': { 'View': 'Our Custom Value' }
- };
- /*
- * Register the template overrides.
- */
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
In the above code overrideCtx variables holds current context of the list item. overrideCtx.Templates.Fields enables to access the individual list field. SPClientTemplates.TemplateManager.RegisterTemplateOverrides function registers our customized template; so that our list can use it.
Here we are overriding the Body (the internal name of the field) with the value “Our Custom Value”. Here the view is the scope. The scope can be DisplayForm, View, EditForm, NewForm.
Now save the JavaScript and give the JSLink as I have mentioned above under Solution 01. Your url will can be something like this. /_layouts/15/cutombody.js
Now our Announcement list will only display “Our Custom Value” in body field.
Conclusion
Stay Calm I will update more post with different examples with the code in upcoming days.
I have attached a Visual Studio solution file with a custom announcement list and have added the the list to XsltListViewWebPart and define the JSLink for the web part as well as for the list through the schema.xml. Please download it and make use of it.
https://drive.google.com/file/d/0ByEnOE8DAdvhcnRkWFBsR3ByYjQ/edit?usp=sharing
This comment has been removed by the author.
ReplyDeleteHi Suhail,
ReplyDeleteIn reference to your article: http://jsuhail.blogspot.com/2014/09/client-side-rendering-using-jslink-post.html
I`m after adding eclipse for text more than 250 characters, so on the webpart it shows only text upto 250 characters and for rest it shows an eclipse (...)
I have tried the following code and seems something gone missing, please assist.
Code:
(function () {
// Create object that have the context information about the field that we want to change it's output render
var bodyFieldContext = {};
bodyFieldContext.Templates = {};
bodyFieldContext.Templates.Fields = {
// Apply the new rendering for Priority field on List View
// update by Jason: name of the field: msg and view name : AllItems (List name: msgtext)
"msg": { "AllItems": bodyFieldTemplate }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFieldContext);
})();
// This function provides the rendering logic for list view
function bodyFieldTemplate(ctx) {
var body = ctx.CurrentItem[ctx.CurrentFieldSchema.body];
var bodyHTML="";
if (body.toString().length > 250) {
bodyHTML = body.toString().substring(0,250) + "...";
}
return bodyHTML;
}
great
ReplyDelete