1

I am trying to add results from two REST calls, into one Data Tables view. I am getting just one of the Rest calls results shown.

//'use strict';
var appweburl;
var hostweburl;
var results;
var allResults = [];



$(document).ready(function () {

    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl =decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

    var scriptbase = hostweburl + "/_layouts/15/";// Load the js files and continue to the successHandler
   // $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);

    $.when(
        execCrossDomainRequest()
        ).done(function () {
            InitData();
        }
        ).fail(
        function () {
            console.log("feil");
        }
        );

});


function execCrossDomainRequest() {
    var defer = $.Deferred();

    var executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url: "/_api/web/lists/getbytitle('Ansatte')/items?$select=Title",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
        }
    );

 executor.executeAsync(
        {
            url: "/_api/web/lists/getbytitle('Kunder')/items?$select=Title",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
        }
    );

    function successHandler(a) {
        var jsonObject = JSON.parse(a.body);
        results = jsonObject.d.results;

        if (allResults.length > 0)
            allResults = allResults.concat(results);

        else
            allResults = results;
        defer.resolve();
    }

    function errorHandler(data, errorCode, errorMessage) {
        alert("Could not complete cross-domain call: " + errorMessage);
        defer.reject();
    };
    return defer;
};


function InitData() {
    console.log(allResults);
    $('#example').DataTable({
        data:allResults ,
        columns: [
                    { data: "Title" }
        ]
    });
};
1

To merge data of two lists in a datatable, i have modified the above code so that it will work:

//'use strict';
var appweburl;
var hostweburl;
var results;
var allResults = [];



$(document).ready(function () {

    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl =decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

    var scriptbase = hostweburl + "/_layouts/15/";// Load the js files and continue to the successHandler
   // $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);

    execCrossDomainRequest();

});


function execCrossDomainRequest() {

    var call1 = $.ajax({
      url: "/_api/web/lists/getbytitle('Ansatte')/items?$select=Title",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
   });

    var call2 = $.ajax({
            url: "/_api/web/lists/getbytitle('Kunder')/items?$select=Title",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler

   });

    function successHandler(a) {
        var jsonObject = JSON.parse(a.body);
        results = jsonObject.d.results;

        if (allResults.length > 0)
            allResults = allResults.concat(results);

        else
            allResults = results;        
    }

    function errorHandler(data, errorCode, errorMessage) {
        console.log("Could not complete cross-domain call: " + errorMessage);        
    };


    // When both Ajax requests were successful
   $.when(call1, call2).done(function(a1, a2){
      console.log(allResults);
    $('#example').DataTable({
        data:allResults ,
        columns: [
                    { data: "Title" }
        ]
    });
   });  
};
1

Why not reduce it to 1 query like so?

$.ajax({
    url: "/_api/web/lists/items?$filter=Title eq 'Kunder' or Title eq 'Ansatte'&$select=Title",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: successHandler,
    error: errorHandler
});

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.