SlideShare a Scribd company logo
Spca2014 js link and display templates hatch
Martin Hatch 
SharePoint Practice Lead @ Ballard Chalmers, UK 
Advanced List Rendering with JSLinkand Display Templates 
@MartinHatch 
https://martinhatch.com 
martin.hatch@hatchsolutions.co.uk 
Slide Deck: http://mhat.ch/SPConnect14 
Code: http://mhat.ch/JSLinkCode
•Custom Fields 
•Display Forms and Views 
•New and Edit Forms 
•Validation 
•Advanced Techniques (SOD / AJAX / JavaScript CSOM) 
•Custom Views 
•Headers & Footers 
•Item Templates 
•Gotchas (Quick Edit / Paging) 
•Multiple Language Support (RESX files in JavaScript!) 
•Leveraging JSLink 
•Deploying with Visual Studio 
Agenda
•Just some JavaScript which needs to be loaded on the page (the Script Editor Web Part is a nice way to do simple testing) 
•It essentially just returns some HTML as a string value 
•List Display Templates can be targeted to a specific List Template Type (e.g. 100 = Custom List, 101 = Document Library) 
•Display Templates execute VERY early in the page lifecycle, so you’ll have to be creative if you want to leverage CSOM or AJAX 
Display Templates 101
varfieldOverride= {}; 
fieldOverride.Templates= {}; 
fieldOverride.Templates.Fields= { 
“InternalFieldName”: { 
“NewForm”: myNewForm, 
“EditForm”: myEditForm, 
“DisplayForm”:myDisplayMethod, 
“View”: myViewMethod 
} 
}; 
// Optional: Restrict this template the “Custom List” template 
fieldOverride.ListTemplateType= 100; 
// register our field override with SharePoint 
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldOverride); 
A basic Display Template 
Rinse-and-Repeat to override multiple fields 
This line tells SharePoint about our template
•Each render method receives a “context” object which allows you to pull out some key information: 
•CurrentFieldValue 
•CurrentFieldSchema 
•CurrentItem* 
* CurrentItemalso contains built-in variables for each field that the item has, so you can use for example “CurrentItem.Title” 
Custom Fields: Views and Display Forms
functionRenderFieldValue(ctx) { // place in a div to make sure the description field (if present) // wraps to the next linereturn"<div>"+ ctx.CurrentFieldValue+ "</div>"; }; 
•End of the day it is just a method which returns some HTML 
•Be careful of Taxonomy / Lookup fields as it will include the raw value and not “clean” text (e.g. “1#;Lookup Item A”) 
Custom Fields: Basic Display Form
DEMO 
Custom Fields: Views and Display Forms 
.....
•These are the same as the Display Form, except we now need to provide SharePoint with the field value on save 
•We can do this using a RegisterCallbacksmethod, passing it the method which will return the field value (as a string). 
•How you retrieve and format the value is up to you, but don’t forget to read in the current item value when the edit form loads! 
•Be aware that complex data types (Lookups / Taxonomy Fields) will expect the data in a specific format! 
Custom Fields: New and EditForms
•You can register Validators for ANY field (even if you aren’t providing a display template for rendering) 
•Standard validation (e.g. “Mandatory / Required Field”) will be implemented automatically, so you only need to add your own. 
•When you register your validator you need to provide 3 things: 
•An object which contains a “Validate” method (returning true or false) 
•A callbackmethod for when the field does not validate. 
•The field you want to bind the validator to. 
Custom Fields: Validating User Input
DEMO 
Custom Fields: Editing and Validating 
.....
•The “Display Template” rendering occurs very early in the page load cycle, which has a few knock-on affects: 
•The JavaScript CSOM doesn’t work as “ClientContext” is null 
•Script on Demand methods “RegisterSod” and “LoadSodByKey” need to be called before your override template or it won’t work 
•Attempting to slow this process down will break your template: 
•$(“document”).ready(function() { }); 
•ExecuteOrDelayUntilScriptLoaded(function() { }, “scriptkey”); 
Its all about the timing …
•Simple Rules: 
•Instead of CSOM, use an AJAX request with the REST API which has no “page state” dependencies 
•Instead of using “RegisterScript” roll your own 
•Return the bare minimum HTML 
•Create a separate asyncfunction to do all your processing (which you can fire off once the page is finished rendering) 
… working around your limitations
•SharePoint contains a Script RESX Handlerwhich will automatically generate a JavaScript object containing the resource strings for the current UI culture 
_layouts15ScriptResx.ashx 
?name=MyResourcefile 
&culture=en-US 
•_spPageContextInfo.currentUICultureNameis generated by SharePoint automatically generates 
Multiple Language Support
•“resheader” attribute needs to be added manually (the SharePoint ones already have this value) 
<resheadername="classFullName"> 
<value>My Namespace</value> 
</resheader> 
•Resource file must be in the 15Resources folder. 
•This technique does NOT work in Office 365. As an alternative, store a list of string variables in a JS files using the UI Culture as the filename and dynamically load them 
RESX file .. Some assembly required
DEMO 
JavaScript Multi-Language Support 
.....
JSLink–The magic plumbing 
•A mechanism which allows you to “attach” JavaScript to different SharePoint elements 
•Any JavaScript specified is automatically added to the page when that “element” is rendered 
•Tokens available to create SharePoint contextual URLs: 
•~layouts 
•~sitecollectionlayouts 
•~sitecollection 
•~site
JSLink 
•You can include multiple JavaScript files by separating them with the “pipe” | symbol. These will be loaded left-to-right 
•~layouts/firstFile.js| ~layouts/secondFile.js 
•SharePoint will automatically ensure that the same file is not loaded in twice (so you don’t need to worry about duplication!)
JSLink–The good and the .. challenges 
•You can leverage JSLinkfrom different places, but these all have different challenges: 
Location 
Good 
Challenge 
Fields 
•Site Columns 
•List Fields (schema.xml) 
Easyto distribute platform wide 
If you want different behaviour on differentlists then you’ll need to manually modify in the schema.xml 
(e.g. standard lookup on one, cascading lookup on another?) 
Content Types 
Easy to distribute platform wide 
Will not firein List Views (no .. I don’t know why either) 
List View Web Part 
Easy to add through the browser 
What about people who create their own views? 
ListForm Web Part 
Easy to add through the browser 
What about creating list templates?
•Your Display Templates can be placed anywhere, at the end of the day it is just some JavaScript 
•If you are deploying WSP Packages, make sure your JSLinkfiles are deployed in the same package as your Fields / Content Types / List Schemas 
•Be aware of cross-site scripting protection, recommended to deploy within the site collection (i.e. instead of Azure CDN) 
Deployment
Martin Hatch 
@MartinHatch 
http://www.martinhatch.com 
martin.hatch@hatchsolutions.co.uk 
Thank You
Spca2014 js link and display templates hatch

More Related Content

PPTX
Introduction to using jQuery with SharePoint
PPTX
SPTechCon - Share point and jquery essentials
PPTX
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
PPTX
SEF2013 - A jQuery Primer for SharePoint
PPTX
Ajax presentation
PPTX
SPSDenver - SharePoint & jQuery - What I wish I would have known
PPTX
Content by query web part
PPTX
Share point saturday presentation 9 29-2012-2
Introduction to using jQuery with SharePoint
SPTechCon - Share point and jquery essentials
2/15/2012 - Wrapping Your Head Around the SharePoint Beast
SEF2013 - A jQuery Primer for SharePoint
Ajax presentation
SPSDenver - SharePoint & jQuery - What I wish I would have known
Content by query web part
Share point saturday presentation 9 29-2012-2

What's hot (20)

PPTX
SharePoint Saturday St. Louis - SharePoint & jQuery
PPTX
Introduction to ajax
PPTX
Ajax:From Desktop Applications towards Ajax Web Applications
PDF
Introduction to ajax
PPTX
SilverStripe From a Developer's Perspective
PPT
Using Ajax In Domino Web Applications
PPTX
SharePoint 2010 Client-side Object Model
PPT
Ajax Presentation
PPT
Introduction to ajax
PPTX
Building Your First Store App with XAML and C#
PDF
Getting Started with Rails
PPTX
Overview of ASP.Net by software outsourcing company india
PPTX
O365 Saturday - Deepdive SharePoint Client Side Rendering
SharePoint Saturday St. Louis - SharePoint & jQuery
Introduction to ajax
Ajax:From Desktop Applications towards Ajax Web Applications
Introduction to ajax
SilverStripe From a Developer's Perspective
Using Ajax In Domino Web Applications
SharePoint 2010 Client-side Object Model
Ajax Presentation
Introduction to ajax
Building Your First Store App with XAML and C#
Getting Started with Rails
Overview of ASP.Net by software outsourcing company india
O365 Saturday - Deepdive SharePoint Client Side Rendering
Ad

Viewers also liked (7)

PDF
Spca2014 cool dashboards for power users niaulin
PDF
Spca2014 hillier 3rd party_javascript_libraries
PPTX
SPCA2013 - SharePoint 2013 in a Hybrid World
PPTX
SPCA2013 - Real-life building public-facing websites with SharePoint 2013
PPTX
SPCA2013 - Dude, Where’s my Search Scopes
PDF
Spca2014 hillier workshop 01
PDF
SPCA2013 - Taking Office Beyond the Client with Office Web Apps 2013
Spca2014 cool dashboards for power users niaulin
Spca2014 hillier 3rd party_javascript_libraries
SPCA2013 - SharePoint 2013 in a Hybrid World
SPCA2013 - Real-life building public-facing websites with SharePoint 2013
SPCA2013 - Dude, Where’s my Search Scopes
Spca2014 hillier workshop 01
SPCA2013 - Taking Office Beyond the Client with Office Web Apps 2013
Ad

Similar to Spca2014 js link and display templates hatch (20)

PDF
SPSSTHLM - Using JSLink and Display Templates for ITPros
PPTX
Popping the Hood: How to Create Custom SharePoint Branding by Randy Drisgill ...
PPT
Ajax workshop
PPTX
ASP.NET MVC 5 - EF 6 - VS2015
PDF
Web Development with Delphi and React - ITDevCon 2016
PPTX
SharePoint Saturday Belgium 2014 - Using JSLink and Display Templates with th...
PDF
SharePoint Saturday The Conference DC - How the client object model saved the...
PPTX
Spsbe using js-linkanddisplaytemplates
PPTX
#SPSLondon - Session 2 JSLink for IT Pros
PPTX
SharePoint and the User Interface with JavaScript
PPTX
web development
PPTX
WEB DEVELOPMENT.pptx
PPTX
025444215.pptx
PPTX
Training presentation
PDF
Intro JavaScript
PPT
Java script
PPT
Java script
PPT
Project First presentation about introduction to technologies to be used
PPTX
Office 365 Saturday (Sydney) - SharePoint framework – build integrated user e...
PPTX
A Beginner's Guide to Client Side Development with Javascript
SPSSTHLM - Using JSLink and Display Templates for ITPros
Popping the Hood: How to Create Custom SharePoint Branding by Randy Drisgill ...
Ajax workshop
ASP.NET MVC 5 - EF 6 - VS2015
Web Development with Delphi and React - ITDevCon 2016
SharePoint Saturday Belgium 2014 - Using JSLink and Display Templates with th...
SharePoint Saturday The Conference DC - How the client object model saved the...
Spsbe using js-linkanddisplaytemplates
#SPSLondon - Session 2 JSLink for IT Pros
SharePoint and the User Interface with JavaScript
web development
WEB DEVELOPMENT.pptx
025444215.pptx
Training presentation
Intro JavaScript
Java script
Java script
Project First presentation about introduction to technologies to be used
Office 365 Saturday (Sydney) - SharePoint framework – build integrated user e...
A Beginner's Guide to Client Side Development with Javascript

More from NCCOMMS (20)

PDF
O365Con19 - UI:UX 101 Learn How to Design Custom Experiences for SharePoint -...
PDF
O365Con19 - Model-driven Apps or Canvas Apps? - Rick Bakker
PDF
O365Con19 - Office 365 Groups Surviving the Real World - Jasper Oosterveld
PDF
O365Con19 - Developing Timerjob and Eventhandler Equivalents - Adis Jugo
PDF
O365Con19 - Sharepoint with (Artificial) Intelligence - Adis Jugo
PDF
O365Con19 - What Do You Mean 90 days Isn't Enough - Paul Hunt
PDF
O365Con19 - Tips and Tricks for Complex Migrations to SharePoint Online - And...
PDF
O365Con19 - Start Developing Teams Tabs and SharePoint Webparts with SPFX - O...
PDF
O365Con19 - Start Your Journey from Skype for Business to Teams - Sasja Beere...
PDF
O365Con19 - Lets Get Started with Azure Container Instances - Jussi Roine
PDF
O365Con19 - Azure Blackbelt - Jussi Roine
PDF
O365Con19 - Customise the UI in Modern SharePoint Workspaces - Corinna Lins
PDF
O365Con19 - Be The Protagonist of Your Modern Workplace - Corinna Lins
PDF
O365Con19 - How to Really Manage all your Tasks Across Microsoft 365 - Luise ...
PDF
O365Con19 - Sharing Code Efficiently in your Organisation - Elio Struyf
PDF
O365Con19 - Things I've Learned While Building a Product on SharePoint Modern...
PDF
O365Con19 - Keep Control of Your Data with AIP and CA - Bram de Jager
PDF
O365Con19 - Kaizala a Dive Into the Unknown - Rick van Rousselt
PDF
O365Con19 - How to Inspire Users to Unstick from Email - Luise Freese
PDF
O365Con19 - O365 Identity Management and The Golden Config - Chris Goosen
O365Con19 - UI:UX 101 Learn How to Design Custom Experiences for SharePoint -...
O365Con19 - Model-driven Apps or Canvas Apps? - Rick Bakker
O365Con19 - Office 365 Groups Surviving the Real World - Jasper Oosterveld
O365Con19 - Developing Timerjob and Eventhandler Equivalents - Adis Jugo
O365Con19 - Sharepoint with (Artificial) Intelligence - Adis Jugo
O365Con19 - What Do You Mean 90 days Isn't Enough - Paul Hunt
O365Con19 - Tips and Tricks for Complex Migrations to SharePoint Online - And...
O365Con19 - Start Developing Teams Tabs and SharePoint Webparts with SPFX - O...
O365Con19 - Start Your Journey from Skype for Business to Teams - Sasja Beere...
O365Con19 - Lets Get Started with Azure Container Instances - Jussi Roine
O365Con19 - Azure Blackbelt - Jussi Roine
O365Con19 - Customise the UI in Modern SharePoint Workspaces - Corinna Lins
O365Con19 - Be The Protagonist of Your Modern Workplace - Corinna Lins
O365Con19 - How to Really Manage all your Tasks Across Microsoft 365 - Luise ...
O365Con19 - Sharing Code Efficiently in your Organisation - Elio Struyf
O365Con19 - Things I've Learned While Building a Product on SharePoint Modern...
O365Con19 - Keep Control of Your Data with AIP and CA - Bram de Jager
O365Con19 - Kaizala a Dive Into the Unknown - Rick van Rousselt
O365Con19 - How to Inspire Users to Unstick from Email - Luise Freese
O365Con19 - O365 Identity Management and The Golden Config - Chris Goosen

Spca2014 js link and display templates hatch

  • 2. Martin Hatch SharePoint Practice Lead @ Ballard Chalmers, UK Advanced List Rendering with JSLinkand Display Templates @MartinHatch https://martinhatch.com martin.hatch@hatchsolutions.co.uk Slide Deck: http://mhat.ch/SPConnect14 Code: http://mhat.ch/JSLinkCode
  • 3. •Custom Fields •Display Forms and Views •New and Edit Forms •Validation •Advanced Techniques (SOD / AJAX / JavaScript CSOM) •Custom Views •Headers & Footers •Item Templates •Gotchas (Quick Edit / Paging) •Multiple Language Support (RESX files in JavaScript!) •Leveraging JSLink •Deploying with Visual Studio Agenda
  • 4. •Just some JavaScript which needs to be loaded on the page (the Script Editor Web Part is a nice way to do simple testing) •It essentially just returns some HTML as a string value •List Display Templates can be targeted to a specific List Template Type (e.g. 100 = Custom List, 101 = Document Library) •Display Templates execute VERY early in the page lifecycle, so you’ll have to be creative if you want to leverage CSOM or AJAX Display Templates 101
  • 5. varfieldOverride= {}; fieldOverride.Templates= {}; fieldOverride.Templates.Fields= { “InternalFieldName”: { “NewForm”: myNewForm, “EditForm”: myEditForm, “DisplayForm”:myDisplayMethod, “View”: myViewMethod } }; // Optional: Restrict this template the “Custom List” template fieldOverride.ListTemplateType= 100; // register our field override with SharePoint SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldOverride); A basic Display Template Rinse-and-Repeat to override multiple fields This line tells SharePoint about our template
  • 6. •Each render method receives a “context” object which allows you to pull out some key information: •CurrentFieldValue •CurrentFieldSchema •CurrentItem* * CurrentItemalso contains built-in variables for each field that the item has, so you can use for example “CurrentItem.Title” Custom Fields: Views and Display Forms
  • 7. functionRenderFieldValue(ctx) { // place in a div to make sure the description field (if present) // wraps to the next linereturn"<div>"+ ctx.CurrentFieldValue+ "</div>"; }; •End of the day it is just a method which returns some HTML •Be careful of Taxonomy / Lookup fields as it will include the raw value and not “clean” text (e.g. “1#;Lookup Item A”) Custom Fields: Basic Display Form
  • 8. DEMO Custom Fields: Views and Display Forms .....
  • 9. •These are the same as the Display Form, except we now need to provide SharePoint with the field value on save •We can do this using a RegisterCallbacksmethod, passing it the method which will return the field value (as a string). •How you retrieve and format the value is up to you, but don’t forget to read in the current item value when the edit form loads! •Be aware that complex data types (Lookups / Taxonomy Fields) will expect the data in a specific format! Custom Fields: New and EditForms
  • 10. •You can register Validators for ANY field (even if you aren’t providing a display template for rendering) •Standard validation (e.g. “Mandatory / Required Field”) will be implemented automatically, so you only need to add your own. •When you register your validator you need to provide 3 things: •An object which contains a “Validate” method (returning true or false) •A callbackmethod for when the field does not validate. •The field you want to bind the validator to. Custom Fields: Validating User Input
  • 11. DEMO Custom Fields: Editing and Validating .....
  • 12. •The “Display Template” rendering occurs very early in the page load cycle, which has a few knock-on affects: •The JavaScript CSOM doesn’t work as “ClientContext” is null •Script on Demand methods “RegisterSod” and “LoadSodByKey” need to be called before your override template or it won’t work •Attempting to slow this process down will break your template: •$(“document”).ready(function() { }); •ExecuteOrDelayUntilScriptLoaded(function() { }, “scriptkey”); Its all about the timing …
  • 13. •Simple Rules: •Instead of CSOM, use an AJAX request with the REST API which has no “page state” dependencies •Instead of using “RegisterScript” roll your own •Return the bare minimum HTML •Create a separate asyncfunction to do all your processing (which you can fire off once the page is finished rendering) … working around your limitations
  • 14. •SharePoint contains a Script RESX Handlerwhich will automatically generate a JavaScript object containing the resource strings for the current UI culture _layouts15ScriptResx.ashx ?name=MyResourcefile &culture=en-US •_spPageContextInfo.currentUICultureNameis generated by SharePoint automatically generates Multiple Language Support
  • 15. •“resheader” attribute needs to be added manually (the SharePoint ones already have this value) <resheadername="classFullName"> <value>My Namespace</value> </resheader> •Resource file must be in the 15Resources folder. •This technique does NOT work in Office 365. As an alternative, store a list of string variables in a JS files using the UI Culture as the filename and dynamically load them RESX file .. Some assembly required
  • 17. JSLink–The magic plumbing •A mechanism which allows you to “attach” JavaScript to different SharePoint elements •Any JavaScript specified is automatically added to the page when that “element” is rendered •Tokens available to create SharePoint contextual URLs: •~layouts •~sitecollectionlayouts •~sitecollection •~site
  • 18. JSLink •You can include multiple JavaScript files by separating them with the “pipe” | symbol. These will be loaded left-to-right •~layouts/firstFile.js| ~layouts/secondFile.js •SharePoint will automatically ensure that the same file is not loaded in twice (so you don’t need to worry about duplication!)
  • 19. JSLink–The good and the .. challenges •You can leverage JSLinkfrom different places, but these all have different challenges: Location Good Challenge Fields •Site Columns •List Fields (schema.xml) Easyto distribute platform wide If you want different behaviour on differentlists then you’ll need to manually modify in the schema.xml (e.g. standard lookup on one, cascading lookup on another?) Content Types Easy to distribute platform wide Will not firein List Views (no .. I don’t know why either) List View Web Part Easy to add through the browser What about people who create their own views? ListForm Web Part Easy to add through the browser What about creating list templates?
  • 20. •Your Display Templates can be placed anywhere, at the end of the day it is just some JavaScript •If you are deploying WSP Packages, make sure your JSLinkfiles are deployed in the same package as your Fields / Content Types / List Schemas •Be aware of cross-site scripting protection, recommended to deploy within the site collection (i.e. instead of Azure CDN) Deployment
  • 21. Martin Hatch @MartinHatch http://www.martinhatch.com martin.hatch@hatchsolutions.co.uk Thank You