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:
- How to get the total number of users from SharePoint group using JSOM
- Create, Update, Delete and Display List items using JavaScript Object Model (JSOM) in SharePoint
- SharePoint CSOM vs JSOM vs SSOM vs REST
- SharePoint get current user id, name, email, display name programmatically
- Grant user access to SharePoint site office 365
This SharePoint Online tutorial helps you to solve the issue cannot read property ‘get_current’ of undefined sharepoint Online.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com