daily pastebin goal
48%
SHARE
TWEET

SiteSuggest.js

fiddi Nov 13th, 2015 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getValueByKey(key, results) {
  2.     var postItem = jQuery.grep(results, function (e) {
  3.         if (e.Key === key)
  4.             return e;
  5.     })[0].Value;
  6.     return postItem;
  7. }
  8.  
  9. $(document).ready(function () {
  10.     $("#site-suggest-input").autocomplete({
  11.         minLength: 2,
  12.         source: function (request, response) {
  13.             $.ajax({
  14.                 beforeSend: function (request) {
  15.                     request.setRequestHeader("Accept", "application/json;odata=verbose;charset=utf-8");
  16.                 },
  17.                 url: "/_api/search/query?querytext='SiteTitle:" + encodeURI(request.term) + "* (contentclass:STS_Web OR contentclass:STS_Site)'&trimduplicates=false",
  18.                 dataType: "json",
  19.                 success: function (data) {
  20.                                         var suggestions = [];
  21.                                         jQuery.each(data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results, function () {                                          
  22.                                                 suggestions.push({ value: getValueByKey('Path', this.Cells.results), label: getValueByKey('Title', this.Cells.results) });
  23.                                         });
  24.                                         response(suggestions);
  25.                 },
  26.                 error: function (data) {
  27.                     alert('search error');
  28.                 }
  29.             });
  30.         },
  31.                 select: function (event, ui) {
  32.             location.href = ui.item.value;
  33.         }
  34.     });
  35. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top