Thursday 5 September 2013

Search Query APIs in SharePoint 2013

To develop the custom solution types for the search results, SharePoint 2013 Provides several query APIs for search and giving us lots of ways to access the search results.

SharePoint 2013 provides the following in addition to Server Object Model which was available in previous versions.

Clicent Object Model(CSOM)
JavaScript Object Model (JSOM)
Representational State Transfer (REST) Service.

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

Using Client APIs is one of the best practices in SharePoint 2013 when you can. Client APIs include the .NET, Silverlight, Phone, and JavaScript client object models, and the REST service. 


1) Query using the .NET Client Object Model

This enable to access search results for online, on – premises and development. The search in SharePoint 2013 CSOM is built on the SharePoint 2013 CSOM.

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 theMicrosoft.SharePoint.Search.Client.Query namespace in the Microsoft.SharePoint.Search.Client.dll.

Follow below example.

using (ClientContext _clientContext = new ClientContext("http://<<serverName>>/sites/<<siteCollectionPath>>"))
{
    KeywordQuery _keywordQuery = new KeywordQuery(_clientContext);
    _keywordQuery.QueryText = "SharePoint 2013";
    SearchExecutor _searchExecutor = new SearchExecutor(_clientContext);
    ClientResult<ResultTableCollection> _results = _searchExecutor.ExecuteQuery(_keywordQuery);
    _clientContext.ExecuteQuery();
}


2) Query using the JavaScript client object model

First we need to 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("SharePoint2013"); 
var _searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(_clientContext);  
var _results = _searchExecutor.executeQuery(_keywordQuery); 
context.executeQueryAsync(onQuerySuccess, onQueryError);
 
3) 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.


4) 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.Querynamespace, 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.

Follow below example.

using (SPSite _siteCollection = new SPSite("<serverRelativeUrl>")) 
{
    KeywordQuery _keywordQuery = new KeywordQuery(_siteCollection);
    _keywordQuery.QueryText = "SharePoint2013";
    SearchExecutor _searchExecutor = new SearchExecutor(); 
    ResultTableCollection _resultTableCollection = _searchExecutor.ExecuteQuery(_keywordQuery); 
    _resultTableCollection = _resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults); 
    ResultTable _resultTable = _resultTableCollection.FirstOrDefault(); 
    DataTable dataTable = _resultTable.Table; 
}