SharePoint Enterprise Search–Searching with Scope


This blog post demonstrates a sample that queries against to a specific site in my SharePoint Online portal.


SharePoint Server 2013 supports building search queries in 2 languages: Keyword Query Language (KQL) and FAST Query Language (FQL). Yet, KQL is selected default in SP2013.  

The solution demonstrated below searches a SharePoint site by passing a KQL query and consists of the following components:

    – SharePoint site: My Office365(o365) SharePoint Online(SPO) site where:

      – Search Service Application (SSA) functional. For details, you can visit Create and configure a Search service application in SharePoint Server 2013
       Managed Metadata Service (MMS) enabled and manage terms created. I have created a term set named Colors in my term store with these values: {Blue, Green, Orange, Purple, Red, Yellow}.    Details, you can visit Overview of managed metadata service applications in SharePoint Server 2013
      – A result source defined for a site (or sub site).  I have outlined its configuration steps just below.

    – Console application – the client: where I use Client Side Object Model (CSOM) search API for querying my SPO site with managed properties.


Steps for configuring the result source

Steps are really straight-forward here; once signed in, you can follow the steps displayed below.


Note that:

    Use of a result source will benefit you from search scope perspective. The client API uses its id (Guid) which can be obtained from the browser as highlighted in the last picture above.


Implementation – source code

    First it creates the client context with my own credentials.

    It builds KeywordQuery object’s QueryText property as {Property}:{theValue} and then constructs SearchExecutor and call its ExecuteQuery method passing the keyword query object just created.

    It sets SourceId property of KeywordQuery object to the result source id.

    Then it displays the results on the screen


Query related source code shared below.

   1: using System;

   2: using Microsoft.SharePoint.Client;

   3: using Microsoft.SharePoint.Client.Search;

   4: using Microsoft.SharePoint.Client.Search.Query;


   6: using SharePointContext;


   8: namespace Console1

   9: {

  10:     class Program

  11:     {

  12:         static ClientContext _clientContext;


  14:         static void Main(string[] args)

  15:         {

  16:             try

  17:             {                

  18:                 CloudClientContext ccc = CloudClientContext.Instance;

  19:                 ccc.Password = System.Configuration.ConfigurationManager.AppSettings["sp_pwd"];

  20:                 ccc.UserName = System.Configuration.ConfigurationManager.AppSettings["sp_tenant"];

  21:                 ccc.SiteUrl = System.Configuration.ConfigurationManager.AppSettings["sp_siteUrl"];


  23:                 _clientContext = ccc.GetContext();


  25:                 DoSearch();

  26:             }catch (Exception ex)

  27:             {

  28:                 string str = ex.Message;

  29:             }

  30:         }


  32:         static void DoSearch()

  33:         {

  34:             KeywordQuery kq = new KeywordQuery(_clientContext);

  35:             kq.QueryText = GetKeywords();

  36:             kq.SourceId = Guid.Parse(System.Configuration.ConfigurationManager.AppSettings["sp_sitesourceId"]);   


  38:             SearchExecutor searchExecutor = new SearchExecutor(_clientContext);


  40:             ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(kq);


  42:             _clientContext.ExecuteQuery();


  44:           foreach (var resultRow in results.Value[0].ResultRows)

  45:             {

  46:                 Console.WriteLine("#{0}:{1}rn ({2})t {3}", 

  47:                     resultRow["piSearchResultId"].ToString().Split('_')[0], 

  48:                     resultRow["Title"], 

  49:                     resultRow["Path"], 

  50:                     resultRow["Write"]);

  51:             }


  53:             Console.ReadLine();

  54:         }


  56:         static string GetKeywords()

  57:         {

  58:             return string.Format("'Color':'{0}'","Blue");

  59:             //return string.Format("author:{0} AND filetype:{1} OR 'Color':'{2}'", "Mecit Atmaca", "docx", "Orange"); //more granular search

  60:         }

  61:     }

  62: }


Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.