1

I have used the SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName) quite a bit in my JSOM (https://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx). Has anybody out there used SP.SOD.executeOrDelayUntilEventNotified(func, eventName) (https://msdn.microsoft.com/en-us/library/ff410354(v=office.14).aspx) successfully? For eventName is it as something as simple as "click"? I have searched the web but haven't found anything useful. Any Feedback appreciated.

Matt
  • 38
  • 8
  • It is used to detect a SharePoint event ("_spEventWebPartAdderReady") in the answer to this question: http://stackoverflow.com/questions/22318388/sp-ribbon-webpartcomponent-getwebpartadder-returns-undefined – Thriggle Apr 06 '16 at 19:13
  • Thanks Thriggle, good answer – Matt Apr 06 '16 at 20:21

1 Answers1

2

Basically the difference between those functions that in first case the name of the file from client library is specified, for example sp.js (parameter depScriptFileName). In the latter case the event name should be specified, for example "sp.scriptloaded-sp.js" ( parameter eventName)

Here is the declaration for SP.SOD.executeOrDelayUntilEventNotified(func, eventName) from SharePoint Client library init.js:

function ExecuteOrDelayUntilScriptLoaded(func, depScriptFileName) {
    depScriptFileName = depScriptFileName.toLowerCase();
    var eventName = "sp.scriptloaded-" + depScriptFileName;

    return ExecuteOrDelayUntilEventNotified(func, eventName);
}

About event names

The list of event names is stored in global variable called g_ExecuteOrWaitJobs. For every SharePoint Client library file is used a predefined event name, for example for a file sp.clienttemplates.js the corresponding event name is sp.scriptloaded-clienttemplates.js


Lets demonstrate how to utilize both SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName) and SP.SOD.executeOrDelayUntilEventNotified(func, eventName) functions.

For that purposes let's introduce a simple example that prints SP.Web Title property:

function printWebInfo(){
      var ctx = SP.ClientContext.get_current();
      var web = ctx.get_web();
      ctx.load(web,'Title'); 
      ctx.executeQueryAsync(
         function(){
            console.log(web.get_title());
         },
         function(sender,args){
            console.log(args.get_message()); 
         });
}

In the following example

ExecuteOrDelayUntilScriptLoaded(printWebInfo, "sp.js");

printWebInfo function will be invoked once SharePoint Client library sp.js is loaded.

The same example that utilizes SP.SOD.executeOrDelayUntilEventNotified(func, eventName) will look like this:

var eventName = "sp.scriptloaded-sp.js";
ExecuteOrDelayUntilEventNotified(printWebInfo,eventName);

where "sp.scriptloaded-sp.js" event name is used to determine whether sp.js library is loaded or not.

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • 1
    Thanks Vadim for the post, I was able to look into all the events in the g_ExecuteOrWaitJobs and see all of the events and good usage example – Matt Apr 06 '16 at 21:08