Skip to main content
Source Link
b1c10
  • 587
  • 3
  • 16

I have one question, you are developing for APP in office 365 ?

If yes, continue reading the post. If not, explain to me how you want to do that maybe will help you :)

I'm sure you must put the AJAX that runs synchronously.

try with this code below:

"use strict";

var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var AppUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var par_url, par_list; // parameters to set in ajax;
function getItemsFromList(){
    var deferred = $.Deferred();
        $.ajax({
            url: String.format("{0}/_api/sp.appcontextsite(@target)/web/lists/getByTitle('{1}')/items?@target='{3}'", AppUrl, par_list, par_url); 
            method:'GET',
            headers:{Accept:'application/json; odata=verbose'},
            success:function(data){
                deferred.resolve(data);
            },
            error:function(err){
                deferred.reject(err);
            }
        });
        return deferred;

};

function readItemsFromList(url, page){
  var scriptbase = hostUrl + "/_layouts/15/";
  // allow cross-domain between host and app!
  $.getScript(scriptbase + "SP.RequestExecutor.js", function () {
    par_url = url;
    par_page = page;
    var processGETitems = new getItemsFromList();
    processGETitems.done(function (result) {
            return result;
    });
    processGETitems.fail(function (result) {
            return result;
    });
  });   
}


function getQueryStringParameter(urlParameterKey) {
    var params = document.URL.split('?')[1].split('&');
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split('=');
        if (singleParam[0] == urlParameterKey) return decodeURIComponent(singleParam[1]);
    }
}

And now, you can call the method:

var items = getItemsFromList('http://yourdomain.com','ListName'); //return [Object] if not, maybe your url or list not exist.

Please let know if I helped you.

Thanks, b1c10

References: http://weshackett.com/2014/02/sp-requestexecutor-cross-domain-calls-using-rest-gotcha/

Note: If you use domain sphost different of hosturl that you set, you will get acess denied by security do Office 365 ! :/

Post Made Community Wiki by b1c10