Share point access using search API


preferred the below link- 

and followed all the step which has mentioned in that link. But I got the below error.


 here I have changed some code - 

 Orignal Code  -  SPSearchResults.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + query + "'";

Modified code -  SPSearchResults.url = "" + "/_api/search/query?querytext='" + query + "'";

Please let me know the reason and do we need to add any Js in my project. If yes please provide the java script file for that.

And also I want to know about the below tag


Is this the server name ?



  • Edited by Manoj 1982 Wednesday, May 20, 2015 11:28 AM
May 20th, 2015 10:45am


You need to include jQuery library reference before using this script. You can use direct link to cdn or from a document library.

Your code should look like below-

<script  type="text/javascript" src=""></script>
<script type="text/javascript">

//Your code goes here

And as far as "_spPageContextInfo.webAbsoluteUrl " is concerned, this is very important piece of code as it provides absolute we URL of your site.

For more information, check these links-

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 5:45am

Hi Danny,

Thanks for your reply.

Also I want to set proxy with credentials like user name and password in this code. Please suggest the code for that. Without proxy we cant hit the url.

And I am also paste my code. Kindly check my code.

Javascript file



function getSearchResultsUsingREST() {
    // search can use the app web URL as the base, so no need to use SP.AppContextSite(@target) to access the host web..
     var queryText = $("#searchBox").val();
    var searchUrl = "" + "/_api/search/query?querytext='" + queryText + "'";
   var executor = new SP.RequestExecutor("");
            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 />";

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

And HTML page 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
    <script src="../resource/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="../resource/jquery-1.8.3.min.js" type="text/javascript"></script>
      <script src="../resource/SP.RequestExecutor.js" type="text/javascript"></script>
    <script src="../share.js" type="text/javascript"></script>

    <form action="">
            <td>Search Text</td>
            <td> <input type="text" name="Search" value="" id="searchBox"/></td>
                <input type="button" value="Search" onclick="getSearchResultsUsingREST()"/>
     <div id="searchResults"></div>



May 22nd, 2015 1:33am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics