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.

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

app

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.

code

execute

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.

module

Following is the detailed 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 onthe response coming from the SharePoint REST End Point in return of the request executed in Step 4.

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

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

Customer

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.

6

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

responce

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.

api

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

Though the scenario I mentioned here is quite simple,  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…

Read more articles on SharePoint:

Up Next
    Ebook Download
    View all
    Learn
    View all