we need to filter list view items based on some conditions for which we need current context(sp.ClientContext). but we are getting undefined or null reference. we have also followed the solution provided by Vadim Gremyachev on Stack exchange

but as soon as the sp.js loads; list view also gets load. so whatever condition we want to apply to filter out list view items does not work as the required code starts working after list view loads.

Please suggest how we can solve this problem.

  • show your code; Vadims answer runs in OnPreRender, before the View is built Commented Jan 2, 2016 at 19:19
  • As of now we are executing the same code given by Vadim. as soon as the sp.js loads view gets diplayed and code related to current context execute after that. function OnPreRenderDocItemTemplate(renderCtx) { SP.SOD.executeOrDelayUntilScriptLoaded(loadContext, 'sp.js'); function loadContext() { var context = SP.ClientContext.get_current(); var web = context.get_web(); } } Commented Jan 4, 2016 at 14:31

2 Answers 2


Another solution to this problem (which I also added as an answer here) is to add whatever JSOM files you need loaded to your JSLink, to ensure they are loaded before your CSR script is loaded and runs.

For instance, I needed to use SP.WorkflowServices in a CSR script, and that requires 3 files (SP.js, SP.Runtime.js, SP.WorkflowServices.js), so I just added those three files to my field's JSLink:


JSLink then takes care of loading those files in order, and then SP.ClientContext.get_current() works from within your CSR code.


Make sure all of these scripts are loaded on the parent page:

<script type="text/javascript" src="/_layouts/1033/init.js"></script>
<script  type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script  type="text/javascript" src="/_layouts/15/sp.core.js"></script>
<script  type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script  type="text/javascript" src="/_layouts/15/sp.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 

Then as an extra step, wait for sp.js to load before executing get_current()

$(document).ready(function () {
    ExecuteOrDelayUntilScriptLoaded(init(), "sp.js");

You can also try this function:


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.