1

In the context of a page in SharePoint 2013, I am trying to get items from a list using below code but when the query executes I am getting this error:

SCRIPT5007: Unable to get property 'apply' of undefined or null reference
File: ScriptResource.axd, Line: 5, Column: 298

The response or error functions are never called.

This is the code:

jQuery(document).ready(function() { 
  // make sure the SharePoint script file 'sp.js' is loaded before code runs
  SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveList);
});

function retrieveList() {
  var ctx = new SP.ClientContext.get_current();
  var list = ctx.get_web().get_lists().getByTitle('ListTitle');
  //load list along with it's GUID and Title
  ctx.load(list, 'Title', 'Id');
  ctx.executeQueryAsync(Function.createDelegate(this,this.RetrievedListID), Function.createDelegate(this, this.ExecutionFailed));
}

//response handler
function RetrievedListID(sender, args) {
  var listid = this.list.get_id();
  return this.list.get_id();
}

//error handler - generic
function ExecutionFailed(sender, args) {
  var listid = "Error";
  return args.get_message();
}

1 Answer 1

9

1) You are using a local variable list to store your list in the retrieveList function and then you are trying to access the variable in another function. You need to make the list variable accessible to both functions.

2) Returning values from the callback functions won't return them to your main function as the callback function executes asynchronously.

3) You do not need to use the Function.createDelegate method unless you want to explicitly set the value of the this variable.

4) I have also wrapped your code in a function expression to prevent global variables.

(function(){

    jQuery(document).ready(function() { 
        // make sure the SharePoint script file 'sp.js' is loaded before code runs
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveList);
    });

    var myList;

    function retrieveList() {
      var ctx = new SP.ClientContext.get_current();
      myList = ctx.get_web().get_lists().getByTitle('ListTitle');
      //load list along with it's GUID and Title
      ctx.load(myList, 'Title', 'Id');
      ctx.executeQueryAsync(RetrievedListID, ExecutionFailed);
    }

    //response handler
    function RetrievedListID(sender, args) {
      var listid = myList.get_id();
      alert(listid);
    }

    //error handler - generic
    function ExecutionFailed(sender, args) {
      var listid = "Error";
      alert(args.get_message());
    }

})();
0

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.