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.
- using Microsoft.SharePoint.Client;
- using Microsoft.SharePoint.Client.Search;
- 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:
- using(ClientContext clientContext = new ClientContext("http://sharepoint-site/"))
- {
- KeywordQuery keywordQuery = new KeywordQuery(clientContext);
- keywordQuery.QueryText = "help";
- SearchExecutor searchExecutor = new SearchExecutor(clientContext);
- ClientResult < ResultTableCollection > results = searchExecutor.ExecuteQuery(keywordQuery);
- clientContext.ExecuteQuery();
-
- Console.WriteLine(" *** Query Search with SharePoint 2013 Client Object Model *** \n");
- Console.WriteLine(" Below are the search results for your Query Text = " + keywordQuery.QueryText + "\n");
-
- foreach(var resultRow in results.Value[0].ResultRows)
- {
- Console.WriteLine("Title: {0} Path: {1} Last Modified At: {2}",
- resultRow["Title"] + "\n", resultRow["Path"] + "\n", resultRow["LastModifiedTime"]);
- Console.WriteLine("\n");
- }
- Console.ReadLine();
- }
Screenshot:
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.