Skip to content

Instantly share code, notes, and snippets.

@klinkby
Created September 8, 2016 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klinkby/52948bdbe488d9faa640b01f2da614eb to your computer and use it in GitHub Desktop.
Save klinkby/52948bdbe488d9faa640b01f2da614eb to your computer and use it in GitHub Desktop.
Extracts aggregated values from a SharePoint view using grouping
function Grouping(title, value, count) {
Object.defineProperties(this, {
title: { value: title },
value: { value: value },
count: { value: count },
groups: { writable: true }
});
}
function getGroupedViewData(listTitle, viewTitle, success, fail) {
var url = window._spPageContextInfo.webServerRelativeUrl +
"/_api/Web/Lists/GetByTitle(%27" +
encodeURIComponent(listTitle) +
"%27)/Views/GetByTitle(%27" +
encodeURIComponent(viewTitle) +
"%27)/renderAsHtml()";
var oReq = new XMLHttpRequest();
oReq.onload = function(e) {
var s = document.createElement("div");
s.innerHTML = this.responseXML.documentElement.textContent;
var pattern = /^\s*([\w\s]+)?\s:\s(.+?)[\s\W]+[(](\d+)/,
match,
tbody,
mapped,
retArr = Array.prototype.map.call(s.querySelectorAll(".ms-gb"), function (g1) {
match = pattern.exec(g1.textContent);
tbody = g1.closest("tbody");
mapped = Array.prototype.map.call(
tbody.closest("table").querySelectorAll("tbody[groupstring^='" + tbody.getAttribute("groupstring") + "']"),
function (g2) {
match = pattern.exec(g2.textContent);
return new Grouping(match[1], match[2], Number(match[3]));
});
var gHead = mapped.shift(); // extract first item
gHead.groups = mapped;
return gHead;
});
success(retArr);
};
onerror = fail;
oReq.open("GET", url);
oReq.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment