400 likes | 594 Views
Chris O’Brien - MVP. Customizing the SharePoint 2013 UI with JavaScript. About me. Independent Consultant Head of Development, Content and Code (UK) Blog: www.sharepointnutsandbolts.com Twitter: @ ChrisO_Brien LinkedIn: http://uk.linkedin.com/in/ chrisobrienmvp. Long Ago….
E N D
Chris O’Brien - MVP Customizing the SharePoint 2013 UI with JavaScript
About me • Independent Consultant • Head of Development, Content and Code (UK) • Blog: www.sharepointnutsandbolts.com • Twitter: @ChrisO_Brien • LinkedIn: http://uk.linkedin.com/in/chrisobrienmvp
Long Ago… SharePoint 2007 CAML SharePoint 2010 XSL
…Today SharePoint 2010 XSL SharePoint 2013 CSR
Terminology • Lists • Fields • Web parts etc.. • Content Search web part • Search hover panel • List callouts
JSLink • Used to transform: • Lists • Views • Fields • Web Parts • Content types
CSR/JSLink – template scope • Use SPView.JSLink = ‘~site/foo/MyFile.js’, but also need to register templates • Register by: • BaseViewID (e.g. 1) • ListTemplateType (e.g. 10001) • ControlMode (e.g. ‘Display’)
Registering templates/running code • The RenderContext JS object: • Templates collection • e.g. header, item, footer • Register with BaseViewID, ListTemplateType, ControlMode • OnPreRender • OnPostRender • ListData • ListSchema
Advanced CSR possibilities • Modify list data/schema in OnPreRender • E.g. lookup something dynamically • Use SPClientRenderer.Render() to obtain HTML: • From arbitrary data • Outside of page context • In AJAX methods
CSWP - 5 ‘easy’ requirements • “More like this” (Items matching a tag) • Recently changed items • Most popular items • “(Recent) items you may be interested in” (Matching current user’s profile) • “Items related to you” (Contributor to document)
The role of Managed Properties • CSWP has mappings to Managed Props for data:
CSWP and Display Templates These items are files in ~sitecollection/_catalogs/masterpage/Display Templates/ Content Web Parts
Diagnostic item template • Diagnostic template gives raw (ish) output • Tip: configure many props temporarily
Dynamic values in query • Several tokens available:
Hover Panel components - Item_CommonHoverPanel_Body.js - Item_[FileType]_HoverPanel.js
Hover Panel actions • Footer actions can also be • Common • Specific • Gotcha! Some item types do not use Common actions • E.g. Discussion - Item_CommonHoverPanel_Actions.js - Item_[FileType]_HoverPanel.js
Hover panel – lessons learnt • Can’t use document.ready() – DOM is not populated • Specify function in ctx.OnPostRender instead • These functions are called ONCE (on first render [mouseover]) • N.B. Array of functions is accessible in all files e.g. header, body, actions etc. • DON’T delete _#= ctx.RenderBody(ctx) =#_ • This executes Item_CommonHoverPanel_Body.js (inheritance model)
Summary • JavaScript is key to many SP2013 UI elements • Examples shown: • Customizing entire list UI • Conditional formatting of a field • Rendering of Content Search web part • Customizing search hover panel (inc. AJAX) • Lots of other possibilities too!
Script/CSS in display templates • Tools: • $includeScript • $includeLanguageScript • $includeCSS • But consider HTTP payload – may be better combining with other CSS/JS
Search display templates • Create the Display Template • Copy/rename then edit 2 files: • Item_Something.js [HTML] • Item_Something_HoverPanel.js [HTML] • Create any core search config • E.g. Managed Properties, Result Source • Create the Result Type Alternative - use search results WP properties to map to Display Template