Create and Delete Result Source in SharePoint 2013 using PowerShell


Sathish Nadarajan
SharePoint MVP
Published On :   16 Jul 2014
Visit Count
Today :  8    Total :   16341
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


In the last article, we saw how to create the Result Source using C# and through the Central Administration. Now, let us see, how to create the same using PowerShell. The script is self-explanatory. There is nothing much to explain on the script as well as about the Result Source as we discussed much about result source on the previous article itself.

Create Result Source


 ################## Create Result Source #####################
 
 $publishingSite = Get-SPSite "http://c4968397007:1000/"
 $sspApp = Get-SPEnterpriseSearchServiceApplication;
 
 # load Search assembly
 
 [void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")
 
 # create manager instances
 
 $fedManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($sspApp)
 $searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::Ssa, $publishingSite.RootWeb)
 
 # define query
 
 $query = '{searchTerms?}'
 $queryProperties = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties
 # define custom sorting
 $sortCollection = New-Object Microsoft.Office.Server.Search.Query.SortCollection
 $sortCollection.Add("Title", [Microsoft.Office.Server.Search.Query.SortDirection]::Ascending)
 $queryProperties["SortList"] = [Microsoft.Office.Server.Search.Query.SortCollection]$sortCollection
 
 # create result source
 $resultSource = $fedManager.CreateSource($searchOwner)
 $resultSource.Name = 'Result Source Through PowerShell'
 $resultSource.ProviderId = $fedManager.ListProviders()['Local SharePoint Provider'].Id
 $resultSource.CreateQueryTransform($queryProperties, $query)
 
 $resultSource.Commit()
 
 ################## Delete Result Source #####################
 
 $publishingSite = Get-SPSite "http://c4968397007:1000/"
 $sspApp = Get-SPEnterpriseSearchServiceApplication;
 
 # load Search assembly
 
 [void] [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")
 
 # create manager instances
 
 $fedManager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($sspApp)
 $searchOwner = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectOwner([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::Ssa, $publishingSite.RootWeb)
 $resultSource = $fedManager.GetSourceByName("Result Source Through PowerShell",$searchOwner)
 
 #The below lines are used to List all the result Sources.  This is not relevant for deletion. 
 $searchObjectFilter = New-Object Microsoft.Office.Server.Search.Administration.SearchObjectFilter($searchOwner)
 $fedManager.ListSources($searchObjectFilter,$true)
 
 $fedManager.RemoveSource($resultSource)
 

Here one important thing with the SearchObjectLevel.

There are 4 different Levels are there.

([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]:: SPSite

([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]:: SPSiteSubscription

([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]:: SPWeb

([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]:: Ssa

We need to retrieve the result source accordingly.

Happy Coding.

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics