SharePoint Pals
 | Sign In
How to Create Result Source Programmatically in SharePoint 2013
by Sathish Nadarajan 14 Jul 2014
SharePoint MVP
Today  :   12     Total  :    16847

In this article, let us see, how to create a result source Programmatically in SharePoint 2013. I am taking C# as my Language here.

Before starting into the coding, let us have a glance about the Result Source.

What is a Result Source?

It is a precompiled Query, on top of that, we can execute our Query. In detail, when a crawl is happening, all the data will be indexed on the Search Database. When we execute our own query, the query will be executing against the Search Database and retrieve the value. Here, the result source plays the role of a subset. i.e., the crawled data can be grouped into a much smaller dataset based on a condition.

To explain with a help of actual scenario, I have a SharePoint Farm, which is having a huge number of Document Libraries, List Items etc., In this scenario, when I execute any Keyword Query, it will get executed against the whole items and retrieve you the values appropriately. Instead, what we can do is, we can create a Result source called “My Documents” which will be having all the Document Library Items alone.

Then when we execute the query, it will get executed against all the Documents alone.

Result sources can be created at the Search service application level, site collection level, or site level. This enables Search service application administrators, site collection administrators, and site owners to create and use result sources to meet their specific requirements for providing search results to users. When you create a result source at the Search service application level, for example, the result source is available to any query rule that is created at the same level, and also to any query rule that is created for a site collection or site that is in a web application that consumes that Search service application.

With this brief explanation, let us see the Result Source in the SharePoint Site itself.

Before proceeding with Result Sources, let me add a Content Search WebPart on my Page.


On the change Query Button,


We can see the below screen.

Go to Advanced Mode.


The default Result Sources are listed on the drop down.



1. Conversation,

2. Local People Results

3. Local Reports and Data Results

4. Local SharePoint Results

Now, we are planning to add a new Result Source here.

To Create from the Site, go to Site Settings – Result Sources


On the screen click New Result Source


The values am providing are shown in the below screens.


I am launching the Query Builder.


In the Query text, we can create the subset of the result source.

Write the sample query as shown in the figure and Click Ok.


That’s it. We have created our Result Source.

Now, let me go back to my Content Search Webpart.

Edit the Query.


The newly created Result Source will be listed on the Drop down.

Now, whatever the Query, we write, it will get executed against that subset data only.

This is such a powerful tool when considering the Performance of Search. Instead of querying the entire Search Service DB, we can only fetch within the subset.

Now, let us see, how to create the same result source Programmatically (C#)

 Public static void CreateResultSource()
 SPSite publishingSite = new SPSite(“http://sathishserver:3000/sites/PublishingSite”);
 Microsoft.SharePoint.SPServiceContext context = SPServiceContext.GetContext(publishingSite);
 SearchServiceApplicationProxy searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
 FederationManager fedManager = new FederationManager(searchProxy);
 SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.SPSite, publishingSite.RootWeb);
 Source currentResultSource = fedManager.CreateSource(owner);
 currentResultSource.Name = “My Program Result Source”;
 currentResultSource.Description = “Description about my Result Source”;
 currentResultSource.ProviderId = fedManager.ListProviders()["Local SharePoint Provider"].Id;
 Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties QueryProperties = new Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties();
 String resultSourceQuery = “{searchTerms} path:””http://sathishserver:3001/sites/Publishing””;
 currentResultSource.CreateQueryTransform(QueryProperties,resultSourceQuery );

That’s it. We have created the ResultSource Programmatically. In the same manner, to delete,

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
                 using (SPSite publishingSite = properties.Feature.Parent as SPSite)
                     Microsoft.SharePoint.SPServiceContext context = SPServiceContext.GetContext(publishingSite);
                     SearchServiceApplicationProxy searchProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
                     FederationManager fedManager = new FederationManager(searchProxy);
                     SearchObjectOwner owner = new SearchObjectOwner(SearchObjectLevel.SPSite, publishingSite.RootWeb);
                     DeleteResultSource(“My Program Result Source”, fedManager, owner);
         private static void DeleteResultSource(string resultSourceName,FederationManager fedManager, SearchObjectOwner owner)
             Source toDelete = fedManager.GetSourceByName(resultSourceName, owner);
             if (toDelete != null)

Happy Coding.

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation