Question

I'm working on a SharePoint app where I need to run SP.RequestExecutor.executeAsync() in order to grab some information from one of the Host Web's lists. Depending on what method is calling the oData call I want to do something different.

Unfortuntely, executor.executeAsync() isn't playing as nice as $.ajax() does in terms of using $.when().done() and executing code after an async call is made. Below is some code if you'd like to see:

    load = function (){
        $.when(getEntries()).done(function () {
            ViewModels.Calendar.addEventSource(ko.utils.unwrapObservable(eventList));
        });
    }

    getEntries = function () {
        return executor.executeAsync({
            url: appweburl
                + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + LIST + "')/items?@target='" + hostweburl
                + "'&$select=Title,OData__x006e_ot5,qnlu,OData__x0066_x20"
                + "&$filter=OData__x0066_x20 eq '" + ViewModels.Person.user.userName() + "' "
                + "and qnlu ge DateTime'" + startDate().toString("yyyy-MM-dd") + "T00:00:00' "
                + "and qnlu le DateTime'" + endDate().toString("yyyy-MM-dd") + "T00:00:00' ",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetEntriesSuccess,
            error: onoDataCallFailure 
        });
    },

    onGetEntriesSuccess = function (data) {
        var jsonObject = JSON.parse(data.body);
        $.each(jsonObject.d.results, function (index, item) {
            eventList.push(new Event(item.qnlu, item.OData__x006e_ot5));
        });
    },

    onoDataCallFailure = function (data, errorCode, errorMessage) {
        alert('Failed to get host site. Error:' + errorMessage);
    };

Any ideas/suggestions would be appreciated.

Was it helpful?

Solution

Create a new .Deferred. Return Deferred.promise from the call. Call Deferred.resolve or Deferred.Reject in the succs and failure handlers.

OTHER TIPS

To add some detail to Scot's answer, you could do something like this:

function load () {
    var call = getEntries();
    call.done(function (eventList) {
        ViewModels.Calendar.addEventSource(ko.utils.unwrapObservable(eventList));
    });
}

function getEntries () {
    // create the deferred object
    var def = new $.Deferred();

    executor.executeAsync({
        url: appweburl
            + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + LIST + "')/items?@target='" + hostweburl
            + "'&$select=Title,OData__x006e_ot5,qnlu,OData__x0066_x20"
            + "&$filter=OData__x0066_x20 eq '" + ViewModels.Person.user.userName() + "' "
            + "and qnlu ge DateTime'" + startDate().toString("yyyy-MM-dd") + "T00:00:00' "
            + "and qnlu le DateTime'" + endDate().toString("yyyy-MM-dd") + "T00:00:00' ",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: onGetEntriesSuccess,
        error: onoDataCallFailure 
    });

    function onGetEntriesSuccess (data) {
        var jsonObject = JSON.parse(data.body);
        var eventList = [];
        $.each(jsonObject.d.results, function (index, item) {
            eventList.push(new Event(item.qnlu, item.OData__x006e_ot5));
        });

        // resolve the deferred object
        // eventList will be passed as parameter to done
        def.resolve(eventList);
    }

    function onoDataCallFailure (data, errorCode, errorMessage) {
        // reject the deferred object            
        def.reject('Failed to get host site. Error:' + errorMessage);
    }

    // return the promise
    return def.promise();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top