none
How to access search service api to get results in JSON format? RRS feed

  • Question

  • How to access search service api to get results in JSON format in asp.net web application outside SharePoint farm. It should be an app or SharePoint hosted.

    I have tried the same with the help of "http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2013/04/09/how-to-query-sharepoint-2013-using-rest-and-javascript.aspx".

    function onQuerySuccess(data) {

        var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;

     

        $("#resultsDiv").append('<table>');

     

        $.each(results, function () {

            $("#resultsDiv").append('<tr>');

            $.each(this.Cells.results, function () {

                $("#resultsDiv").append('<td>' + this.Value + '</td>');

            });

            $("#resultsDiv").append('</tr>');

        });

     

        $("#resultsDiv").append('</table>');

    }


    But we are getting data.d as undefined.

    I have checked and verify that I am getting success and data in XML format not in JSON. As I expected the JSON. Although I have specified the "JSON" in headers " headers: { "Accept""application/json; odata=verbose" } ". 

    Thanks


    Monday, February 23, 2015 10:26 AM

Answers

  • Hi,

    Per my understanding, you might want to access search service REST API and get results in JSON format using jQuery.

    The code you use is working in a SharePoint Hosted App project, to compose a working code snippet easier, you can take the demo below for a try:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
    
    _spBodyOnLoadFunctionNames.push("ready");
    
    function ready()
    {
    	var spAppWebUrl = _spPageContextInfo.webAbsoluteUrl;
    	var queryUrl = spAppWebUrl + "/_api/search/query?querytext='book'";
    	$.ajax({ 
    		url: queryUrl, 
    		method: "GET", 
    		headers: { "Accept": "application/json; odata=verbose" }, 
    		success: function (data) {        
    			console.log(data);
    			var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
    			$.each(results, function () {
    				$.each(this.Cells.results, function () {
    					console.log(this.Key+"--"+this.Value);
    				});
    			});
    	    },
    	    error: function (data) {
    	      console.log(data);
    	    }
    	});	
    }
    </script>

    Add the code into a page of your site, it will retrieve JSON format data and print the search result in the console of browser.

    More information about how to work with SharePoint Hosted App:

    https://msdn.microsoft.com/en-us/library/office/fp142379.aspx

    If you want to call the search service REST API in a remote environment, authentication is required to be taken into consideration, the link below with a working demo would be helpful:

    https://pholpar.wordpress.com/2013/05/11/accessing-office-365-sharepoint-sites-using-rest-from-a-local-html-javascript-host/

    Best regards


    Patrick Liang
    TechNet Community Support


    Tuesday, February 24, 2015 7:46 AM
    Moderator