Can any body explain this ?

Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed));

it always output an Error in on MSDN site sure i've something wrong it worked when i replaced with the functions like


can anybody tell why ? and whats "createDelegate" ? and whats "this" ?

https://onedrive.live.com/redir?resid=EA9AE801B5328F18!1627&authkey=!APlXsyQQR9a-DJI&v=3&ithint=photo%2cPNG the error

Copied code from PastBin

function getAllFilesInFolders(listName) {
    var images = [];
var clientContext = SP.ClientContext.get_current();
var lists = clientContext.get_web().get_lists();
var oList = clientContext.get_web().get_lists().getByTitle(listName);
var collListItem;
var camlQuery = SP.CamlQuery.createAllItemsQuery();

 var query = SP.CamlQuery.createAllItemsQuery();
   // camlQuery.set_folderServerRelativeUrl("/"+ListName+"/"+Folder);

collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(File, FileSystemObjectType)');

 clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    function onQuerySucceeded(sender, args) {
       var listItemEnumerator = collListItem.getEnumerator();
        var currentItem = listItemEnumerator.get_current();                                                                                                                    
        if(currentItem.get_fileSystemObjectType() == "0")
            var file = currentItem.get_file();
            if(file != null)
                //console.log('File Name: ' + file.get_name() + '\n' + 'File Url: ' + file.get_serverRelativeUrl());






    function onQueryFailed(sender, args) {
        alert('Error getting list items: ' + args.get_message());


it worked only like this

Function.createDelegate(this, onQuerySucceeded)

3 Answers 3


JavaScript has objects and functions.

this is attached to functions. When you invoke a function this get initialized differently based on how it is invoked.

Below code is the typical pattern used in MSDN

<script type="text/javascript">
function StartFunction()
    var web = clientContext.get_web();
    var listCollection = web.get_lists();
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

function onQueryFailed(sender, args) {
  //show error message ()stacktrace
  alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());

function onQuerySucceeded() {
    //show successful message

Function.createDelegate creates a delegate function that retains the context first used during an objects creation. The createDelegate function is useful when setting up an event handler to point to an object method that must use the this pointer within its scope.

  • Amal good explanation. can you explain more, i still don't get it. and by the way good question. Commented Jan 30, 2015 at 13:17

There are two ways to call executeQueryAsync in JSOM:

Function.createDelegate(this, _onSucceed), 
Function.createDelegate(this, _onFail)


context.executeQueryAsync(_onSucceed, _onFail);

The difference between the two is

the former allows you to override what the value of the "this" variable will be inside the function. In the latter case, not using Function.createDelegate, when you are inside either the _onSucceed or _onFail callbacks, this will reference the callback function itself – onSucceed or onFail respectively.

For more details visit this link from where I copied the above information. But basically creating delegate sets the value of this in callback functions. Hope it helps.

  • Thanks @Garima for the link and the explaination it was helpful Commented Jan 30, 2015 at 13:38
  • I am glad it helped :)
    – Garima
    Commented Jan 30, 2015 at 13:43
  • 1
    Just want to add that Function.createDelegate more or less just do an .apply() , you could have done context.executeQueryAsync(_onSucceed.apply(this),....); Commented Feb 16, 2015 at 7:31

Apart from the explanation given by Amal, the reason you see error is var collListItem; is defined in getAllFilesInFolders function and the onQuerySucceeded method doesn't have its information unless you use this.collListItem which is passed during Function.createDelegate(this, this.onQuerySucceeded). Else you should define var collListItem; globally in order to use it as you are doing correctly.


If you were to use clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);, then the variable collListItem will need to be declared globally as onQuerySucceeded doen't have any context about it. Also using this.collListItem will not be helpful as this doesn't represent the function getAllFilesInFolders where collListItem is declared. However, when you use

    Function.createDelegate(this, this.onQuerySucceeded),
    Function.createDelegate(this, this.onQueryFailed));

In the onQuerySucceeded, you can use this.collListItem as this now represents getAllFilesInFolders object which is passed as the first parameter in Function.createDelegate

  • when i defined collListItem variable Globally Still the same error only worked when i remove this.onQuerySucceed to be come Function.createDelegate(this, onQuerySucceeded) Commented Jan 30, 2015 at 13:37
  • If you use Function.createDelegate(this, onQuerySucceeded) then you don't need to declare the variable globally. this makes it possible to refer to variable using this.variable in onQuerySucceeded method. Commented Jan 30, 2015 at 13:43
  • sorry Nadeem u know i just started looking at that yesterday im little bit confused can u give little example of what u just said ? Commented Jan 30, 2015 at 13:54
  • Not a problem. We are here to help each other. Commented Jan 30, 2015 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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