Using the SharePoint 2013 search Query APIs

SharePoint 2013

Learn about the query APIs available in SharePoint 2013 that enable you to add search functionality to custom solutions and applications.

Search in SharePoint 2013 provides several query APIs, giving you lots of ways to access search results, so that you can return search results in a variety of custom solution types.

In addition to the server object model that was available in previous versions of SharePoint, Search in SharePoint 2013 also provides the following:

  • Client object model (CSOM)

  • JavaScript object model (JSOM)

  • Representational State Transfer (REST) service

Table 1 shows the APIs that you can use to program search queries and the path to the source file on the server.

Table 1. Search APIs

API name

Class library or schema and path

.NET CSOM

Microsoft.SharePoint.Client.Search.dll
%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI

Silverlight CSOM

Microsoft.SharePoint.Client.Search.Silverlight.dll
%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS\ClientBin

JavaScript CSOM

SP.search.js
%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS

REST service endpoints

http://server/_api/search/query
http://server/_api/search/suggest

Server object model

Microsoft.Office.Server.Search.dll
%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI

As a best practice in SharePoint 2013 development, use client APIs when you can. Client APIs include the .NET, Silverlight, Phone, and JavaScript client object models, and the REST service. For more information about the APIs in SharePoint 2013 and when to use them, see Choose the right API set in SharePoint 2013.

Query using the .NET client object model

Search in SharePoint 2013 includes a client object model that enables access to search results for online, on-premises, and mobile development. The Search in SharePoint 2013 CSOM is built on the SharePoint 2013 CSOM. Therefore, your client code first needs to access the SharePoint 2013 CSOM and then access the Search in SharePoint 2013 CSOM. For more information about the SharePoint CSOM, see SharePoint 2010 Client Object Model. For more information about the ClientContext class, which is the entry point to the CSOM, see Client Context as Central Object.

For the .NET managed CSOM, get a ClientContext instance (located in the Microsoft.SharePoint.Client namespace in the Microsoft.SharePoint.Client.dll). Then use the object model in the Microsoft.SharePoint.Search.Client.Query namespace in the Microsoft.SharePoint.Search.Client.dll.

Here's a basic example.

using (ClientContext clientContext = new ClientContext("http://<serverName>/sites/<siteCollectionPath>"))
{
    KeywordQuery keywordQuery = new KeywordQuery(clientContext);
    keywordQuery.QueryText = "SharePoint";
    SearchExecutor searchExecutor = new SearchExecutor(clientContext);
    ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
    clientContext.ExecuteQuery();
}

To download an example, see the following code sample posted by SharePoint MVP Corey Roth: SharePoint 2013: Query Search with the Managed Client Object Model.

Query using the JavaScript client object model

For the JavaScript CSOM, get a ClientContext instance, and then use the object model in the SP.Search.js file.

Here's a basic example.

var clientContext = new SP.ClientContext("<serverRelativeUrl>");
var contextSite = clientContext.get_site();
var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); 
keywordQuery.set_queryText("SharePoint"); 
var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);  
var results = searchExecutor.executeQuery(keywordQuery); 
context.executeQueryAsync(onQuerySuccess, onQueryError);

To download an example, see the following code sample posted by SharePoint MVP Corey Roth: SharePoint 2013: Querying Search with the JavaScript Client Object Model.

Query using the REST service

SharePoint 2013 includes a REST service that enables you to remotely execute queries against the SharePoint 2013 Search service from client applications by using any technology that supports REST web requests. The Search REST service exposes two endpoints, query and suggest, and will support both GET and POST operations. Results are returned in either XML or JavaScript Object Notation (JSON) format.

The following is the access point for the service: http://server/_api/search/. You can also specify the site in the URL, as follows: http://server/site/_api/search/. The search service returns results from the entire site collection, so the same results are returned for both ways to access the service.

See SharePoint Search REST API overview and Retrieving query suggestions using the Search REST service for more information.

Query using the .NET server object model

Applications that use the server object model must run directly on a server that is running SharePoint 2013. The search Query server object model resides in the Microsoft.Office.Server.Search.Query namespace, which is located in Microsoft.Office.Server.Search.dll.

As in SharePoint Server 2010, you use the KeywordQuery class to define the query, and then called the Execute() method to submit the query. In SharePoint 2013, the Execute method is obsolete, and while it will still work, you should use the SearchExecutor class instead. To submit the query, call the ExecuteQuery() method, passing the instance of the KeywordQuery class in the call.

Here's a basic example.

using (SPSite siteCollection = new SPSite("<serverRelativeUrl>")) 
{
    KeywordQuery keywordQuery = new KeywordQuery(siteCollection);
    keywordQuery.QueryText = "SharePoint";
    SearchExecutor searchExecutor = new SearchExecutor(); 
    ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery); 
    resultTableCollection = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults); 
    ResultTable resultTable = resultTableCollection.FirstOrDefault(); 
    DataTable dataTable = resultTable.Table; 
}

To download an example, see the following code sample posted by SharePoint MVP Corey Roth: SharePoint 2013: Query Search with the KeywordQuery Class.

Show:
© 2015 Microsoft