Create Result Source In SharePoint 2013 Search Using PowerShell

This blog post describes how to create a search result source using PowerShell in SharePoint 2013. I have a requirement to add additional search rules which will be used with the keywords entered by the user in the search box, then you will need to create custom search result sourc

sample query 

The query will be,

{searchTerms} Author={User.Name} FileType=pdf FileType=docx

Source Code
  1. Add-PSSnapin Microsoft.SharePoint.PowerShell  
  2.     
  3. $SearchServiceApp = Get-SPEnterpriseSearchServiceApplication  
  4. $fedmanager = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($SearchServiceApp)  
  5.     
  6. $searchOwner = Get-SPEnterpriseSearchOwner -Level Ssa    
  7.     
  8. $resultSourceName = "Search"  
  9. $urlTemplate = "http://yoursharepointsitename/search?q={searchTerms}&format=rss&Market=en-Us"  
  10. $qT = "{searchTerms}"  
  11. $ResultSource = $fedmanager.GetSourceByName($resultSourceName, $searchOwner)  
  12. if(!$ResultSource){  
  13.     Write-Host "Result source does not exist. Creating."  
  14.     $ResultSource = $fedmanager.CreateSource($searchOwner)  
  15. }  
  16. else { Write-Host "Using existing result source." }  
  17.     
  18. $ResultSource.Name =$resultSourceName  
  19. $ResultSource.ProviderId = $fedmanager.ListProviders()['OpenSearch Provider'].Id  
  20. $ResultSource.ConnectionUrlTemplate = $urlTemplate  
  21. $ResultSource.CreateQueryTransform($queryProperties,$qT)  
  22. $ResultSource.Commit()  
Conclusion

Was my blog helpful? If yes, please let me know and if not, please explain what was confusing or missing. I’ll use your feedback to double-check the facts, add info, and update this blog.