Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phillipharding/05747f21b0cf870134e9 to your computer and use it in GitHub Desktop.
Save phillipharding/05747f21b0cf870134e9 to your computer and use it in GitHub Desktop.
Get AppWeb Lists using a Promises Pattern (or chaining) Allowing for Enrichment/Further Processing
/* jQuery DOM ready */
$(function() {
JSRequest.EnsureSetup();
GetAppWebLists()
.then(EnrichData)
.then(RenderAppWebLists)
.fail(function(error) {
$(".content-body").html(error).show();
});
});
function GetAppWebLists() {
var
p = new $.Deferred(),
appWeburl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]),
r = {
url: appWeburl + '/_api/web/lists',
type: 'GET',
headers: { ACCEPT: 'application/json;odata=verbose' }
};
$.ajax(r)
.done(function(response, textStatus, xhrObj) {
var data = (response.value || response.d.results || response.d);
/* you might want to mangle or otherwise process the REST results
here before resolving the promise with the data
*/
p.resolve(data);
})
.fail(function(xhrObj, textStatus, err) {
var
e = JSON.parse(xhrObj.responseText || "{}"),
err = e.error || e["odata.error"],
m = '<div style="color:red;font-family:Calibri;font-size:1.2em;">Exception<br/>&raquo; ' +
((err && err.message && err.message.value) ? err.message.value : (xhrObj.status + ' ' + xhrObj.statusText))
+' <br/>&raquo; '+r.url+'</div>';
p.reject(m);
});
return p.promise();
}
function EnrichData(data) {
$.each(data, function(i,e) {
e.Title = e.Title + " ("+e.Title.toUpperCase()+")";
});
/* either return the data we were passed or return a new Promise and resolve/reject it here */
return data;
}
function RenderAppWebLists(data) {
$(".content-body").empty().show();
$ul = $("<UL/>");
$.each(data, function(i,e) {
$ul.append("<li>"+e.Title+"</li>");
});
$(".content-body").append("<h2>AppWeb Lists</h2>").append($ul);
/* either return the data we were passed or return a new Promise and resolve/reject it here */
return data;
}
@westerdaled
Copy link

Can this work with a cross domain calls ?

@phillipharding
Copy link
Author

Sure, the key is the way promises are used to chain functions together, regardless of what those functions do

@westerdaled
Copy link

Thanks. Sorry I missed both you at SharePoint Stat.. I am the chap who 'won' the pebble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment