Friday, June 28, 2013

Create SharePoint 2010 Search Scope and Rules using Powershell

For instance, I have 3 site collections and all site collections under same database as shown below.
http://spdev5:8004 (root site colection 1)
http://spdev5:8004/sites/site2   (site collection 2)
http://spdev5:8004/sites/site3  (site collection 3)
By default when I search in the root site collection it displays results from all site collections. For ex if I search something in the root site collection it is showing data of site col2 and site col 3 as well. However if search anything in site col2 or site col3 results are limited to that site collection only. That’s fine.
Below script will create a scope in the central admin and you can see this one in the site collection as well.

In the scope I am including my root level site and excluding site collection2.


$siteURL="rootsitecolurl"
$searchServiceName="Search Service Application"
$scopeName="MyScope"
$docLibraryURL = “sitecoll2url”


 $ssa = Get-SPEnterpriseSearchServiceApplication –Identity $searchServiceName | New-SPEnterpriseSearchQueryScope -Name $scopeName -Description "Include root site collection and Exclude rest of the site collections"  -DisplayInAdminUI $true
$scope = Get-SPEnterpriseSearchQueryScope -SearchApplication $searchServiceName -Identity $scopeName
Write-Host $scope.NAME
$rule1 = New-SPEnterpriseSearchQueryScopeRule -RuleType "Url" -Url $siteURL -MatchingString $siteURL -FilterBehavior "Include" -UrlScopeRuleType "Folder" -scope $scope
$rule2 = New-SPEnterpriseSearchQueryScopeRule -RuleType "Url" -Url $docLibraryURL -MatchingString $docLibraryURL -FilterBehavior "Exclude" -UrlScopeRuleType "Folder" -scope $scope


Write-Host "Scope Created"

#This scope name (MyScope) you have to update in the root site collection search results page #‘OSSSearchResults.aspx’. just look for ‘Scope’ and update as ‘Scope= MyScope

# for more info 

No comments:

Post a Comment

Followers