Skip to content

Instantly share code, notes, and snippets.

@zplume
Last active March 24, 2017 15:26
Show Gist options
  • Save zplume/d2148e89b55fe9a1f9efb36e06afb2ef to your computer and use it in GitHub Desktop.
Save zplume/d2148e89b55fe9a1f9efb36e06afb2ef to your computer and use it in GitHub Desktop.
(function() {
var allResults = [],
queryText = encodeURIComponent("#ext# contentclass:spspeople"),
rowLimit = 500,
selectProperties = ["AccountName", "PreferredName", "WorkEmail"];
function chunk (arr, len) {
var chunks = [],
i = 0,
n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, i += len));
}
return chunks;
}
function mapResult(value) {
var obj = {};
value.Cells.results.forEach(function(item) {
if(selectProperties.indexOf(item.Key) > -1) {
obj[item.Key] = item.Value;
}
});
return obj;
}
function getResults(startRow) {
if(!startRow)
startRow = 0;
var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryText + "'&rowlimit=" + rowLimit + "&startrow=" + startRow + "&trimduplicates=false&selectproperties='" + selectProperties.join(",") + "'&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'";
return jQuery.ajax({
url: queryUrl,
async: true,
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
}
})
.then(function (data) {
var totalRowCount = data.d.query.PrimaryQueryResult.RelevantResults.TotalRows,
results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results,
resultsMapped = jQuery.map(results, mapResult);
allResults = resultsMapped.concat(allResults); // add sites to allSites array
// if there are more results ? get the next page : return the results
return startRow + rowLimit < totalRowCount ? getResults(startRow + rowLimit) : allResults.sort(function(a, b) { return a[selectProperties[0]].localeCompare(b[selectProperties[0]]); });
});
}
getResults().then(function(results) {
var chunks = chunk(results, 1000); // split results into 1000 row chunks (limit for Chrome console.table)
chunks.forEach(function(chunk) {
console.table(chunk);
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment