How to: Query Search with the SharePoint 2013 Client Object Model
Posted
Monday, September 10, 2012 11:41 PM
by
CoreyRoth
When I first gave the Preview of Search in SharePoint 2013, I mentioned that we could now query search using the Client Object Model. After enough digging through the documentation, I finally pieced together the steps required to get search results back using this new API. Today, I will demonstrate how to query from a console application. 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.
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
- Microsoft.SharePoint.Client.Search.dll
Now in our console application, we need to add the necessary references.
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search;
using Microsoft.SharePoint.Client.Search.Query;
This next step should be familiar if you have used the Client Object Model before in SharePoint 2010. Start by creating a ClientContext object and pass in the URL to a site. Put this in a using block.
using (ClientContext clientContext = new ClientContext("http://servername"))
We then need to create a KeywordQuery class to describe the query. This class is similar to the server side KeywordQuery class but there are some differences. We pass the ClientContext into the constructor.
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
To set the query use the QueryText property. In this case, I am doing a search for the keyword “SharePoint”.
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. We pass a ClientContext to it as well:
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
To execute the query, we use the ExecuteQuery method. It returns a type of ClientResult<ResultTableCollection>.
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.
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, I write out the Title, Path, and Modified Date (Write).
foreach (var resultRow in results.Value[0].ResultRows)
{
Console.WriteLine("{0}: {1} ({2})", resultRow["Title"], resultRow["Path"], resultRow["Write"]);
}
That’s it. Here is what the entire class looks like together.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search;
using Microsoft.SharePoint.Client.Search.Query;
namespace SearchClientObjectModelTest
{
class Program
{
static void Main(string[] args)
{
using (ClientContext clientContext = new ClientContext("http://sp2010"))
{
KeywordQuery keywordQuery = new KeywordQuery(clientContext);
keywordQuery.QueryText = "SharePoint";
SearchExecutor searchExecutor = new SearchExecutor(clientContext);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
foreach (var resultRow in results.Value[0].ResultRows)
{
Console.WriteLine("{0}: {1} ({2})", resultRow["Title"], resultRow["Path"], resultRow["Write"]);
}
Console.ReadLine();
}
}
}
}
As you can see, you can get started with Search using the Client Object Model with relatively few lines of code. This certainly beats, constructing that nasty XML file that the Search Web Service takes.
I cross-posted the Visual Studio Solution to code.msdn.microsoft.com.
Follow me on twitter: @coreyroth.