Create a Content Source Scope Rule in SharePoint 2010 using PowerShell


In this article we will be seeing how to create a Content Source Scope Rule in SharePoint 2010 Enterprise Search Service Application using PowerShell.

Content Source Scope Rule creation in SharePoint 2010

We can create a Content Source Scope Rule in SharePoint 2010 Enterprise Search Service Application from Central Administration.

Go to Central Administration => Application Management => Manage Service Applications => Enterprise Search Service Application.

CrtScpSil1.gif

On the Quick Launch Menu, go to Queries and Results then click on Scopes.

CrtScpSil2.gif

You could be able to see the Scopes.

CrtScpSil3.gif

Click on the Scope to which Scope Rule has to be added.

Click on Add New Rule link to add the new scope rule.

CrtScpSil4.gif

In the Scope Rule Type section, select Content Source.

In the Content Sources section, select the Content Source that has to be added to the scope rule.

Click on Ok.

CrtScpSil5.gif

Automating the Content Source Scope Rule creation in SharePoint 2010

Here we will be seeing about automating the Content Source Scope Rule creation using powershell script.

Steps Involved:

  1. Create the input XML file which contains the inputs for Content Source Scope Rule creation.
  2. Create ps1 file which contains the script for Content Source Scope Rule creation.

CreateContentSourceScopeRules.xml

<?xml version="1.0" encoding="utf-8" ?>
<ScopeRules>
  <
SSAName>EnterPrise Search Service Application</SSAName>
  <ScopeURL>http://server06:8080/</ScopeURL>
  <Rule ScopeName="Sample1" ContentSourceName="Sample1 SharePoint Sites" /> 
  <Rule ScopeName="Sample2" ContentSourceName="Sample2 SharePoint Sites" />
  <Rule ScopeName="Sample3" ContentSourceName="Sample3 SharePoint Sites" />
</ScopeRules>

CreateContentSourceScopeRules.ps1


 #----------------Get the xml file---------------------------------------------------------------

 [xml]$xmlData=Get-Content "C:\Users\Desktop\ContentSources\CreateContentSourceScopeRules.xml"

 #----------------Create Content Source Scope Rule function---------------------------------------------

Function CreateContentSourceScopeRule()
{
    
$flag=0     
       
$ssa=Get-SPEnterPriseSearchServiceApplication -Identity $xmlData.SSAName
       
$ssaContent = new-object Microsoft.Office.Server.Search.Administration.Content($ssa)
       
$scopeCollection=Get-SPEnterpriseSearchQueryScope  -SearchApplication $ssa
       $xmlData.ScopeRules.Rule | ForEach-Object {
             
$ScopeName=$_.ScopeName
             
$contentSourceName=$_.ContentSourceName
             
$scope=Get-SPEnterpriseSearchQueryScope -SearchApplication $ssa  | Where-Object {$_.Name -eq $ScopeName}
             
if($scope -ne $null)
             {
                   
foreach ($availableContentSource in $ssaContent.ContentSources)
                {
                       
if ($availablecontentSource.Name.ToString() -eq $contentSourceName)
                        {
                               
$flag=1
                               
break
                          }
                   }

                     if($flag -eq 1)
                     { 
                        
$ScopeRules=Get-SPEnterpriseSearchQueryScopeRule -Scope $scope | Where-Object {$_.Value -eq $contentSourceName}                
                          
if($scopeRuleFlag -ne $null)
                           {
                                
write-host -f Yellow "Can't add or modify scope rule. A rule with the same parameters already exists in this
scope"
                                
                           }

                           else
                           {
                                
write-host -f Magenta Creating Content Source Scope Rule
                                 New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty ContentSource -PropertyValue $contentSourceName -FilterBehavior Include -URL $xmldata.ScopeRules.ScopeURL -scope $scope -SearchApplication $ssa
                                 write-host -f Green Content Source Scope Rule is created successfully to $ScopeName scope
                           }                      
                     }
                    
else
                     {
                          
write-host -f Yellow $contentSourceName content source does not exists                           
                     }
             }
             
else
             {
                   
write-host -f yellow $ScopeName scope does not exists                  
             } 
       }
}
 
#----------------Calling the function---------------------------------------------

 CreateContentSourceScopeRule

Run the Script:

  1. Go to Start.

  2. Click on All Programs.

  3. Click on Microsoft SharePoint 2010 Products and then click on SharePoint 2010 Management Shell.

  4. Run the C:\Users\Desktop\ContentSources\CreateContentSourceScopeRules.ps1

Output:

CrtScpSil6.gif

And in the Central Administration you could see the newly added Content Source Scope Rule as shown in the following

CrtScpSil7.gif

Up Next
    Ebook Download
    View all
    Learn
    View all