%3CLINGO-SUB%20id%3D%22lingo-sub-509770%22%20slang%3D%22en-US%22%3EHOW%20TO%3A%20Add%20a%20custom%20field%20to%20blog%20posts%20in%20SharePoint%202013%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509770%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Apr%2009%2C%202013%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThis%20blog%20post%20is%20a%20contribution%20from%20David%20Wilborn%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20recently%20worked%20a%20project%20where%20we%20needed%20to%20add%20a%20custom%20field%20to%20blog%20posts%20and%20use%20the%20new%20Client%20Side%20Rendering%20in%20SharePoint%202013%20to%20display%20the%20field.%20My%20first%20thought%20was%20to%20create%20a%20new%20custom%20template%20to%20render%20the%20entire%20post%20item.%20This%20would%20allow%20us%20to%20place%20the%20new%20field%20wherever%20we%20liked.%20While%20this%20may%20be%20the%20logical%20choice%20in%20some%20cases%2C%20it%20practice%20it%20takes%20a%20lot%20of%20work%20if%20you%20want%20your%20blog%20post%20to%20look%20like%20the%20out-of-the-box%20version.%20There%20is%20a%20lot%20of%20customization%20and%20custom%20logic%20in%20the%20blog%20post%20rendering.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20simpler%20option%20is%20to%20set%20up%20your%20custom%20field%20to%20be%20rendered%20along%20with%20another%20field.%20This%20allows%20you%20to%20keep%20the%20default%20look%20of%20the%20post%20without%20having%20to%20replicate%20all%20of%20the%20existing%20layout%20and%20logic%20yourself.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3E%20Note%3A%20Custom%20Client-Side%20Rendering%20scripts%20can%20sometimes%20have%20issues%20with%20the%20Minimal%20Download%20Strategy%20feature.%20See%20the%20following%20blog%20post%20for%20more%20information%3A%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Fsridhara%2Farchive%2F2013%2F02%2F08%2Fregister-csr-override-on-mds-enabled-sharepoint-2013-site.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20http%3A%2F%2Fblogs.msdn.com%2Fb%2Fsridhara%2Farchive%2F2013%2F02%2F08%2Fregister-csr-override-on-mds-enabled-sharepoint-2013-site.aspx%20%3C%2FA%3E%20%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20my%20example%2C%20I%20will%20be%20adding%20a%20%E2%80%9CSubtitle%E2%80%9D%20field%20that%20I%20want%20to%20be%20displayed%20above%20the%20main%20blog%20post%20Body%20text.%20The%20first%20step%20is%20to%20add%20the%20Subtitle%20field%20to%20the%20Post%20list%20for%20my%20blog%20site.%20I%20can%20do%20this%20through%20the%20GUI%20by%20going%20to%20Settings%20-%26gt%3B%20Site%20%3CBR%20%2F%3EContent%20-%26gt%3B%20Posts.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20case%20I%20am%20adding%20a%20simple%20text%20field%20named%20%E2%80%9CSubtitle.%E2%80%9D%26nbsp%3B%20Select%20the%20%E2%80%9CLIST%E2%80%9D%20tab%20in%20the%20upper%20right%20corner%2C%20and%20go%20to%20%E2%80%9CList%20Settings%E2%80%9D%20on%20the%20ribbon%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20962px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111461i1618DDF077998BA2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EToward%20the%20bottom%20of%20the%20page%20above%20the%20%E2%80%9CViews%E2%80%9D%20section%2C%20click%20%E2%80%9CCreate%20column.%E2%80%9D%20Enter%20%E2%80%9CSubtitle%E2%80%9D%20for%20the%20column%20name.%20You%20can%20leave%20the%20remainder%20of%20the%20settings%20at%20their%20defaults%2C%20and%20click%20%E2%80%9COK.%E2%80%9D%20Be%20sure%20to%20enter%20some%20values%20into%20the%20Subtitle%20fields%20in%20your%20Posts%20list%20so%20that%20we%20can%20see%20them%20once%20we%E2%80%99ve%20modified%20the%20template.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20next%20step%20is%20to%20add%20my%20new%20field%20to%20the%20current%20view%20that%20displays%20the%20blog%20post.%20I%20navigate%20to%20the%20main%20page%20of%20my%20blog%20site%20and%20edit%20the%20page%20(Page%20tab%2C%20select%20%E2%80%9CEdit%20Page%E2%80%9D%20from%20the%20ribbon).%20Next%2C%20I%20edit%20the%20%E2%80%9CPosts%E2%80%9D%20web%20part%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20635px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111462iF342468EAB51489A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20%E2%80%9CList%20Views%E2%80%9D%20section%2C%20click%20%E2%80%9CEdit%20the%20current%20view%E2%80%9D%20(underneath%20%E2%80%9CSelected%20View%E2%80%9D)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20256px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111463iAABBCEE86B7F72F0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20select%20the%20checkbox%20for%20the%20field%20I%20created%20and%20click%20%E2%80%9COK%E2%80%9D%20to%20add%20it%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20380px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111464i5B1AD35218C4CF62%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20field%20is%20now%20available%20to%20be%20rendered%20as%20part%20of%20the%20current%20view.%20Note%20that%20this%20custom%20view%20is%20saved%20as%20part%20of%20the%20web%20part%20page%2C%20and%20is%20not%20saved%20to%20the%20views%20that%20are%20part%20of%20the%20Posts%20list%20itself.%20Next%2C%20I%20create%20the%20custom%20JavaScript%20file%20that%20will%20render%20the%20new%20field.%20I%E2%80%99ll%20be%20adding%20the%20%E2%80%9CSubtitle%E2%80%9D%20rendering%20to%20the%20renderer%20for%20the%20post%20Body%2C%20since%20I%20am%20displaying%20the%20Subtitle%20over%20the%20body.%20The%20first%20part%20of%20the%20code%20does%20the%20work%20to%20associate%20our%20custom%20rendering%20code%20with%20the%20Body%20field.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3E%20Note%20the%20%E2%80%9C(function()%20%7B%20%E2%80%A6%E2%80%9D%20syntax%20which%20causes%20the%20JavaScript%20to%20be%20executed%20immediately.%20This%20code%20creates%20a%20template%20for%20the%20Body%20field%20when%20it%20is%20displayed%20in%20a%20View%20form%2C%20and%20associates%20it%20with%20our%20custom%20rendering%20function%2C%20named%20CBody.%20The%20template%20is%20then%20registered%20with%20the%20Template%20Manager.%20%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E(function%20()%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Evar%20overrideCtx%20%3D%20%7B%7D%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EoverrideCtx.Templates%20%3D%20%7B%7D%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EoverrideCtx.Templates.Fields%20%3D%20%7B'Body'%3A%7B'View'%3ACBody%7D%7D%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx)%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%7D)()%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20we%E2%80%99ll%20implement%20the%20%E2%80%9CCBody%E2%80%9D%20function%2C%20which%20is%20our%20custom%20client-side%20rendering%20function.%20This%20is%20simply%20JavaScript%20code%20that%20outputs%20the%20HTML%20we%20want%20displayed%20when%20the%20field%20is%20rendered%20by%20the%20web%20part.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Efunction%20CBody(ctx)%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Evar%20ret%20%3D%20%22%3CB%3E%22%20%2B%20ctx.CurrentItem.Subtitle%20%2B%20%22%3C%2FB%3E%3C%2FP%3E%3CHR%20%2F%3E%22%20%2B%20ctx.CurrentItem.Body%3B%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ereturn%20ret%3B%20%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20%3CSTRONG%3E%20ctx%20%3C%2FSTRONG%3E%20parameter%20passed%20in%20to%20our%20custom%20function%20gives%20us%20access%20to%20the%20current%20item%20and%20its%20fields.%20More%20on%20this%20shortly.%20You%20can%20see%20that%20the%20code%20renders%20the%20Subtitle%20field%20in%20bold%2C%20followed%20by%20a%20horizontal%20rule%2C%20and%20then%20the%20Body%20field.%26nbsp%3B%26nbsp%3BSo%20our%20final%20JavaScript%20file%20contains%20both%20of%20these%20functions%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E(function%20()%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Evar%20overrideCtx%20%3D%20%7B%7D%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EoverrideCtx.Templates%20%3D%20%7B%7D%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EoverrideCtx.Templates.Fields%20%3D%20%7B'Body'%3A%7B'View'%3A%20CBody%7D%7D%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx)%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%7D)()%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Efunction%20CBody(ctx)%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Evar%20ret%20%3D%20%22%3CB%3E%22%20%2B%20ctx.CurrentItem.Subtitle%20%2B%20%22%3C%2FB%3E%3C%2FP%3E%3CHR%20%2F%3E%22%20%2B%20ctx.CurrentItem.Body%3B%3CP%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ereturn%20ret%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESave%20the%20file%20as%20formatblogpost.js%20in%20your%20%3CSTRONG%3E%20C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C15%5CTEMPLATE%5CLAYOUTS%20%3C%2FSTRONG%3E%20directory%20(or%20equivalent%20location%20in%20your%2015%20hive).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20final%20step%20is%20to%20tell%20your%20Posts%20web%20part%20to%20use%20the%20custom%20JavaScript%20code.%20Edit%20your%20blog%20page%2C%20and%20edit%20the%20Posts%20web%20part.%20In%20the%20Miscellaneous%20section%20at%20the%20bottom%20of%20the%20web%20part%2C%20specify%20%E2%80%9Cformatblogpost.js%E2%80%9D%20(without%20the%20quotes)%20in%20the%20JS%20Link%20field%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20256px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111465iF269E59DD255F321%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3ESave%20your%20web%20part%20and%20stop%20editing%20the%20page.%20Do%20an%20IISRESET%20to%20ensure%20that%20the%20new%20template%20is%20used.%20If%20everything%20has%20worked%20correctly%2C%20you%20should%20see%20your%20custom%20field%20rendering%20in%20your%20blog%20posts%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20571px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111466i5FF1EA1325831809%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet%E2%80%99s%20take%20a%20closer%20look%20at%20the%20available%20fields%20using%20the%20JavaScript%20debugger%3A%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3EWith%20your%20blog%20page%20loaded%20in%20Internet%20Explorer%2C%20press%20F12%20to%20launch%20the%20Developer%20Tools.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3EClick%20the%20Script%20tab%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3ESelect%20your%20JavaScript%20file%20from%20the%20list%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3EPlace%20a%20breakpoint%20in%20the%20CBody%20function%20by%20clicking%20to%20the%20left%20of%20the%20first%20line%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20662px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111467iCD23145FFED4EF4B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3EClick%20the%20%E2%80%9CStart%20Debugging%E2%80%9D%20button%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CUL%3E%0A%3CLI%3ERefresh%20your%20page%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EWhen%20you%20hit%20the%20breakpoint%2C%20click%20the%20%E2%80%9CWatch%E2%80%9D%20tab%20in%20the%20right%20plane.%20Click%20on%20the%20%3CEM%3E%20Click%20to%20add%E2%80%A6%20%3C%2FEM%3E%20text%20and%20type%20%E2%80%9Cctx.CurrentItem%E2%80%9D%20in%20the%20line.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EExpand%20the%20ctx.CurrentItem%20node%20by%20clicking%20the%20plus%20sign%20to%20the%20left%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20492px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111468iE3123E136B52B9D7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20see%20the%20fields%20that%20are%20available%20to%20your%20JavaScript%20code%2C%20including%20the%20new%20%E2%80%9CSubtitle%E2%80%9D%20field%20that%20we%20created.%20Since%20the%20displayed%20field%20names%20can%20vary%20from%20the%20internal%20names%2C%20the%20JavaScript%20debugger%20can%20be%20invaluable%20for%20debugging%20any%20issues%20you%20have%20with%20Client%20Side%20Rendering.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509770%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20TECHNET%20on%20Apr%2009%2C%202013%20This%20blog%20post%20is%20a%20contribution%20from%20David%20Wilborn%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-509770%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

