2

I try to get search results using a javascript (jQuery) call to my on premise SharePoint search REST API but I don't get any results.

that's my js:

$.ajax({
    type: "GET",
    url: "http://my.local.domain/_api/search/query",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    data: { querytext: "'ContentType:MyContentType'", rowlimit: 1, trimduplicates: false, selectproperties: "'Title%2c+LastModifiedTime'", sortlist:"'LastModifiedTime:descending'" },
    dataType: "json"
})
.done(function( json ) {
}

jQuery call this url:

http://my.local.domain/_api/search/query?querytext=%27ContentType%3AMyContentType%27&rowlimit=1&trimduplicates=false&selectproperties=%27Title%252c%2BLastModifiedTime%27&sortlist=%27LastModifiedTime%3Adescending%27

The url I enter manually is a bit different:

http://my.local.domain/_api/search/query?querytext='ContentType:FsgNewsContentType'&trimduplicates=false&rowlimit=1&selectproperties='Title%2cLastModifiedTime'&sortlist='LastModifiedTime:descending'

It looks as if there's a problem with the encoding of the special characters.

4
  • Type that URL in your browser and check if you are getting any results? Commented Sep 24, 2014 at 15:36
  • Are you calling your javascript code from sharepoint page only? Commented Sep 24, 2014 at 15:37
  • Are you getting any error in browser? Commented Sep 24, 2014 at 19:39
  • I get an empty json or xml result but no error message Commented Sep 25, 2014 at 6:04

2 Answers 2

4

In your example since HTTP GET request is used, query parameters have to be specified in the URL. You can construct the GET request URL in two ways:

  • http://server/_api/search/query?query_parameter=value&query_parameter=value
  • http://server/_api/search/query(query_parameter=value&query_parameter=<value>)

The Search REST service supports both HTTP POST and HTTP GET requests. The following examples demonstrate how to consume Search Query REST Interface using JavaScript.

How to consume Search Query REST Interface using JavaScript

Assume the following query is used contentclass:STS_ListItem AND ContentType:Task to retrieve task items.

Search GET request

function getJson(endpointUrl,success,failure)
{
    $.ajax({
      type: "GET", 
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose"
      }, 
      url: endpointUrl, 
      success: success,
      failure: failure 
   });
}

function searchTasks(webUrl,success, failure) {
    var searchUrl = webUrl + "/_api/search/query?querytext='contentclass:STS_ListItem AND ContentType:Task'";
    getJson(searchUrl,success,failure);
}

Usage

//get and print tasks
searchTasks(_spPageContextInfo.webAbsoluteUrl,
  function(data){
      var query = data.d.query; 
      var resultsCount = query.PrimaryQueryResult.RelevantResults.RowCount;
      for(var i = 0; i < resultsCount;i++) {
          var row = query.PrimaryQueryResult.RelevantResults.Table.Rows.results[i];
          var taskName = row.Cells.results[3].Value;
          console.log(taskName);
      }   
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

Search POST request

function postJson(endpointUrl,payload,success,failure)
{
    $.ajax({
      type: "POST", 
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
      }, 
      data: JSON.stringify(payload),
      url: endpointUrl, 
      success: success,
      failure: failure 
   });
}


function searchTasks(webUrl,success, failure) {
    var searchUrl = webUrl + "/_api/search/postquery";
    var searchPayload = {
      'request': {
         '__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},
         'Querytext' : 'contentclass:STS_ListItem AND ContentType:Task'
      }  
    };     
    postJson(searchUrl,searchPayload,success,failure);
}

Usage

searchTasks(_spPageContextInfo.webAbsoluteUrl,
  function(data){
      var query = data.d.postquery; 
      var resultsCount = query.PrimaryQueryResult.RelevantResults.RowCount;
      for(var i = 0; i < resultsCount;i++) {
          var row = query.PrimaryQueryResult.RelevantResults.Table.Rows.results[i];
          var taskName = row.Cells.results[3].Value;
          console.log(taskName);
      }   
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

References

SharePoint Search REST API overview

0

I found a solution. It work if I pass the data as string (not as object) and set processData to false.

data: "querytext='ContentType:MyContentType'&rowlimit=1&trimduplicates=false&selectproperties='Title,LastModifiedTime'&sortlist='LastModifiedTime:descending'",
processData: false,
4
  • You could find the examples how perform requests to SharePoint Search REST API in my answer. Commented Sep 25, 2014 at 10:04
  • If you pass a data object (and it is an object), you must pass it to the $.ajax() request as JSON.stringify(data). Commented Sep 25, 2014 at 11:16
  • @wjervis data takes PlainObject, String or Array, see api.jquery.com/jquery.ajax Commented Sep 25, 2014 at 13:15
  • @jlai79, and yet it fails otherwise. SharePoint expects it as a string. Commented Sep 25, 2014 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.