In this article we will be seeing how to create Content Sources for Enterprise
Search Service Application in SharePoint 2010 using PowerShell.
Automating the Content Source creation in SharePoint 2010
A content source is used to specify what type of content is crawled, what URLs
to crawl, how and when to crawl.
We can create Content Sources for Enterprise Search Service Application in
SharePoint 2010 from Central Administration.
Go to Central Administration => Application Management => Manage Service
Applications => Enterprise Search Service Application.
On the Quick Launch Menu, go to Crawling and then click on Content Sources.
You could be able to see the Content Sources. By clicking on New Content Source
link we can create a new Content Source.
Another way is we can create the content sources for Search Service Application
using SharePoint Object Model.
Here we will be seeing about automating the content source creation using
powershell script.
Steps Involved:
- Create the input XML file which contains
the inputs for content source creation.
- Create ps1 file which contains the script
for content source creation.
CreateContentSources.xml
<?xml
version="1.0"
encoding="utf-8"
?>
<ContentSources>
<SSAName>EnterPrise
Search Service Application</SSAName>
<ContentSource
Name="Sample1
SharePoint Sites" />
<ContentSource
Name="Sample2
SharePoint Sites" />
<ContentSource
Name="Sample3
SharePoint Sites" />
</ContentSources>
CreateContentSources.ps1
#----------------Get
the xml file---------------------------------------------------------------
[xml]$xmlData=Get-Content
"C:\Users\Desktop\ContentSources\CreateContentSources.xml"
#----------------Create
New Content Source function---------------------------------------------
function
CreateNewContentSource
{
$flag=0
$ssa=Get-SPEnterPriseSearchServiceApplication
-Identity
$xmlData.SSAName
$ssaContent
=
new-object
Microsoft.Office.Server.Search.Administration.Content($ssa)
$xmlData.ContentSources.ContentSource
|
ForEach-Object
{
$contentSourceName=$_.Name
foreach
($availableContentSource
in
$ssaContent.ContentSources)
{
if
($availablecontentSource.Name.ToString()
-eq
$contentSourceName)
{
write-host
-f
Yellow
$contentSourceName
"content source already exists"
write-host
-f
Yellow
Deleting
$contentSourceName
content
source
$availablecontentSource.Delete()
write-host
-f
Green
$contentSourceName
content
source
is
deleted
successfully
write-host
-f
yellow
Creating
$contentSourceName
$ssaContent.ContentSources.Create([Microsoft.Office.Server.Search.Administration.SharePointContentSource],
$contentSourceName)
write-host
-f
green
$contentSourceName
"content source is added successfully"
$Flag=1
break
}
}
if($Flag
-eq
0)
{
write-host
-f
yellow
Creating
$contentSourceName
$ssaContent.ContentSources.Create([Microsoft.Office.Server.Search.Administration.SharePointContentSource],
$contentSourceName)
write-host
-f
green
$contentSourceName
"content source is added successfully"
}
}
}
#----------------Calling
the function---------------------------------------------
CreateNewContentSource
Run the Script:
- Go to Start.
- Click on All Programs.
- Click on Microsoft SharePoint 2010
Products and then click on SharePoint 2010 Management Shell.
- Run the C:\Users\Desktop\ContentSources\CreateContentSources.ps1
Output:
And in the Central Administration you could see the newly added content sources
as shown in the following