lundi 9 février 2015

SharePoint App Crossdomain Rest function rewritten as a Promise



I have written a successful crossdomain call using Rest which is hosted in an Menu Action App. It returns all document item properties. Ok now I think is the time to rewrite as a JavaScript" Promise. Could someone please take look at my code to see if I am on the write lines - allot of the samples I have seen seem to use $.ajax() whereas I have previously used a different syntax as shown below.



// define our unique namespace for our functions.
var WSL = window.Client.WSL || {};
WSL.Rest = WSL.Rest || {};

WSL.Rest.Lists = function () {


// REST Style promise functions
var getItemAllFields = function () {


var deferred = $.Deferred();


var executor;


$('#message').text("REST Cross domain call"); // document.getElementById("message").innerText =

// although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
executor = new SP.RequestExecutor(appweburl);



var urlItemAllFields = appweburl +
"/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('" + itemurl + "')/ListItemAllFields?@target='" + hostweburl + "'";



executor.executeAsync(
{

url: urlItemAllFields,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: Function.createDelegate(this, function (data, errorCode, errorMessage) {
//alert('Sucess');
deferred.resove(data, errorCode,errorMessage);


}),

error: Function.createDelegate(this, function (data, error, errorMessage) { deferred.reject(data, error, errorMessage);


}));




return deferred.promise();



};



return {

getItemAllFields: getItemAllFields

};


}();







0 commentaires:

Enregistrer un commentaire