2

I have some prerender code that is being used to get information from another list to add into the display of the main list.

I am puzzled - the prerender code executes twice.

 (function () {
 var ctx = {};
 ctx.Templates = {};
 ctx.OnPreRender = preRenderHandler;
 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();

function preRenderHandler(renderCtx) { 
//        Execute SP Services to get external variables to add into current site
};

the code works fine, just executes twice.

I suspect that this site explains why:

http://geekswithblogs.net/KunaalKapoor/archive/2012/07/18/sharepoint-webpart-life-cycle-events-and-event-sequence.aspx

namely, that there is an OnLoad and Postback event happening resulting in the preRenderhandler running twice?

Is this correct?

What is the best way to stop the code running twice? Is there any benefit in it running twice?

Thanks!

4
  • 1
    Don't confuse serverside lifecycle events with the clientside events. Your ctx.OnPreRender is not related to the blogpost you mention. Where do you reference your JavaScript - maybe it's referenced twice.
    – Mx.
    Commented Oct 22, 2015 at 8:55
  • The name of the javascript code is entered in the miscellaneous settings when you edit the web part for the list. The code is not referenced twice. I am not sure what you mean by ctx.OnPreRender is not related. This is the code section that executes twice. The issue is that I do not need to run the code twice, as it is only used to set up values, so during which event should it run, and how do I stop it running in that other event?
    – southskies
    Commented Oct 29, 2015 at 1:54
  • all serverside events in your blogpost are executed before the JS you entered in the JSLink setting is executed (per Definition). Is the code referrenced twice on the page? (you can use the f12 devTools to check that)
    – Mx.
    Commented Oct 29, 2015 at 6:49
  • No, but what I have found is that I have twisties on the view as I have a Group By attribute on the view. The prerender gets called - once for the whole page and then once again for every twistie that has been opened... I only need for it to execute once for the whole page, not for every open twistie..
    – southskies
    Commented Oct 29, 2015 at 7:25

2 Answers 2

0

ist absolut logically that the prerender is executed on both onLoad and Postback. Because everytime the control is rendered new, so the prerender is also executed. It would be a Problem if it wouldn´t do so.

1
  • I am not doubting the need to have onLoad and Postback events. The question is that my code is only used to set up variables, and does not need to run twice. So which event is it better to run in, and how to I stop it running in the duplicate event. One option is that I add a variable IFRUN into that section, which I set at the end of the execution. On second execution it sees that the variable is set and the code is not run again. Is this the best approach? Or should I be testing the value of the event (how?) and just make sure that it only executes when this event has a certain value?
    – southskies
    Commented Oct 29, 2015 at 2:00
0

The answer I provided to Leopold seemed to be the way to go - set up a variable to detect if the render has executed previously and then stop execution on the second and subsequent passes.

Agreement on this approach provided here:

http://www.codeproject.com/Articles/620110/SharePoint-Client-Side-Rendering-List-Views

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.