I would like to insert some CSR (client side rending) code on every page of a site collection. Hence I added the related line in the Master page. Later I found all the publishing pages do not trigger the code. clienttemplates.js
is not loaded by default. May I know is there any reason clienttemplates.js
is not loaded? Can I simply add <SharePoint:ScriptLink>
to include the clienttemplates.js
? Thanks.
2 Answers
You can ensure clienttemplates.js
is loaded before setting up your client side rendering.
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", CSR_Setup);
function CSR_Setup() {
// Client side rendering code
}
-
I added <SharePoint:ScriptLink language="javascript" name="clienttemplates.js" OnDemand="false" runat="server" Localizable="false" /> to Master page it force load clienttemplates.js. However I think SPClientTemplates is not called so my script fail to RegisterTemplateOverrides anything. Publishing page cannot use CSR?– Mark LJun 3, 2015 at 2:53
-
You shouldn't need to add that. As far as I know, there is no limitation with CSR in publishing pages. Did you try what I suggested in your JavaScript?– SubmitsJun 3, 2015 at 10:06
-
By using F12 tool, I confirmed clienttemplates.js is not loading unless I added the ScriptLink tag. But I am not sure whether it means CSR is applying. In my script there is function [onPostRender(renderCtx)] but this function never fire in publishing pages.– Mark LJun 5, 2015 at 3:34
-
I believe SP.SOD.executeFunc is ensure clienttemplates.js is loaded. If clienttemplates.js is not loaded, the SP.SOD.executeFunc will not be triggered?– Mark LJun 5, 2015 at 6:20
-
SP.SOD.executeFunc
ensures thatclienttemplates.js
is loaded before theCSR_Setup
function is called (in my example). So you DO NOT need to use aScriptLink
.– SubmitsJun 5, 2015 at 9:20
Try to add into your MasterPage, in the end of the HEAD section, this block:
<script type="text/javascript">RegisterSod("clienttemplates.js", "/_layouts/15/clienttemplates.js");</script>
This will register SOD name clienttemplates.js
on your page. Then, in the BODY, try code suggested by Submits:
<script type="text/javascript">
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", CSR_Setup);
function CSR_Setup() {
// Client side rendering code
}
</script>
Also notice, in some cases, all content on page is loading async, so may by your CSR-code is executing before content is fully loaded? If this is your case, you can call your functions after sp.bodyloaded
event:
<script type="text/javascript">
SP.SOD.executeOrDelayUntilEventNotified(CSR_Setup, "sp.bodyloaded");
</script>