SlideShare une entreprise Scribd logo
1  sur  22
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Using Ajax In Domino Web Applications
Using Ajax In Domino Web ApplicationsUsing Ajax In Domino Web Applications
Using Ajax In Domino Web Applications
dominion
 
SharePoint 2010 Client-side Object Model
SharePoint 2010 Client-side Object ModelSharePoint 2010 Client-side Object Model
SharePoint 2010 Client-side Object Model
Phil Wicklund
 

Tendances (20)

SharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuerySharePoint Saturday St. Louis - SharePoint & jQuery
SharePoint Saturday St. Louis - SharePoint & jQuery
 
Ajax
AjaxAjax
Ajax
 
Introduction to ajax
Introduction  to  ajaxIntroduction  to  ajax
Introduction to ajax
 
Ajax:From Desktop Applications towards Ajax Web Applications
Ajax:From Desktop Applications towards Ajax Web ApplicationsAjax:From Desktop Applications towards Ajax Web Applications
Ajax:From Desktop Applications towards Ajax Web Applications
 
Ajax
AjaxAjax
Ajax
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Ajax
AjaxAjax
Ajax
 
SilverStripe From a Developer's Perspective
SilverStripe From a Developer's PerspectiveSilverStripe From a Developer's Perspective
SilverStripe From a Developer's Perspective
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Using Ajax In Domino Web Applications
Using Ajax In Domino Web ApplicationsUsing Ajax In Domino Web Applications
Using Ajax In Domino Web Applications
 
ASP.NET - Web Programming
ASP.NET - Web ProgrammingASP.NET - Web Programming
ASP.NET - Web Programming
 
SharePoint 2010 Client-side Object Model
SharePoint 2010 Client-side Object ModelSharePoint 2010 Client-side Object Model
SharePoint 2010 Client-side Object Model
 
Ajax Presentation
Ajax PresentationAjax Presentation
Ajax Presentation
 
Introduction to ajax
Introduction to ajaxIntroduction to ajax
Introduction to ajax
 
Building Your First Store App with XAML and C#
Building Your First Store App with XAML and C#Building Your First Store App with XAML and C#
Building Your First Store App with XAML and C#
 
Ajax.ppt
Ajax.pptAjax.ppt
Ajax.ppt
 
Getting Started with Rails
Getting Started with RailsGetting Started with Rails
Getting Started with Rails
 
Overview of ASP.Net by software outsourcing company india
Overview of ASP.Net by software outsourcing company indiaOverview of ASP.Net by software outsourcing company india
Overview of ASP.Net by software outsourcing company india
 
O365 Saturday - Deepdive SharePoint Client Side Rendering
O365 Saturday - Deepdive SharePoint Client Side RenderingO365 Saturday - Deepdive SharePoint Client Side Rendering
O365 Saturday - Deepdive SharePoint Client Side Rendering
 
A View about ASP .NET and their objectives
A View about ASP .NET and their objectivesA View about ASP .NET and their objectives
A View about ASP .NET and their objectives
 

En vedette (7)

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

Similaire à Spca2014 js link and display templates hatch

SharePoint Saturday Belgium 2014 - Using JSLink and Display Templates with th...
SharePoint Saturday Belgium 2014 - Using JSLink and Display Templates with th...SharePoint Saturday Belgium 2014 - Using JSLink and Display Templates with th...
SharePoint Saturday Belgium 2014 - Using JSLink and Display Templates with th...
BIWUG
 
SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...
Liam Cleary [MVP]
 

Similaire à Spca2014 js link and display templates hatch (20)

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

Plus de NCCOMMS

Plus de NCCOMMS (20)

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

Spca2014 js link and display templates hatch

  • 1.
  • 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