Create a Content Source Scope Rule in SharePoint 2010 using PowerShell

Anavijai
Posted by in SharePoint category on for Intermediate level | Points: 250 | Views : 8552 red flag
Rating: 4 out of 5  
 1 vote(s)

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

Introduction

In the previous article, we learnt Business Data List Web Part in SharePoint 2010. 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 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.

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

You could be able to see the Scopes.

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

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

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.

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>AZ ESP EnterPrise Search Service Application</SSAName>

<ScopeURL>http://ukslazspddev06: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:

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

Hope this article was useful, keep reading my forthcoming article of this series.

Thanks for reading. In the next article we shall learn Create Content Sources in SharePoint 2010 using PowerShell.

Page copy protected against web site content infringement by Copyscape

About the Author

Anavijai
Full Name: Vijai Anand R
Member Level: Starter
Member Status: Member
Member Since: 5/22/2011 5:12:38 AM
Country: India
Vijai Anand.R
http://www.dotnetfunda.com
SharePoint Developer

Login to vote for this post.

Comments or Responses

Posted by: debal_saha-9451 on: 10/17/2011 | Points: 25
HI,
This is very interesting article , but to be honest I want to say that I don't know what is sharepoint . How to do work , From where and what software to need install in my computer , I have VS2010, Can I learn sharepoint in it .

If you help me , so that i can study and understand sharepoint. Please also tell me why to need sharepoint ?
Is this means that to share important files, document reliable through it .

As a developer Should I learn SharePoint?

Login to post response

Comment using Facebook(Author doesn't get notification)