Creating a Content Source Scope Rule using PowerShell
Posted
Wednesday, August 11, 2010 1:33 PM
by
CoreyRoth
Two weeks ago, I posted about how to create scopes and scope rules using PowerShell. At the end of the post I mentioned that there was no rule type for creating scope rules by content type. I knew there had to be a way to do this, so I created a scope rule using the UI and then I used the Get-SPEnterpriseSearchQueryScopeRule cmdlet to return all the scope rules on my content source.
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$scope = Get-SPEnterpriseSearchQueryScope -Identity "File Share" -SearchApplication $searchapp
Get-SPEnterpriseSearchQueryScopeRule -Scope $scope
From this result, the answer was immediately obvious to me. ContentSource is a managed property and we already know how to create scope rules like that using the PropertyQuery rule type. Remember how we can query by content source? Let’s give it a try and see what happens. I am going to create a new scope and have it use my BCS content source called Products. Here is what the syntax looks like.
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$scope = New-SPEnterpriseSearchQueryScope -Name "My Scope" -Description "My scope created in PowerShell" -SearchApplication $searchapp -DisplayInAdminUI $true
New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty ContentSource -PropertyValue Products -FilterBehavior Include -url http://sp2010 -scope $scope -SearchApplication $searchapp
I’ll only mention the relevant parameters today. If you are curious about the other ones, see the previous post where I go into detail on each one. On the New-SPEnterpriseSearchQueryScopeRule method, we set the RuleType parameter to ContentSource. Then, we set the PropertyValue to the name of the content source. In my case, I set it to Products. Executing the script above, I get the following result.
It appears to have worked, but let’s verify it in the UI.
Excellent. The new scope was created with the content source rule and it has 43 items included. Now, I can pretty much script all of my search settings in PowerShell. Once you have your scopes created, if you want to create scope display groups be sure and check out the SharePoint PowerShell Community Toolkit.