Skip to content

Instantly share code, notes, and snippets.

@tristian2
Last active November 4, 2015 09:00
Show Gist options
  • Save tristian2/f47df824c83a92d0bbab to your computer and use it in GitHub Desktop.
Save tristian2/f47df824c83a92d0bbab to your computer and use it in GitHub Desktop.
SharePointSitesListsandFieldsReportUsingREST
<!--place code in a script editor webpart, or use as the basis of custom JavaScript etc.
returns all Webs on a given url and child lists
to return both domains, duplicate the code, but need to configure CORS on your webservers
this version carries out three depenedent rest calls to get the webs, their lists and then the url of the list - this is a "feature" of working with restful SHarePoint
intent tion is to bundle this into a webpart - then solution-->
<script type="text/javascript">
ACME.SupportSite.Utilities.WebListsReport = function (divToPlaceReport) {
var webs = [];
var lists = [];
var TPRWebListDataContainer = 'TPRWebListData';
divToPlaceReport = '#' + divToPlaceReport;
jQuery(divToPlaceReport).append('<div id="' + TPRWebListDataContainer + '"></div>').attr('class', 'TPRWebListDataContainer');
jQuery.support.cors = true;
jQuery.ajax({
crossDomain: true,
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
url: "//sharepointsupport/_api/Web/Webs",
type: "GET",
success: function (response) {
jQuery(response.d.results).each(function () {
webs.push(this);
});
},
dataType: "json",
error: function (xhr, status) {
console.log('error');
}
}).then(function () {
jQuery.each(webs, function () {
var webUrl = this.Url;
var webGuid = this.Id
jQuery('#' + TPRWebListDataContainer).append('<table id="TPRWeb' + webGuid + '"><strong>' + this.Title + '</strong></table>').attr('class', 'TPRWeb');
jQuery.ajax({
crossDomain: true,
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
url: this.Url + "/_api/Web/Lists",
type: "GET",
success: function (response) {
//after calling fields method, back to listitems stuff
jQuery(response.d.results).each(function () {
lists.push(this);
jQuery('#TPRWeb' + webGuid).append('<tr><td id="TPRListUrl' + this.Id + '" class="TPRListUrl"><a href="' + webUrl + '/Lists/' + this.Title + '">' + this.Title + '</a></td><td id="TPRListId' + this.Id + '" class="TPRListId">' + this.Id + '</td></tr>');
});
},
dataType: "json",
error: function (xhr, status) {
console.log('error');
},
complete: function (xhr, textStatus) {
lists = [];
}
}).then(function () {
jQuery.each(lists, function () {
var listGuid = this.Id;
var listTitle = this.Title;
//now get the field
jQuery.ajax({
crossDomain: true,
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
url: webUrl + "/_api/Web/Lists(guid'" + listGuid + "')/Items?$select=GUID,FileDirRef,Title",
type: "GET",
success: function (response) {
//need to select the element of the list anchor and update it's url
jQuery('#TPRListUrl' + listGuid + '>a').prop("href", response.d.results[0].FileDirRef);
},
dataType: "json",
error: function (xhr, status) {
console.log('error');
}
}).done(function () {
console.log('complete');
});
})
}).done(function () {
console.log('done');
});
});
});
}
</script>
<div id="TPRWebListData"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment