I am struggling with javascript and learning callbacks, and applying this to SharePoint 2010 specific js/jquery methods of getting and savings values from lists. So I am recently getting into the following method (i know there are others but I would like to learn this one):

function getListData()
    var gridURL = "http://spdev:42977/sites/devcenter/_vti_bin/ListData.svc/Projects";
    var request = new Sys.Net.WebRequest();
    request.get_headers()["Accept"] = "application/json";

function onCompletedCallback(response, eventArgs) {
      var getProject = eval("(" + response.get_responseData() + ")");
      var buildMarkUp = "";
      for (var i = 0; i < getProject.d.results.length; i++)  {
         buildMarkUp += "<div>" + getProject.d.results[i].Title + " </div>";
         var passID = getProject.d.results[i].ProjectID;

         // need a nested call to go out and get items from related lists using the  passID varible, and build table/grid   

so i trying to build a data grid, and for parent item, or project ID/Title that comes back on the list has several related data values stored in other lists that I need to pull, but I can't seem to figure out how to apply callbacks to the above method (over and over for each list I need to pull from since they have their own very specific pre-defined callbacks returning the data already).

So I asked a similiar question on stack which got me pointed in the direction of callbacks in the first place (new to me). I am beginning to understand this in a basic sense....

hence the example here.

I tried playing around with the SP method like:

request.add_completed(onCompletedCallback, passedAdditionalValue);

thinking I might be able to keep passing and build the string through all the repeat functions and callbacks. adding additional parameter to the successfulCallback doesn't work because it breaks the response, eventArgs......response not define.

how would i go about using the getListData() listdata.svc with callback method per above, then use it again (2 or 3 times more) to get additional relative data, using more callbacks? or something like this to achieve the desired result. I can't seem to figure out how to apply the general callbacks to the SP specific example.

greatly appreciated as always!

1 Answer 1


Cool that you use the built-in asp.net ajax class Sys.Net.WebRequest(). I have always used other ajax frameworks like jQuery ajax, angular $http, amplify request.

But it doesn't matter what you use. Callbacks are vital for ajax implementations. Because you have a series of ajax requests, in order to reuse your functions, you have to pass a callback into your function as parameter. Consider this:

var projectsUrl = "http://spdev:42977/sites/devcenter/_vti_bin/ListData.svc/Projects";
var someOtherUrl = "http://spdev:42977/sites/devcenter/_vti_bin/ListData.svc/OtherStuff";
function getListData(gridUrl, callback)
    var request = new Sys.Net.WebRequest();
    request.get_headers()["Accept"] = "application/json";

function onProjectsCallback(response, eventArgs) {
      //show your content as you wish

      // nested invokation
      // want to modify "someOtherUrl" based on projects you got, go ahead! 
      getListData(someOtherUrl , callback)   

function onOtherCallback(response, eventArgs) {
      // do what you need whith this "other" data
      //nested invokation again, go ahead!

//Now the most important. Fire the chain of requests:
getListData(projectsUrl, onProjectsCallback)
  • 1
    ...thanks so much for this example. this is starting to make much more sense to me now that I can see it; and not just how to make this work for my SP use case, but this concept in general. appreciated as always!
    – Justin
    Feb 2, 2013 at 16:32
  • Glad to help. Cool to see that you embrace the client side! Feb 2, 2013 at 22:14

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.