Created
May 1, 2014 06:22
-
-
Save chrisobriensp/ca89a98b622fff902ec0 to your computer and use it in GitHub Desktop.
Full CSOM code used to demonstrate executing a search, and then combining result tables (i.e. for Office 365 and SharePoint hybrid deployments) into one resultset. See accompanying blog post for notes.
This file contains hidden or 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
| var COB = COB || {}; | |
| COB.SharePoint = COB.SharePoint || {}; | |
| COB.SharePoint.HybridSearch = COB.SharePoint.HybridSearch || {}; | |
| COB.SharePoint.HybridSearch.Standard = COB.SharePoint.HybridSearch.Standard || {}; | |
| COB.SharePoint.HybridSearch.CustomInterleaving = COB.SharePoint.HybridSearch.CustomInterleaving || {}; | |
| var results; | |
| COB.SharePoint.HybridSearch.GenericResult = (function ( ) { | |
| var __searchResult; | |
| __searchResult = function ( data ) { | |
| var self = this; | |
| self.Path = data.Path; | |
| self.Title = data.Title; | |
| self.Rank = data.Rank; | |
| self.LastModifiedTime = data.LastModifiedTime; | |
| self.Description = data.Description; | |
| }; | |
| return { | |
| SearchResult: __searchResult | |
| }; | |
| }) ( ); | |
| COB.SharePoint.HybridSearch.Utility = { | |
| GetParameterByName: function (name) { | |
| name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
| var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | |
| results = regex.exec(location.search); | |
| return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
| } | |
| } | |
| COB.SharePoint.HybridSearch.Standard = { | |
| someVar: '', | |
| Init: function() { | |
| $.getScript("/_layouts/15/SP.search.js", | |
| function () { | |
| COB.SharePoint.HybridSearch.Standard.RunSearch(); | |
| }); | |
| }, | |
| RunSearch: function () { | |
| $('#CSOMSearchWP').text(''); | |
| var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl); | |
| var contextSite = clientContext.get_site(); | |
| var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); | |
| var queryText = COB.SharePoint.HybridSearch.Utility.GetParameterByName('query'); | |
| if (queryText == undefined) { | |
| queryText = 'sharepoint'; | |
| } | |
| var enableInterleavingOverride = COB.SharePoint.HybridSearch.Utility.GetParameterByName('enableInterleaving'); | |
| var enableInterleaving = (enableInterleavingOverride === 'true') ? true : false; | |
| $('#queryText').text(queryText); | |
| $('#interleaving').text(enableInterleaving); | |
| keywordQuery.set_queryText(queryText); | |
| // here's where we set the interleaving property.. | |
| keywordQuery.set_enableInterleaving(enableInterleaving); | |
| var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext); | |
| results = searchExecutor.executeQuery(keywordQuery); | |
| clientContext.executeQueryAsync(onSearchQuerySuccess, onSearchQueryError); | |
| } | |
| } | |
| COB.SharePoint.HybridSearch.CustomInterleaving = { | |
| Init: function () { | |
| $.getScript("/_layouts/15/SP.search.js", | |
| function () { | |
| COB.SharePoint.HybridSearch.CustomInterleaving.RunSearch(); | |
| }); | |
| }, | |
| RunSearch: function () { | |
| $('#CSOMSearchWP_Interleaved').text(''); | |
| var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl); | |
| var contextSite = clientContext.get_site(); | |
| var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); | |
| var queryText = COB.SharePoint.HybridSearch.Utility.GetParameterByName('query'); | |
| if (queryText == undefined) { | |
| queryText = 'sharepoint'; | |
| } | |
| var enableInterleavingOverride = COB.SharePoint.HybridSearch.Utility.GetParameterByName('enableInterleaving'); | |
| var enableInterleaving = (enableInterleavingOverride === 'true') ? true : false; | |
| $('#queryText').text(queryText); | |
| $('#interleaving').text(enableInterleaving); | |
| keywordQuery.set_queryText(queryText); | |
| // here's where we set the interleaving property.. | |
| keywordQuery.set_enableInterleaving(enableInterleaving); | |
| var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext); | |
| results = searchExecutor.executeQuery(keywordQuery); | |
| clientContext.executeQueryAsync(onCustomInterleavedSearchQuerySuccess, onSearchQueryError); | |
| } | |
| } | |
| function renderResultTable(resultTable) { | |
| // since this code is used by two web parts, some presentation code here to decide what to do.. | |
| var selector; | |
| if ($('#CSOMSearchWP').is("DIV")) { | |
| selector = '#CSOMSearchWP'; | |
| } | |
| if ($('#CSOMSearchWP_Interleaved').is("DIV")) { | |
| selector = '#CSOMSearchWP_Interleaved'; | |
| } | |
| $(selector).append('<H1>Result table - ' + resultTable.TableType + '</H1>'); | |
| $(selector).append('<table style=\'border-spacing: 3px; empty-cells: show; border: thin solid #C0C0C0\'>'); | |
| $(selector).append('<tr style=\'background-color: #E4E4E4\' class=\'ms-webpart-titleText\'><td>Title</td><td>Url</td><td>Rank</td><td>Last modified</td><td>Description</td></tr>'); | |
| $.each(resultTable.ResultRows, function () { | |
| $(selector).append('<tr>'); | |
| $(selector).append('<td>' + this.Title + '</td>'); | |
| $(selector).append('<td>' + this.Path + '</td>'); | |
| $(selector).append('<td>' + this.Rank + '</td>'); | |
| $(selector).append('<td>' + this.LastModifiedTime + '</td>'); | |
| $(selector).append('<td>' + this.Description + '</td>'); | |
| $(selector).append('</tr>'); | |
| }); | |
| $(selector).append('</table><br />'); | |
| } | |
| function renderBestBetsResultTable(resultTable) { | |
| var selector; | |
| if ($('#CSOMSearchWP').is("DIV")) { | |
| selector = '#CSOMSearchWP'; | |
| } | |
| if ($('#CSOMSearchWP_Interleaved').is("DIV")) { | |
| selector = '#CSOMSearchWP_Interleaved'; | |
| } | |
| $(selector).append('<H1>Result table - ' + resultTable.TableType + '</H1>'); | |
| $(selector).append('<table style=\'border-spacing: 3px; empty-cells: show; border: thin solid #C0C0C0\'>'); | |
| $(selector).append('<tr style=\'background-color: #E4E4E4\' class=\'ms-webpart-titleText\'><td>Title</td><td>Url</td><td>Description</td></tr>'); | |
| $.each(resultTable.ResultRows, function () { | |
| $(selector).append('<tr>'); | |
| $(selector).append('<td>' + this.Title + '</td>'); | |
| $(selector).append('<td>' + this.Url + '</td>'); | |
| $(selector).append('<td>' + this.Description + '</td>'); | |
| $(selector).append('</tr>'); | |
| }); | |
| $(selector).append('</table><br />'); | |
| } | |
| function onSearchQuerySuccess() { | |
| $.each(results.m_value.ResultTables, function () { | |
| if (this.TableType === 'SpecialTermResults') { | |
| // show best bets if we have any.. | |
| renderBestBetsResultTable(this) | |
| } | |
| else { | |
| renderResultTable(this); | |
| } | |
| }); | |
| } | |
| 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 { | |
| console.log("Processing result table with " + resultTable.ResultRows.length + " rows"); | |
| $.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); | |
| } | |
| function onSearchQueryError() { | |
| if ($('#CSOMSearchWP').is("DIV")) { | |
| $('#CSOMSearchWP').text('Something went wrong executing the search :('); | |
| } | |
| if ($('#CSOMSearchWP_Interleaved').is("DIV")) { | |
| $('#CSOMSearchWP_Interleaved').text('Something went wrong executing the search :('); | |
| } | |
| } | |
| $(document).ready(function () { | |
| // since this JS file is added to multiple pages (would be changed for prod use!), just doing some hacky | |
| // DOM detection here to work out which mode we're operating in.. | |
| if ($('#CSOMSearchWP').is("DIV")) { | |
| SP.SOD.executeFunc('sp.js', 'SP.ClientContext', COB.SharePoint.HybridSearch.Standard.Init); | |
| } | |
| if ($('#CSOMSearchWP_Interleaved').is("DIV")) { | |
| SP.SOD.executeFunc('sp.js', 'SP.ClientContext', COB.SharePoint.HybridSearch.CustomInterleaving.Init); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment