Skip to content

Instantly share code, notes, and snippets.

@alirobe
Created July 2, 2015 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alirobe/d5ad4cd9f3c0eed1dc27 to your computer and use it in GitHub Desktop.
Save alirobe/d5ad4cd9f3c0eed1dc27 to your computer and use it in GitHub Desktop.
SharePoint 2013 Related Pages
// 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