Sharepoint 2013 search auto suggest using api.
Use the below snippet for SharePoint 2013 search suggest.This will only work for authenticated users not for anonymous users.
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" >
var j$ = jQuery.noConflict();
var siteCollUrl = "";
SP.SOD.executeFunc("SP.js", "SP.ClientContext", function () {
var clientContext = new SP.ClientContext.get_current();
var site = clientContext.get_site();
clientContext.load(site);
clientContext.executeQueryAsync(Function.createDelegate(this, function () {
siteCollUrl = site.get_url();
}))
});
function GetConfigValue() {
if (siteCollUrl.charAt(siteCollUrl.length - 1) == '/') {
siteCollUrl = siteCollUrl.substr(0, siteCollUrl.length - 1);
}
return siteCollUrl;
}
function EncodeString(input) {
input = input.replace(/&/g, '&');
input = input.replace(/</g, "");
input = input.replace(/>/g, "");
input = input.replace(/'/g, "");
return input;
}
j$(document).ready(function () {
var termTemplate = "<span class='ui-autocomplete-term'>%s</span>";
j$(document.getElementById('<%=txtSearch.ClientID %>')).autocomplete({
source: function (request, response) {
j$.ajax({
url: GetConfigValue() + "/_api/search/suggest?querytext='" + EncodeString(request.term) + "'&fprequerysuggestions=true&fhithighlighting=false",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var result = data.d.suggest.Queries.results;
jsonObj = [];
var arrayLength = result.length;
for (var i = 0; i < arrayLength; i++) {
var suggession = result[i].Query.replace('<B>', '<span class="autosuggest">').replace('</B>', '</span>');
item = {}
item["label"] = result[i].Query;
item["value"] = result[i].Query;
jsonObj.push(item);
//Do something
}
response(jsonObj);
},
error: function (result) {
alert("Un expected error.");
}
});
},
messages: {
noResults: '',
results: function () { }
},
minLength: 3
}).data("ui-autocomplete")._renderItem = function (ul, item) {
var term = this.element.val(),
html = item.label.replace(term, "<b>$&</b>");
return j$("<li></li>")
.data("item.autocomplete", item)
.append(j$("<a></a>").html(html))
.appendTo(ul);
}
});
</script>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" >
var j$ = jQuery.noConflict();
var siteCollUrl = "";
SP.SOD.executeFunc("SP.js", "SP.ClientContext", function () {
var clientContext = new SP.ClientContext.get_current();
var site = clientContext.get_site();
clientContext.load(site);
clientContext.executeQueryAsync(Function.createDelegate(this, function () {
siteCollUrl = site.get_url();
}))
});
function GetConfigValue() {
if (siteCollUrl.charAt(siteCollUrl.length - 1) == '/') {
siteCollUrl = siteCollUrl.substr(0, siteCollUrl.length - 1);
}
return siteCollUrl;
}
function EncodeString(input) {
input = input.replace(/&/g, '&');
input = input.replace(/</g, "");
input = input.replace(/>/g, "");
input = input.replace(/'/g, "");
return input;
}
j$(document).ready(function () {
var termTemplate = "<span class='ui-autocomplete-term'>%s</span>";
j$(document.getElementById('<%=txtSearch.ClientID %>')).autocomplete({
source: function (request, response) {
j$.ajax({
url: GetConfigValue() + "/_api/search/suggest?querytext='" + EncodeString(request.term) + "'&fprequerysuggestions=true&fhithighlighting=false",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var result = data.d.suggest.Queries.results;
jsonObj = [];
var arrayLength = result.length;
for (var i = 0; i < arrayLength; i++) {
var suggession = result[i].Query.replace('<B>', '<span class="autosuggest">').replace('</B>', '</span>');
item = {}
item["label"] = result[i].Query;
item["value"] = result[i].Query;
jsonObj.push(item);
//Do something
}
response(jsonObj);
},
error: function (result) {
alert("Un expected error.");
}
});
},
messages: {
noResults: '',
results: function () { }
},
minLength: 3
}).data("ui-autocomplete")._renderItem = function (ul, item) {
var term = this.element.val(),
html = item.label.replace(term, "<b>$&</b>");
return j$("<li></li>")
.data("item.autocomplete", item)
.append(j$("<a></a>").html(html))
.appendTo(ul);
}
});
</script>
Comments
Post a Comment