Share point access using search API

Hi,

preferred the below link-

http://zimmergren.net/technical/sp-2013-searching-in-sharepoint-2013-using-the-rest-new-apis 

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

{"statusText":"","status":0,"responseURL":"","response":"","responseType":"","responseXML":null,"responseText":"","upload":{"ontimeout":null,"onprogress":null,"onloadstart":null,"onloadend":null,"onload":null,"onerror":null,"onabort":null},"withCredentials":false,"readyState":4,"timeout":0,"ontimeout":null,"onprogress":null,"onloadstart":null,"onloadend":null,"onload":null,"onerror":null,"onabort":null}

 here I have changed some code - 

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

Modified code -  SPSearchResults.url = "https://myprofile.bt.com/" + "/_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

_spPageContextInfo.webAbsoluteUrl 

Is this the server name ?

Regards,

Manoj




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

Hi

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="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

//Your code goes here
});
</script>

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-

http://sympmarc.com/2013/03/26/using-_sppagecontextinfo-to-determine-the-current-sharepoint-context-in-script/

http://www.dotnetmirror.com/Articles/silverlight/38/sppagecontextinfo-javascript-variable-in-sharepoint

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();
     alert(queryText);
    var searchUrl = "https://myprofile.bt.com/sites/BTFleet-Content" + "/_api/search/query?querytext='" + queryText + "'";
   var executor = new SP.RequestExecutor("https://myprofile.bt.com");
    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 />";
        });
        $('#searchResults').html(searchResultsHtml);
    }
}

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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
    <title>shareMain</title>
    <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>

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

Regards,

Manoj

May 22nd, 2015 1:33am

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

Other recent topics Other recent topics