I have the same scenario as what this below question has:

How to get the return value of an on demand executed function?

I have SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getPageTitle); function which gets me the title of a page. I want to be able to get the return of this method to a variable something like:

var par = SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getPageTitle);

I have tried this but doesnt work

Asking this question as there is no response on the above question. Please take this as a priority thing because the old question was asked 2 years back. Kindly help in this regards.

Please note: I am using it in Sharepoint Online but its a generic question

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
     var par = getPageTitle();
     //Do the rest of your logic here

The third parameter is a callback function. The SP.SOD.executeFunc call doesn't actually return the value of your callback function. So you can supply your own callback function as above and do whatever you like (including calling other functions and processing their return values).

SP.SOD.executeFunc Documentation

  • No, This doesn't work, It gives par as undefined. Jun 29 '16 at 12:57
  • I was assuming getPageTitle() is your function. If you are just looking to get the current title of the page then you can just use document.title and forget all this SOD stuff. Jun 29 '16 at 13:06
  • No its a function which returns something. Its an example which I posted. Jun 29 '16 at 13:10
  • I think there may be some confusion about when par will be populated. If you put lines of code immediately following the executeFunc call then par would not have been populated yet. Your logic needs to happen in the callback function (I've moved the var par declaration here to help emphasize that). Jun 29 '16 at 13:13
  • yes it might happen here, as there is an asynchronous call which is happening which populates the return value. Its in executeQueryAsync method. Any idea how i could get the return to work if its coming from an asynchronous call? Jun 29 '16 at 13:16

The SP.SOD.executeFunc function will always return undefined, as most SharePoint JS functions that run asynchronous code do. The third argument, your getPageTitle callback function, can be declared to return something, but that something cannot be captured by the SP.SOD.executeFunc function to be able to do var par = SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getPageTitle);.

From your comments on Chris Kent's answer, I understand your getPageTitle function is doing some asynchronous logic as well (hint: executeQueryAsync). In this case, you need another callback function because you cannot return the result of the executeQueryAsync function in your getPageTitle function.

Here is how your code could look like if you were to get the web title (instead of page title):

var web;

// Run this when the async call to get the web title succeeds.
function getWebTitleSuccess() {
    var par =  web.get_title();

    // more logic here that depends on your web title value.

// Run this when the async call to get the web title fails.
function getWebTitleFailure(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

function getWebTitle() {
    var context = SP.ClientContext.get_current();        

    // Note the "web" variable has been declared globally so it can be accessed 
    // in other functions (e.g. the "getPageTitleSuccess" function).
    web = context.get_web();

    context.executeQueryAsync(getWebTitleSuccess, getWebTitleFailure);

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getWebTitle);

I hope this clarifies why you cannot return a value, but rather use callback functions to get what you need.

When you work with asynchronous JS, you need to use callback functions that will run only after the async execution completes. Using the return keyword works only in synchronous scenarios because it immediately returns a value without waiting for the async logic to complete.

  • Hi, Thanks for the elaborate explanation on this and your response. I will try your answer. Jun 29 '16 at 14:57

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.