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

SharePoint deveopment training course

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.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →