Share via


SharePoint 2013 : Execute SharePoint Search Queries using CSOM

In this article we will explore the method of executing Search Queries using SharePoint Client Side Object Model. The code sample used in this article can be used in any kind of solution (Farm, Sandbox) or any kind of App (SharePoint Hosted, Cloud Hosted) executing in context of SharePoint.

Prerequisites

  1. Fiddler Web Proxy should be installed. You can get Fiddler from http://www.telerik.com/download/fiddler

Like in other of the earlier articles we will start with creating new SharePoint Hosted App.

https://howtodowithsharepoint.files.wordpress.com/2015/01/1.png?w=450&h=312

The next thing is to provide a simple UI for our implementation which allows the users to specify the Search Terms & execute the queries.

In order to keep the story simple we will take one input box to specify the Search Terms & one button to execute the query as shown below.

https://howtodowithsharepoint.files.wordpress.com/2015/01/2.png?w=450&h=68 https://howtodowithsharepoint.files.wordpress.com/2015/01/31.png?w=450&h=51

Now once we are done with the UI of the App, the next thing is to prepare a module that will execute the Search Queries and receive the response.

https://howtodowithsharepoint.files.wordpress.com/2015/01/4.png?w=450&h=234

Following is the detail inspection of the above code snippet

  1. Initialize SharePoint Context variable (Step 1) then we need to
  2. Initialize the Query Object (Step 2) using the context variable created in Step 1.
  3. Set the Query Text (Step 3). This query text has been specified in the input box by the user executing the query.
  4. Initialize the Search Executor Object (Step 4) using the context variable created in Step 1. This object is solely responsible to execute Search Queries. This object contains a method “executeQuery” which takes query text as its parameter.

Now the next challenge is to identify how to code the next step, as it is dependent on response coming from the SharePoint REST End Point in return of the request executed in Step 4.

So the trick to first reviews the response coming from SharePoint using any Web Proxy. Being a big fan of Fiddler I must insist to make use of it to the purpose.

Below image is showing some sample data present in one of the lists called “Customer”.

https://howtodowithsharepoint.files.wordpress.com/2015/01/5.png?w=450&h=105

For the sake of demo let’s pick “ALFKI” as Search Term. On clicking the “Executing Search using CSOM” , the Search Query gets issued to SharePoint End Point and if it is a success we will get the response for sure.

https://howtodowithsharepoint.files.wordpress.com/2015/01/6.png?w=450&h=77

If we investigate the response in Fiddler we can see the data structure returned as a part of the response.

https://howtodowithsharepoint.files.wordpress.com/2015/01/7.png?w=450&h=204

Based on this observation, we can read the data as shown in Step 5 above.

Once we receive the search results, it is just the matter of choice on how to display it. For the sake of this demo I am just displaying the search results in a div already added to the App UI.

https://howtodowithsharepoint.files.wordpress.com/2015/01/8.png?w=450&h=90

And that’s it. We are all done here.

Though the scenario I mentioned here is quite simple, but the presence of Client Side Search APIs in SharePoint 2013 is proved to be very powerful and could be utilized to cater complex business requirements without using any custom code.

Hope this will help someone in need.