Search api in SharePoint
Search api in SharePoint
HTML Code -
<div id="searchDiv">
<b>Search For</b>
<input type="text" id="searchTerm" />
<input type="button" id="btnSearch" value="Search" onclick="searchAppWeb()" />
</div>
<div id="SearchResultsDiv"></div>
Custom Code -
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
var html;
function searchAppWeb() {
debugger;
//Get the Search Term from textbox
var searchTerm = $("#searchTerm").val();
console.log(searchTerm);
//REST API query URL
var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + searchTerm + "'";;
console.log(queryUrl);
//Empty the string
html = "";
//Make the ajax call
$.ajax({
url: queryUrl,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: onSearchSuccess,
error: onSearchError
});
}
function onSearchSuccess(data){
// JSON object contains two elements which have search results
//1. PrimaryQueryResult
//2. SecondaryQueryResults (When documents are grouped on Host Web)
//Get PrimaryQueryResult and Render it in HTML Table format
html = "<table>";
var primaryQueryResult = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
if (primaryQueryResult != null && primaryQueryResult != undefined) {
for (var iPrimaryResultCounter = 0; iPrimaryResultCounter < primaryQueryResult.length; iPrimaryResultCounter++) {
html += "<tr><td>";
html += primaryQueryResult[iPrimaryResultCounter].Cells.results[3].Value;
html += "</td><td><a href=\""
html += primaryQueryResult[iPrimaryResultCounter].Cells.results[6].Value;
html += "\">" + primaryQueryResult[iPrimaryResultCounter].Cells.results[6].Value + "</a></td><tr>";
}
}
//Get SecondaryQueryResults and continue rendering it in HTML Table format
var secondaryResult = data.d.query.SecondaryQueryResults;
if (data.d.query.SecondaryQueryResults != null && data.d.query.SecondaryQueryResults!=undefined) {
for (var iSecondaryResultCounter = 0; iSecondaryResultCounter < data.d.query.SecondaryQueryResults.results.length; iSecondaryResultCounter++) {
var resultBlock = data.d.query.SecondaryQueryResults.results[iSecondaryResultCounter].RelevantResults.Table.Rows.results;
for (var iResults = 0; iResults < resultBlock.length; iResults++) {
html += "<tr><td>";
html += resultBlock[iResults].Cells.results[3].Value;
html += "</td><td><a href=\""
html += resultBlock[iResults].Cells.results[6].Value;
html += "\">" + resultBlock[iResults].Cells.results[6].Value + "</a></td><tr>";
}
}
}
html += "</table>";
$("#SearchResultsDiv").append(html);
}
function onSearchError(err){
alert(JSON.stringify(err));
}
</script>
HTML Code -
<div id="searchDiv">
<b>Search For</b>
<input type="text" id="searchTerm" />
<input type="button" id="btnSearch" value="Search" onclick="searchAppWeb()" />
</div>
<div id="SearchResultsDiv"></div>
Custom Code -
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
var html;
function searchAppWeb() {
debugger;
//Get the Search Term from textbox
var searchTerm = $("#searchTerm").val();
console.log(searchTerm);
//REST API query URL
var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + searchTerm + "'";;
console.log(queryUrl);
//Empty the string
html = "";
//Make the ajax call
$.ajax({
url: queryUrl,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: onSearchSuccess,
error: onSearchError
});
}
function onSearchSuccess(data){
// JSON object contains two elements which have search results
//1. PrimaryQueryResult
//2. SecondaryQueryResults (When documents are grouped on Host Web)
//Get PrimaryQueryResult and Render it in HTML Table format
html = "<table>";
var primaryQueryResult = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
if (primaryQueryResult != null && primaryQueryResult != undefined) {
for (var iPrimaryResultCounter = 0; iPrimaryResultCounter < primaryQueryResult.length; iPrimaryResultCounter++) {
html += "<tr><td>";
html += primaryQueryResult[iPrimaryResultCounter].Cells.results[3].Value;
html += "</td><td><a href=\""
html += primaryQueryResult[iPrimaryResultCounter].Cells.results[6].Value;
html += "\">" + primaryQueryResult[iPrimaryResultCounter].Cells.results[6].Value + "</a></td><tr>";
}
}
//Get SecondaryQueryResults and continue rendering it in HTML Table format
var secondaryResult = data.d.query.SecondaryQueryResults;
if (data.d.query.SecondaryQueryResults != null && data.d.query.SecondaryQueryResults!=undefined) {
for (var iSecondaryResultCounter = 0; iSecondaryResultCounter < data.d.query.SecondaryQueryResults.results.length; iSecondaryResultCounter++) {
var resultBlock = data.d.query.SecondaryQueryResults.results[iSecondaryResultCounter].RelevantResults.Table.Rows.results;
for (var iResults = 0; iResults < resultBlock.length; iResults++) {
html += "<tr><td>";
html += resultBlock[iResults].Cells.results[3].Value;
html += "</td><td><a href=\""
html += resultBlock[iResults].Cells.results[6].Value;
html += "\">" + resultBlock[iResults].Cells.results[6].Value + "</a></td><tr>";
}
}
}
html += "</table>";
$("#SearchResultsDiv").append(html);
}
function onSearchError(err){
alert(JSON.stringify(err));
}
</script>
Comments
Post a Comment