Uncaught TypeError: Cannot read property get_current of undefined error SharePoint Online jsom

This SharePoint Online tutorial explains how to solve an issue cannot read property ‘get_current’ of undefined or cannot read property ‘get_current’ of undefined SharePoint online or the issue also comes as SharePoint jsom cannot read property ‘get_current’ of undefined.

Recently while working with javascript object model in SharePoint online site, I got the below error which says:

Uncaught TypeError: Cannot read property ‘get_current’ of undefined. I was trying to retrieve data from a SharePoint list. But it was giving the error in the below line.

var clientContext = new SP.ClientContext.get_current();

cannot read property ‘get_current’ of undefined

JavaScript object model (jsom) in SharePoint Online or SharePoint 2016/2013 depends on sp.js libraries.

SP.ClientContext presented inside sp.js in SharePoint 2013/2016 or SharePoint Online.

So we need to make sure the SharePoint script file ‘sp.js‘ is loaded before our code runs.

That we can do by following any of the approaches below:

Cannot read property ‘get_current’ of undefined

You can write the JSOM code in SP.SOD.executeFunc like below:

<script type="text/JavaScript">
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ourCustomMethodName);

function ourCustomMethodName()
{
//Your Code
}
</script>

Cannot read property ‘get_current’ of undefined SharePoint Online

Another approach is to write the JSOM code inside ExecuteOrDelayUntilScriptLoaded function like below:

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(ourCustomMethodName, “sp.js"); });

function ourCustomMethodName()
{
//Your Code
}

SharePoint jsom cannot read property ‘get_current’ of undefined

You can also directly write the Jsom code like below:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
//Your code goes here…
});

After this, the error Uncaught TypeError: Cannot read property ‘get_current’ of undefined will not come.

You may like below SharePoint jsom tutorials:

This SharePoint Online tutorial helps you to solve the issue cannot read property ‘get_current’ of undefined sharepoint Online.

>