1

I have a blog site in sharepoint 2013, I wanted to use Javascript Template for displaying the Post in a custom style.
Here is how my js file looks like.

(function () {
    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.Templates.Fields = {'Body':{'View': CBody}};    
    alert("CSR Registered");    
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})(); 

function CBody(ctx) {
    var ret = ctx.CurrentItem.Body + "<b>" + 
    "<button onclick='alert("template working");'>Test Button</button>" + "</b>" ;
    return ret;
}

But When I use the file in JSLink of the Post Page (SiteCollection/sites/test/Lists/Posts/Post.aspx?ID=160)

I am unable to see the changes, the CSR is not getting registered and page is not displaying any errors.

In other Custom List I tried a similar approach, In the list I created a custom view and applied the JSLink template it worked fine however on the default AllItems View it failed to display the Custom template.

Can't we apply CSR on default views?. Help me solve this issue.

2 Answers 2

1

As long as the view supports JSLink it should work fine, for the default page too. You have an error within your CBody function. Below is the modified code:

 (function () {
   var overrideCtx = {};
   overrideCtx.Templates = {};
   overrideCtx.Templates.Fields = {'Body':{'View': CBody}};
   window.console && console.log("CSR Registered"); //Removed alert, see your console F12
   SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
 })();

 function CBody(ctx) {
    var ret = ctx.CurrentItem.Body + "<b>" +
    "<button onclick=\"alert('template working');\">Test Button</button>" + "</b>" ;
    return ret;
 }

enter image description here

2
  • Hi, I tried the above code also, it's not working for me. Similarly I have a CSR for a custom list, same JSLink file is being referred by the Default All Items view and a custom view. Custom view is able to display the custom CSR, whereas Default view is not. Do I need to turn on some feature, or turn off MDS anything I need to do.
    – Sadanand
    Commented May 6, 2014 at 5:06
  • Thanks for the help provided, as I examined in the F12 I couldn't find JSLink file and it's contents. How do I know it supports JSLink.
    – Sadanand
    Commented May 6, 2014 at 5:18
1

JSLink file will work only if you use the JSLink url in the website like this.

~sitecollection/_catalogs/masterpage/file.js

where the actual url of the file is

http://yourSPsite/sites/DevBlogs/_catalogs/masterpage/file.js

After this everything started working fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.