2

I would like to know what is the difference when loading SP.JS file in Publishing site and Team Site.

SP.SOD.executeFunc('sp.runtime.js', 'SP.ClientContext', function () {});
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {});
ExecuteOrDelayUntilScriptLoaded(CustomJSFunction, "SP.js");        

The above code works for Publishing Site but not for team site.

Issue is the below line of code doesnt go to CustomJSFunction in team site.

ExecuteOrDelayUntilScriptLoaded(CustomJSFunction, "SP.js");

But if its Publishing Site, the custom method gets executed.

Note: If publishing features at site level and site collection level are enabled for Team site, then the code works fine and CustomJSFunction is executed.

0

Prefer SP.SOD.executeFunc over SP.SOD.executeOrDelayUntilEventNotified since it supports on demand scripts. Many SharePoint JavaScript libraries including sp.js are registered on demand using ScriptLink control.

About ScriptLink

ScriptLink is a SharePoint server control for registering JavaScript files.

ScriptLink.OnDemand property determines whether the script should be loaded on demand like in case of Team site:

RegisterSod("sp.js", "https://cdn.sharepointonline.com/14927/_layouts/15/16.0.3912.1229/sp.js");

or loaded explicitly like in case of Publishing site:

<script type="text/javascript" src="https://cdn.sharepointonline.com/14927/_layouts/15/16.0.3912.1229/sp.js"></script>

Solution

In case of Team site:

ExecuteOrDelayUntilScriptLoaded(myfunc, "SP.js");

myfunc will not be invoked since SP.SOD.executeOrDelayUntilEventNotified does not support load on demand scripts

So, the solution would be to utilize SP.SOD.executeFunc, in that case the following code will be triggered in Team and Publishing sites:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.