First published on TECHNET on Apr 09, 2013

This blog post is a contribution from David Wilborn, an engineer with the SharePoint Developer Support team.

 

I recently worked a project where we needed to add a custom field to blog posts and use the new Client Side Rendering in SharePoint 2013 to display the field. My first thought was to create a new custom template to render the entire post item. This would allow us to place the new field wherever we liked. While this may be the logical choice in some cases, it practice it takes a lot of work if you want your blog post to look like the out-of-the-box version. There is a lot of customization and custom logic in the blog post rendering.

 

A simpler option is to set up your custom field to be rendered along with another field. This allows you to keep the default look of the post without having to replicate all of the existing layout and logic yourself.

 

Note: Custom Client-Side Rendering scripts can sometimes have issues with the Minimal Download Strategy feature. See the following blog post for more information: http://blogs.msdn.com/b/sridhara/archive/2013/02/08/register-csr-override-on-mds-enabled-sharepoint...

 

In my example, I will be adding a “Subtitle” field that I want to be displayed above the main blog post Body text. The first step is to add the Subtitle field to the Post list for my blog site. I can do this through the GUI by going to Settings -> Site
Content -> Posts.

 

In this case I am adding a simple text field named “Subtitle.”  Select the “LIST” tab in the upper right corner, and go to “List Settings” on the ribbon:

 

 

Toward the bottom of the page above the “Views” section, click “Create column.” Enter “Subtitle” for the column name. You can leave the remainder of the settings at their defaults, and click “OK.” Be sure to enter some values into the Subtitle fields in your Posts list so that we can see them once we’ve modified the template.

 

The next step is to add my new field to the current view that displays the blog post. I navigate to the main page of my blog site and edit the page (Page tab, select “Edit Page” from the ribbon). Next, I edit the “Posts” web part:

 

 

In the “List Views” section, click “Edit the current view” (underneath “Selected View”):

 

 

 

 

I select the checkbox for the field I created and click “OK” to add it:

 

 

 

 

The field is now available to be rendered as part of the current view. Note that this custom view is saved as part of the web part page, and is not saved to the views that are part of the Posts list itself. Next, I create the custom JavaScript file that will render the new field. I’ll be adding the “Subtitle” rendering to the renderer for the post Body, since I am displaying the Subtitle over the body. The first part of the code does the work to associate our custom rendering code with the Body field.

 

Note the “(function() { …” syntax which causes the JavaScript to be executed immediately. This code creates a template for the Body field when it is displayed in a View form, and associates it with our custom rendering function, named CBody. The template is then registered with the Template Manager.

 

(function () {

 

var overrideCtx = {};

 

overrideCtx.Templates = {};

 

overrideCtx.Templates.Fields = {'Body':{'View':CBody}};

 

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);

 

})();

 

Next we’ll implement the “CBody” function, which is our custom client-side rendering function. This is simply JavaScript code that outputs the HTML we want displayed when the field is rendered by the web part.

 

function CBody(ctx) {

 

var ret = "<b>" + ctx.CurrentItem.Subtitle + "</b><hr/>" + ctx.CurrentItem.Body;

 

return ret; }

 

The ctx parameter passed in to our custom function gives us access to the current item and its fields. More on this shortly. You can see that the code renders the Subtitle field in bold, followed by a horizontal rule, and then the Body field.  So our final JavaScript file contains both of these functions:

 

(function () {

 

var overrideCtx = {};

 

overrideCtx.Templates = {};

 

overrideCtx.Templates.Fields = {'Body':{'View': CBody}};

 

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);

 

})();

 

function CBody(ctx) {

 

var ret = "<b>" + ctx.CurrentItem.Subtitle + "</b><hr/>" + ctx.CurrentItem.Body;

 

return ret;

 

}

 

Save the file as formatblogpost.js in your C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS directory (or equivalent location in your 15 hive).

 

The final step is to tell your Posts web part to use the custom JavaScript code. Edit your blog page, and edit the Posts web part. In the Miscellaneous section at the bottom of the web part, specify “formatblogpost.js” (without the quotes) in the JS Link field:

 




Save your web part and stop editing the page. Do an IISRESET to ensure that the new template is used. If everything has worked correctly, you should see your custom field rendering in your blog posts:

 

 

Let’s take a closer look at the available fields using the JavaScript debugger:



    • With your blog page loaded in Internet Explorer, press F12 to launch the Developer Tools.

 

    • Click the Script tab

 

    • Select your JavaScript file from the list

 

    • Place a breakpoint in the CBody function by clicking to the left of the first line:





    • Click the “Start Debugging” button

 

    • Refresh your page



When you hit the breakpoint, click the “Watch” tab in the right plane. Click on the Click to add… text and type “ctx.CurrentItem” in the line.

 

Expand the ctx.CurrentItem node by clicking the plus sign to the left:

 

 

You can see the fields that are available to your JavaScript code, including the new “Subtitle” field that we created. Since the displayed field names can vary from the internal names, the JavaScript debugger can be invaluable for debugging any issues you have with Client Side Rendering.