Created
July 2, 2015 05:11
-
-
Save alirobe/d5ad4cd9f3c0eed1dc27 to your computer and use it in GitHub Desktop.
SharePoint 2013 Related Pages
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
// shows related pages in SharePoint 2013 or Office 365 | |
// via https://gist.github.com/chrisobriensp/4375519 | |
// requires (minified under 10k): underscorejs, es6-promise (ES6 polyfill), fetch (ES6 polyfill) | |
function(element, title, count) { | |
var header = '<b>'+title+'</b><br/>'; | |
var _linkTmpl = _.template('<a href="<%=Cells.results[6].Value%>"><%=Cells.results[3].Value%></a>'); | |
var linkJoin = '<br/>'; | |
var linkCount = count; | |
var query = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + document.title + "'"; | |
fetch(query, { | |
headers: {'Accept': 'application/json; odata=verbose'}, | |
}).then(function(response) { | |
return response.json(); | |
}).then(function(json) { | |
var results = json.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results; | |
if(results.length) { | |
var html = _(results).take(linkCount).map(_linkTmpl).join(linkJoin); | |
element.innerHTML = header + html; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment