3

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 – Danny '365CSI' Engelman Jan 2 '16 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(); } } – Harminder Singh Jan 4 '16 at 14:31
0

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:

clienttemplates.js|sp.js|sp.runtime.js|sp.workflowservices.js|~site/path/to/my-csr-script.js

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

0

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:

_spBodyOnLoadFunctionNames.push("init");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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