Provided by: Corey RothSP2 Apps

Introduction

This code sample provides details on how to query Search with the new SharePoint 2013 Client Object Model.

Building the Sample

The sample is a console application.  Edit the server URL in the ClientContext constructor for your SharePoint 2013 server.  Build the solution as normal.
Description
This example demonstrates how to use the SharePoint 2013 Search Client Object Model.  To get started, we first need to add referenced to the appropriate assemblies.  These assemblies can be found in the ISAPI folder of the 15 hive.
Now in our console application, we need to add the following references:
 
C#
Edit|Remove
using Microsoft.SharePoint.Clientusing Microsoft.SharePoint.Client.Search; 
using Microsoft.SharePoint.Client.Search.Query;
Start by creating a ClientContext object and pass in the URL to a site.  Put this in a using block.
 
C#
Edit|Remove
using (ClientContext clientContext = new ClientContext(http://servername))
Then, create a KeywordQuery class to describe the query.  This class is similar to the server side KeywordQuery class but there are some differences.  Pass the ClientContext into the constructor.
 
C#
Edit|Remove
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
To set the query use the QueryText property.  For example, a search for the keyword “SharePoint”.
 
C#
Edit|Remove
keywordQuery.QueryText = "SharePoint";
Unlike the server object model, with the Client OM we have to use another class, SearchExecutor, to send the queries to the search engine.  Pass a ClientContext to it as well:
 
C#
Edit|Remove
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
To execute the query, we use the ExecuteQuery method.  It returns a type of ClientResult<ResultTableCollection>.
 
C#
Edit|Remove
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
However, the query doesn’t actually execute until you call ExecuteQuery() on the ClientContext object.  If you have done a lot of Client OM work before, you might think you need to call Load() first but it is not required.
 
C#
Edit|Remove
clientContext.ExecuteQuery();
Assuming no exception occurs, you can now iterate through the results.  The ClientResult<ResultTableCollection> will have a property called Value.  You will want to check the zero index of it to get the search results.  From there, the ResultRows collection has the data of each row.  This object is simply a dictionary object that you can use an indexer with and pass the name of the managed property.  In the example below, write out the Title, Path, and Modified Date (Write).
C#
Edit|Remove
foreach (var resultRow in results.Value[0].ResultRows) 
{ 
    Console.WriteLine("{0}: {1} ({2})", resultRow["Title"], resultRow["Path"], resultRow["Write"]); 
}
 That's it.  Review the code from the solution file for the complete listing.
 

Source Code Files

More Information

For more information, see my blog entry at DotNetMafia.com.  Follow me on twitter: @coreyroth.