1

I want to create a PowerShell script that creates Result Sources in Search Service Application.

If it's possible I would like to state the Result Sources in an XML-file with a sourceID.

How can this be done?

0

You can use below server side Powershell function to create a Result Source. I have used it in SharePoint 2013 On Premise

function CreateResultSource(){
[CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$siteUrl,
        [Parameter(Mandatory=$true)][string]$searchServiceName,
        [Parameter(Mandatory=$true)][string]$query,
        [Parameter(Mandatory=$true)][string]$resultSourceName,
        [Parameter(Mandatory=$false)][boolean]$setAsDefault,
        [Parameter(Mandatory=$false)][boolean]$setPeopleType
    )

try{

        # Get Site, SSA and owner
        #
        $site = get-spsite $siteUrl -WarningAction SilentlyContinue
        $sspApp = Get-SPEnterpriseSearchServiceApplication $searchServiceName
        $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]::SPSite, $site.RootWeb)

        # Query properties
        #
        $queryProperties = New-Object Microsoft.Office.Server.Search.Query.Rules.QueryTransformProperties

        # Check if source exists
        #
        $source = $fedManager.GetSourceByName($resultSourceName, $searchOwner)
        if ($source -eq $null)
        {
            # create result source
            #
            $resultSource = $fedManager.CreateSource($searchOwner)
            $resultSource.Name = $resultSourceName

            if ($setPeopleType)
            {
                $resultSource.ProviderId =  $fedManager.ListProviders()['Local People Provider'].Id
                Write-host  $resultSourceName "set as default 'People Search Results'" -ForegroundColor Yellow
            }
            else{
                $resultSource.ProviderId = $fedManager.ListProviders()['Local SharePoint Provider'].Id
            }

            $resultSource.CreateQueryTransform($queryProperties, $query)
            $resultSource.Commit()

            if ($setAsDefault)
            {
                try{
                    $fedManager.UpdateDefaultSource($resultSource.Id, $searchOwner)
                    Write-host  $resultSourceName "set as default result source" -ForegroundColor Yellow
                }
                catch {
                    Write-Host "Error : fail to set as default " $resultSourceName " :  " $_.Exception.Message -ForegroundColor Red
                }
            }
            Write-host "Created successfully" -ForegroundColor Green
        }
        else
        {
            write-host "Result Source exists!" -ForegroundColor Yellow

        }
    }
    catch{
        Write-Host $_.Exception.Message -ForegroundColor Red
    }

}

Usage:

CreateResultSource "http://YourSite.com" "Search Service App Name" "Your KQL Query" "Your Result Source Name"  $False $False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.