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
1.9k views
in SharePoint Server by 30 37 42

In SharePoint 2016 and SharePoint 2019, I am using JSOM to access SharePoint objects using Javascript code to get current context as below

var spContext = new SP.ClientContext();

But I got this error this.set_formDigestHandlingEnabled is not a function in SP.js

Uncaught TypeError: this.set_formDigestHandlingEnabled is not a function
    at new SP.ClientContext (sp.js:2:11609)
    at LikePage (test.aspx:617:30)
    at HTMLAnchorElement.<anonymous> (test.aspx:686:9)
    at HTMLAnchorElement.handle (jquery.min.js:19:14478)
    at HTMLAnchorElement.<anonymous> (jquery.min.js:19:11945)Uncaught TypeError: 

this.set_formDigestHandlingEnabled is not a function SharePoint 2019


1 Answer

1 like 0 dislike
by 159 187 375
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!
...