Query Search SharePoint 2013 Client Object Model

Getting a list of search results from SharePoint 2013 sounds like a pretty trivial task which you would say the client-side object model is capable of handling.

To get started, we first need to add references to the assemblies listed below. These can be added using Nuget Package Manager in Visual Studio. If you are new to using NuGet package, refer to this post - Adding references using NuGet packages.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
  • Microsoft.SharePoint.Client.Search.dll

Next, we need to add these references in our application with below using statements.

  1. using Microsoft.SharePoint.Client;  
  2. using Microsoft.SharePoint.Client.Search;  
  3. using Microsoft.SharePoint.Client.Search.Query;  
Once you are done with the above steps, go to program.cs and write the below code in Main() method.

Code:
  1. using(ClientContext clientContext = new ClientContext("http://sharepoint-site/"))  
  2. {  
  3.     KeywordQuery keywordQuery = new KeywordQuery(clientContext);  
  4.     keywordQuery.QueryText = "help";  
  5.     SearchExecutor searchExecutor = new SearchExecutor(clientContext);  
  6.     ClientResult < ResultTableCollection > results = searchExecutor.ExecuteQuery(keywordQuery);  
  7.     clientContext.ExecuteQuery();  
  8.   
  9.     Console.WriteLine(" *** Query Search with SharePoint 2013 Client Object Model *** \n");  
  10.     Console.WriteLine(" Below are the search results for your Query Text = " + keywordQuery.QueryText + "\n");  
  11.   
  12.     foreach(var resultRow in results.Value[0].ResultRows)  
  13.     {  
  14.         Console.WriteLine("Title: {0} Path: {1} Last Modified At: {2}",  
  15.             resultRow["Title"] + "\n", resultRow["Path"] + "\n", resultRow["LastModifiedTime"]);  
  16.         Console.WriteLine("\n");  
  17.     }  
  18.     Console.ReadLine();  
  19. }  


Screenshot:

code

Note: URL details are hidden due to security & privacy concerns.

Download source code for Free

 

This is it friends. Hope you enjoyed reading and this article was of some help to you.

What do you think?

Dear Reader,
If you have any questions or suggestions, please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.

Happy Learning.

Ebook Download
View all
Learn
View all