Skip to main content
2 of 2
added 1913 characters in body
Utkarsh Dubey
  • 379
  • 1
  • 4
  • 18

Apply search in SharePoint Custom List

I want to apply Search functionality only on Custom List in SharePoint using Rest API or CAML query.

  • I already tried that rest api example:

     $.getJSON("https://SharePointURL/_api/search/query?
     querytext='QueryText*+Path:https://SharePointListPath", function(data){
         console.log(data);
     });
    

but it take too much of JSON data. Therefore any example to make it simple.

Visit this URL: Calling SharePoint Search Using Rest

Go through this code:

function getSearchResultsUsingREST(queryText) {
    // search can use the app web URL as the base, so no need to use SP.AppContextSite(@target) to access the host web..
    var searchUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryText + "'";
    var executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);
    executor.executeAsync(
        {
            url: searchUrl,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetSearchResultsSuccess,
            error: onGetSearchResultsFail
        }
    );
}

function onGetSearchResultsSuccess(data) {
    var jsonObject = JSON.parse(data.body);
    var results = jsonObject.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
    if (results.length == 0) {
        $('#related-content-results').text('No related documents were found');
    }
    else {
        var searchResultsHtml = '';
        $.each(results, function (index, result) {
            searchResultsHtml += "<a target='_blank' href='" + result.Cells.results[6].Value + "'>" + result.Cells.results[3].Value + "</a> (" + result.Cells.results[10].Value + ")<br />";
        });
        $('#related-content-results').html(searchResultsHtml);
    }
}

function onGetSearchResultsFail(data, errorCode, errorMessage) {
    $('#related-content-results').text('An error occurred whilst searching for related content - ' + errorMessage);
}

but var results take 48 cells which doesn't contain Custom List field.

Thanks for help!

Utkarsh Dubey
  • 379
  • 1
  • 4
  • 18