2

I have a script file MyScript.js in the folder TEMPLATES\LAYOUTS\MyProject\Scripts which looks like this:

var MyCompany = window.MyComapny || {};
MyComany.MyProject = MyComany.MyProject || {};

MyCompany.MyProject.alertingFunc = function (firstName, lastName) {
  "use strict";

  var bondStyle = 'My name is ' + lastName + ', ' + firstName + ' ' + lastName + '.';
  alert(bondStyle);
};

MyCompany.MyProject.returningFunc = function (firstName, lastName) {
  "use strict";

  var bondStyle = 'My name is ' + lastName + ', ' + firstName + ' ' + lastName + '.';
  return bondStyle;
};

SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs('MyScript.js');

I can manually load this script on demand and call alertingFunc this way:

SP.SOD.registerSod('MyScript.js', '/_layouts/MyProject/Scripts/MyScript.js');
SP.SOD.execute('MyScript.js', 'MyCompany.MyProject.alertingFunc', 'Big', 'Boss');

Now to the question: How can I on demand execute a function which returns a value?

The SP.SOD.execute function does not provide a return value. There is a callback version.

SP.SOD.registerSod('MyScript.js', '/_layouts/MyProject/Scripts/MyScript.js');
SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject.returningFunc', function (result) {
  alert(result);
});

The callback function does not get the return value of MyCompany.MyProject.returningFunc as parameter injected.

One possible solution is to use LoadSodByKey this way

SP.SOD.registerSod('MyScript.js', '/_layouts/MyProject/Scripts/MyScript.js');
LoadSodByKey(NormalizeSodKey('MyScript.js'), function () {
  var result = MyComany.MyProject.returningFunc('Big', 'Boss');
});

LoadSodByKey is nowhere documented in MSDN. It is a private function which can be changed in future releases. Therefore I would like to use the public documented APIs.

EDIT: Solution

I have found a solution which is a combination of the LoadSodByKey and SP.SOD.exececuteFunc aproach above.

SP.SOD.registerSod('MyScript.js', '/_layouts/MyProject/Scripts/MyScript.js');
SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject.returningFunc', function () {
  var result = MyComany.MyProject.returningFunc('Big', 'Boss');
});

In the MSDN Documentation the description of the parameters is wrong.

Parameters

key

Type: String

The name of the file containing the function that is executed.

functionName

Type: String

The name of the function that is executed.

fn

Type: Function

The function that is called once functionName has finished executing.

In my tests the function functionName is never called. Only the callback function fn is executed whenever SP.SOD.executeFunc(key, functionName, fn) is executed.

You have to be careful when passing the value of the functionName parameter. It must be a valid function or object. These are all valid calls:

SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject.returningFunc', function () {
  var result = MyComany.MyProject.returningFunc('Big', 'Boss');
});
SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject', function () {
  var result = MyComany.MyProject.returningFunc('Big', 'Boss');
});
SP.SOD.executeFunc('MyScript.js', 'MyCompany', function () {
  var result = MyComany.MyProject.returningFunc('Big', 'Boss');
});

If functionName does not exists in MyScript.js then the callback function fn is only executed once.

SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject.nonexistentFunc', function () {
  alert(MyComany.MyProject.returningFunc('Big', 'Boss'));
});
SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject.nonexistentFunc', function () {
  alert(MyComany.MyProject.returningFunc('Big', 'Boss'));
});

The text "My name is Boss, Big Boss." is alerted only once. In contrast to the above these statements

SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject', function () {
  alert(MyComany.MyProject.returningFunc('Big', 'Boss'));
});
SP.SOD.executeFunc('MyScript.js', 'MyCompany.MyProject', function () {
  alert(MyComany.MyProject.returningFunc('Big', 'Boss'));
});

show the message box twice.

Your Answer

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

Browse other questions tagged or ask your own question.