Last active
August 29, 2015 14:09
-
-
Save phillipharding/aca652426e7f65211375 to your computer and use it in GitHub Desktop.
Get AppWeb and HostWeb Lists using the SharePoint 2013 REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery DOM ready | |
$(function() { | |
JSRequest.EnsureSetup(); | |
$(".content-body").empty().show(); | |
getAppWebLists(); | |
getHostWebLists(); | |
}); | |
function getAppWebLists() { | |
var | |
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), | |
$ul = $("<UL/>"); | |
$.each(data, function(i,e) { | |
$ul.append("<li>"+e.Title+"</li>"); | |
}); | |
$(".content-body").append("<h2>AppWeb Lists</h2>").append($ul); | |
}) | |
.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/>» ' + | |
((err && err.message && err.message.value) ? err.message.value : (xhrObj.status + ' ' + xhrObj.statusText)) | |
+' <br/>» '+r.url+'</div>'; | |
$(".content-body").append(m); | |
}); | |
} | |
function getHostWebLists() { | |
var | |
appWeburl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]), | |
hostWeburl = decodeURIComponent(JSRequest.QueryString["SPHostUrl"]), | |
requesturl = String.format("{0}/_api/SP.AppContextSite(@target)/web/lists?@target='{1}'", | |
appWeburl, hostWeburl), | |
executor = new SP.RequestExecutor(appWeburl), | |
r = { | |
url: requesturl, | |
method: 'GET', | |
headers: { Accept: 'application/json;odata=verbose' }, | |
success: function(response) { | |
var | |
data = JSON.parse(response.body || "{}"), | |
$ul = $("<UL/>"); | |
data = (data.value || data.d.results || data.d); | |
$.each(data, function(i,e) { | |
$ul.append("<li>"+e.Title+"</li>"); | |
}); | |
$(".content-body").append("<h2>HostWeb Lists</h2>").append($ul); | |
}, | |
error: function(data, errorCode, errorMessage) { | |
var m = String.format('<div style="color:red;font-family:Calibri;font-size:1.2em;">Exception<br/>» {0}: {1}</div>', | |
errorCode, errorMessage); | |
$(".content-body").append(m); | |
} | |
}; | |
executor.executeAsync(r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment