Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save chrisobriensp/e66a03d0caa8f8e46def to your computer and use it in GitHub Desktop.

Select an option

Save chrisobriensp/e66a03d0caa8f8e46def to your computer and use it in GitHub Desktop.
Success handler code which merges results from Office 365 and on-premises sites (*if hybrid SharePoint is configured*).
function onCustomInterleavedSearchQuerySuccess() {
var mergedResults = new Array();
var fakeResultTable = {};
$.each(results.m_value.ResultTables, function (index, resultTable) {
if (resultTable.TableType === 'SpecialTermResults') {
// show best bets if we have any..
renderBestBetsResultTable(this)
}
else {
$.each(resultTable.ResultRows, function (index2, resultRow) {
var item = new COB.SharePoint.HybridSearch.GenericResult.SearchResult(resultRow);
// filtering on items which have a last modified time (for sorting by last modified)..
if (item.LastModifiedTime != null) {
mergedResults.push(item);
}
});
}
});
// now sort (using underscore.js)..
// OPTION 1: enable this section to sort by rank (descending) [but remember, this is not valid across two SP instances, e.g. O365 and on-prem]..
var sortedResults = _.sortBy(mergedResults, function (item) {
// multiply Rank by -1 to get a DESC order sort..
return (item.Rank * -1);
});
// OPTION 2: or enable this section to sort by last modified time..
//var sortedResults = _.sortBy(mergedResults, function (item) {
// // multiply milliseconds by -1 to get a DESC order sort..
// return (item.LastModifiedTime != null) ? (item.LastModifiedTime.getTime() * -1): 0;
//});
// OPTION 3: enable this line to sort by rank (ascending) - mainly to demonstrate how to do this with underscore.js [but remember, this is not valid across two SP instances, e.g. O365 and on-prem]..
//var sortedResults = _.sortBy(mergedResults, 'Rank');
// now render sortedResults..
fakeResultTable.ResultRows = sortedResults;
fakeResultTable.TableType = 'merged with custom code';
renderResultTable(fakeResultTable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment