CSOM, REST and SharePoint 2013 Search

SharePoint 2013 introduce couple of new ways to access Search Feature in custom applications.  SharePoint 2010 provided the Served Side Search Object Model and Search.asmx web service which allowed us to use Search in Custom apps but there was no access from client.  Now you can call Search from either CSOM or REST.  Even at this moment there is not enough information available out there to completely understand and demo the feature.  But I was able to make it work.

CSOM is more traditional approach if you are developing application for .NET, Silverlight.  Simply Add Reference to

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll

These assemblies are necessary as the base implementation of CSOM is part of these assemblies.

Additionally you have to add reference to SharePoint 2013 Search Client OM DLL. which is

Microsoft.SharePoint.Client.Search.dll

Now you can write the C# code to Query and Parse the Result.

using (ClientContext context = new ClientContext(new Uri("http://intranet.contoso.com/sites/Search/")))

{

        context.Credentials = System.Net.CredentialCache.DefaultCredentials;

        KeywordQuery keywordQuery = new KeywordQuery(context);

        if (textBox1.Text.Length == 0)

        {

            MessageBox.Show("Please enter Title in Text Box to Save.");

            return;

        }

        keywordQuery.QueryText = textBox1.Text;

 

        SearchExecutor searchExec = new SearchExecutor(context);

        ClientResult<ResultTableCollection> Results = searchExec.ExecuteQuery(keywordQuery);

        context.ExecuteQuery();

 

        if (Results != null)

        {

            if (Results.Value[0].RowCount > 0)

            {

                DataTable dt = new DataTable();

 

                dt.Columns.Add("Title");

                dt.Columns.Add("Path");

                dt.Columns.Add("Write");

                dt.Columns.Add("Author");

                dt.Columns.Add("Rank");

 

                foreach (var Row in Results.Value[0].ResultRows)

                {

                    DataRow dr = dt.Rows.Add();

                    if (Row["Title"] != null)

                    {

                        dr[0] = Row["Title"].ToString();

                    }

                    else

                        dr[0] = Row["AccountName"].ToString();

 

                    dr[1] = Row["Path"].ToString();

                    dr[2] = Row["Write"].ToString();

                    dr[3] = Row["Author"].ToString();

                    dr[4] = Row["Rank"].ToString();

                }

 

 

                dataGridView1.DataSource = dt.DefaultView;

            }

            else

            {

                MessageBox.Show("No Results Found for your Query " + textBox1.Text);

            }

        }

        else

        {

            MessageBox.Show("No Results Found for your Query " + textBox1.Text);

        }

        

 

 

}

As almost every CSOM method has its REST Endpoint now SharePoint 2013 now also provide an effective way to using Search using REST and oData.  First you have to make a httpRequest object using oData and make a GET request which will be passed to AJAX. 

For this sample I created a SharePoint Hosted App.  I added a Text Box and Button to the page and a Result Div that will show the output to the user  (Very Normal way in HTML apps)

“Another thing to know that this SharePoint app will not work until you give it permission to use Search.”  You have to double click on application manifest file and inside permission choose Search.

While Executing the app the app will ask for user permission to allow it to run Search. 

First you have to understand the URL convention of the Search 2013 REST.  To make Search using REST you have to using URL in the format of

SiteURL/host/_api/Search/Query?QueryText=’Text Keyword’

You can also perform a Query to get Query Suggestions using Sugges Instead of Query

SiteURL/_api/search/suggest?querytext=’keyword needed suggestion’

You can read all about Search and performing Search from here

The Result will return in JSON format which you have to parse.  The Results are included in d:RelevantResults section of the output XML.  This section include all the properties you need to display results to the end users.  the results include almost everything that you need from keywords, Tags, Image Thumbnails etc.

I will post about making Query using Fiddler and then from SharePoint App tomorrow.

About these ads

4 thoughts on “CSOM, REST and SharePoint 2013 Search

  1. It’s hard to find experienced people about this topic, however, you seem like you know what you’re talking about! Thanks

  2. Hi Jerry,

    Thanks for a nice article.

    I have one more doubt?

    Is it possible to retrieve Search Schema and Result Source using REST or CSOM?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s