SP.ClientContext.get_current() return error: Cannot read property 'webServerRelativeUrl' of undefined

2.1k views Asked by At

I have trouble solving the following issue. I am doing JSOM on html with the scripts located in the Document Library.

Here is my code

index.html

<script src="/_layouts/1033/init.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script src="/_layouts/sp.core.js"></script>
<script src="/_layouts/sp.js"></script>
<script src="../includes/jquery.min.js"></script>
<script src="../includes/script.js"></script>

script.js

jQuery(document).ready(function($) { ExecuteOrDelayUntilScriptLoaded(execOperation, "sp.js"); }

function execOperation() {
    var clientContext = new SP.ClientContext.get_current(); // Get SPSITEURL  
    console.log(clientContext)
    var list = clientContext.get_web();
}

When I try to call get_current(), it returns the following error

jquery.min.js:2 Uncaught TypeError: Cannot read property 'webServerRelativeUrl' of undefined at Function.SP.PageContextInfo.get_webServerRelativeUrl (sp.js:2) at new SP.ClientContext.get_current (sp.js:2)

Possible to help check anything wrong with my code?

1

There are 1 answers

0
AudioBubble On

May be you need to load SP.ClientContext by SP.SOD.executeFunc method or load additional scripts like here:
https://sharepoint.stackexchange.com/questions/253763/how-to-get-clientcontext-in-sharepointonline-with-jsom
Or use REST API instead of JSOM.