Created
September 8, 2016 14:16
-
-
Save klinkby/52948bdbe488d9faa640b01f2da614eb to your computer and use it in GitHub Desktop.
Extracts aggregated values from a SharePoint view using grouping
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
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