Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
2k views
in SharePoint Server by 31 37 42

I got this error Cannot read properties of undefined (reading 'isNullOrUndefined') when trying to use Javascript in script editor webpart in SharePoint 2019/2016

My code is

if (!SP.ScriptHelpers.isNullOrUndefined($v0)) {

}

Console error

test.aspx:662 Uncaught TypeError: Cannot read properties of undefined (reading 'isNullOrUndefined')
    at test.aspx:662:31
    at Array.<anonymous> (ScriptResource.axd?d=cOoh1yYXNEGGL500N8suiZFwkj78PUvfVMMVZ-XIuP4i4Ou3O7yul1G1yWTTYC7yqeedYuRFmnahqKJGK9FJmqwR5LlYBgWzVBn92kygWmj4mQij31byq0Q_sZuDHHPb12n0GhFFVJ9XX7L394dEiFcrhLmHVjfk_jcodlizYhZ6YivyjyWj6r0R9_2tgY6B0&t=72fc8ae3:5:307)
    at ScriptResource.axd?d=cOoh1yYXNEGGL500N8suiZFwkj78PUvfVMMVZ-XIuP4i4Ou3O7yul1G1yWTTYC7yqeedYuRFmnahqKJGK9FJmqwR5LlYBgWzVBn92kygWmj4mQij31byq0Q_sZuDHHPb12n0GhFFVJ9XX7L394dEiFcrhLmHVjfk_jcodlizYhZ6YivyjyWj6r0R9_2tgY6B0&t=72fc8ae3:5:51370
    at SP.ClientRequest.$32_0 (sp.runtime.js:2:51281)
    at Array.<anonymous> (ScriptResource.axd?d=cOoh1yYXNEGGL500N8suiZFwkj78PUvfVMMVZ-XIuP4i4Ou3O7yul1G1yWTTYC7yqeedYuRFmnahqKJGK9FJmqwR5LlYBgWzVBn92kygWmj4mQij31byq0Q_sZuDHHPb12n0GhFFVJ9XX7L394dEiFcrhLmHVjfk_jcodlizYhZ6YivyjyWj6r0R9_2tgY6B0&t=72fc8ae3:5:307)
    at ScriptResource.axd?d=cOoh1yYXNEGGL500N8suiZFwkj78PUvfVMMVZ-XIuP4i4Ou3O7yul1G1yWTTYC7yqeedYuRFmnahqKJGK9FJmqwR5LlYBgWzVBn92kygWmj4mQij31byq0Q_sZuDHHPb12n0GhFFVJ9XX7L394dEiFcrhLmHVjfk_jcodlizYhZ6YivyjyWj6r0R9_2tgY6B0&t=72fc8ae3:5:51370
    at Sys.Net.WebRequest.completed (ScriptResource.axd?d=cOoh1yYXNEGGL500N8suiZFwkj78PUvfVMMVZ-XIuP4i4Ou3O7yul1G1yWTTYC7yqeedYuRFmnahqKJGK9FJmqwR5LlYBgWzVBn92kygWmj4mQij31byq0Q_sZuDHHPb12n0GhFFVJ9XX7L394dEiFcrhLmHVjfk_jcodlizYhZ6YivyjyWj6r0R9_2tgY6B0&t=72fc8ae3:5:89758)
    at XMLHttpRequest._onReadyStateChange (ScriptResource.axd?d=cOoh1yYXNEGGL500N8suiZFwkj78PUvfVMMVZ-XIuP4i4Ou3O7yul1G1yWTTYC7yqeedYuRFmnahqKJGK9FJmqwR5LlYBgWzVBn92kygWmj4mQij31byq0Q_sZuDHHPb12n0GhFFVJ9XX7L394dEiFcrhLmHVjfk_jcodlizYhZ6YivyjyWj6r0R9_2tgY6B0&t=72fc8ae3:5:84357)

please help me to solve this error


1 Answer

1 like 0 dislike
by 163 204 403
selected by
 
Best answer

JSOM is not working in SharePoint Server 2019 / 2016

Actually, I have many issues related to JSOM code in SharePoint Server, and the main issue was mainly related to the order of SharePoint JS files, so in case, you need to use JS code in your SharePoint Site to access SharePoint Objects using SPContext, you have to make sure that the SharePoint JS files were listed in the below order

<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.core.js"></script>

Then try to call your function using SP.SOD.executeFunc('sp.js', 'SP.ClientContext', yourfunctionname); or _spBodyOnLoadFunctionNames.push("yourfunctionname");

Example: Run JSOM script in SharePoint

<a href="#" onclick="RunJSOM();">Run JSOM in SharePoint</a>

So to get this JSOM code working in SharePoint, you have to do the following:

function RunJSOM(){
        
     SP.SOD.executeFunc("sp.js", 'SP.ClientContext', myFunction);
 }

function myFunction(){
     //add your code
}

For more details, Please check JSOM code is not Working in SharePoint 2016/2019

If you don’t ask, the answer is always NO!
...