| By Corey Roth | Article Rating: |
|
| September 11, 2012 12:41 AM EDT | Reads: |
2,013 |
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.
Read the original blog entry...
Published September 11, 2012 Reads 2,013
Copyright © 2012 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Corey Roth
Corey Roth, a SharePoint Server MVP, is a consultant at Infusion specializing in SharePoint for clients in the energy sector. He has more than ten years of experience delivering solutions in the energy, travel, advertising and consumer electronics verticals.
Corey specializes in delivering ECM and search solutions to clients using SharePoint. Corey has always focused on rapid adoption of new Microsoft technologies including Visual Studio 2010, .NET Framework 4.0, and Silverlight.
He is a member of the .NET Mafia (www.dotnetmafia.com) where he blogs about the latest technology and SharePoint. He is dedicated to the community and speaks regularly at user groups and SharePoint Saturdays.
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: Delivering Digital Marketing on the Cloud
- AWS Going into a New Line of Work
- Session Topics: 12th Cloud Expo / Cloud Expo New York
- Five Big Data Features in SQL Server
- How Bon-Ton Stores Align Business Goals with IT Requirements
- Amazon Cuts Prices on S3
- Cloud Conversations: AWS EBS, Glacier and S3 Overview | Part 2 S3
- Cloud Conversations: AWS EBS, Glacier and S3 Overview | Part 3
- Compuware Signs New APM Partnership
- Google Submits Concessions to EC; Gets Sued in the UK
- GenieDB Makes MySQL Web-Scale & Always Available
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: Delivering Digital Marketing on the Cloud
- AWS Going into a New Line of Work
- Session Topics: 12th Cloud Expo / Cloud Expo New York
- Help Desk Solution Empowers Employees
- Five Big Data Features in SQL Server
- Big Data Is Not Just About Marketing: Don’t Forget the IT Department’s Needs
- How Bon-Ton Stores Align Business Goals with IT Requirements
- A Cloud-Based Testing Tool for the Budget-Minded
- Top Considerations for Your Hybrid Cloud Environment
- Componentizing Applications with Layered Architecture
- From ESBs to API Portals, an Evolutionary Journey | Part 2
- Where Are RIA Technologies Headed in 2008?
- Processing XML with C# and .NET
- AJAX World RIA Conference & Expo Kicks Off in New York City
- JSON vs XML - A Jason vs Freddie Sequel
- The Top 250 Players in the Cloud Computing Ecosystem
- Has the Technology Bounceback Begun?
- BPEL Processes and Human Workflow
- i-Technology Viewpoint: The Very Confused World of 3D and XML
- Generating XML from Relational Database Tables
- "HP's Problem Ain't the SAP Install," Says Sun's Schwartz
- Open Source Database Special Feature: An Introduction to Berkeley DB XML
- eXist - An Introduction To Open Source Native XML Database






















