How to Create Result Source Programmatically in SharePoint 2013


Sathish Nadarajan
SharePoint MVP
Published On :   14 Jul 2014
Visit Count
Today :  5    Total :   18549



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.

image

On the change Query Button,

image

We can see the below screen.

Go to Advanced Mode.

image

The default Result Sources are listed on the drop down.

 

image 

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

image

On the screen click New Result Source

image

The values am providing are shown in the below screens.

image

I am launching the Query Builder.

image

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.

image

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

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

Edit the Query.

image

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 );
 
 currentResultSource.Commit();
 
 }
 
 

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

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
         {
             SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 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)
             {
                 fedManager.RemoveSource(toDelete);
             }
         }
 
 
 

Happy Coding.

Sathish Nadarajan.

Categories