SharePoint Migration – Restricting Search by site collection using KQL in Sharepoint 2013

SharePoint 2013 Search Migration

Here’s an interesting episode that caught me engaged for a few days. Due to an overhaul in search scope in SharePoint 2013, we had to customize the whole web-part and devise methods in SharePoint 2013 environment to meet end users need.

SharePoint Migration Services
SharePoint Migration 2010 to 2013

Earlier in SharePoint 2010, we created a custom search web part for our clients that would provision search amidst Multi-site collections. As our client migrated to SharePoint 2013, the same methods were not applicable due to changes in search scope with certain restrictions in SharePoint 2013 milieu. So we leveraged KQL (Keyword Query Language) attributes to categorise site collections and facilitated attribute based search in SharePoint 2013 environment. Here’s the code we developed while implementing the changes.

using System;
 using Microsoft.SharePoint.Client;
 using Microsoft.SharePoint.Client.Search.Query;
 namespace SearchTest
 {
 class Program
 {
 static void Main(string[] args)
 {
 using (var context = new ClientContext("http://localhost"))
 {
 var query = new KeywordQuery(context)
 {
 QueryText = "test",
 HiddenConstraints = "Department",
 };
 var executor = new SearchExecutor(context);
 var results = executor.ExecuteQuery(query);
 context.ExecuteQuery();
 if(results!=null)
 foreach (var result in results.Value[0].ResultRows)
 {
 Console.WriteLine(result["Title"]);
 }
 }
 }
 }
 }

Author