Tuesday, December 4, 2012

Importing BestBets from CSV file - Search Center Site


Importing BestBets from CSV file

function Import-SearchKeywords($SiteUrl, $CSVFilePath,[switch]$RemoveOldKeywords)

{
#Fetching search service application
$ssap = Get-SPEnterpriseSearchServiceApplicationProxy -Identity "Sample Search Service Application"
write-host $ssap
#Defining the object for the keyword
$keywords = New-Object Microsoft.Office.Server.Search.Administration.Keywords($ssap, $SiteUrl)
write-host $keywords

#Assigning all keywords to a member variable
$allKeywords = $keywords.AllKeywords
write-host $allkeywords

#Reading current date stamp
$date = Get-Date
write-host $date

#Remove all previous keywords from the site collection if chosen
if($RemoveOldKeywords) {
$keywordsArray = @()
$allKeywords
ForEach-Object {
$keywordsArray = $keywordsArray + $_.Term
}
$keywordsArray
ForEach-Object {
write-host "Deleting keyword:"$_
$allKeywords[$_].Delete()
}
}
#Import CSV file
$csvData = Import-Csv $CSVFilePath
ForEach-Object {

#Create keyword
write-host "Importing keyword:"$_.BestBetKey
$bestbetkeyvalue = $_.BestBetKey
$BestBetKey = $allKeywords.Create($_.BestBetKey,$date.AddHours(0))
#$BestBetKey.Definition = $_.Definition
#Create synonyms
$synonymArray = @()
$synonymArray = $_.SynonymRing.Split(";")
$synonymArray
ForEach-Object {
write-host "Creating synonym"$_ "for keyword" $BestBetKey.Term
if (($_ -ne $null) -and ($_ -ne "")) {
try
{
$BestBetKey.Synonyms.Create($_)
}
catch
{
write-host "Synonym already existed"$_
}
}
}
#Create best bets
$bestBetColumn = $_.BestBetTitle
$descriptionColumn = $_.BestBetAbstract
$urlColumn = $_.BestBetURL
if (($bestBetColumn -ne "") -and ($urlColumn -ne ""))

{
try
{
write-host "Creating best bet"$bestBetColumn "for keyword" $BestBetKey.Term
$BestBetKey.BestBets.Create($bestBetColumn,$descriptionColumn, $urlColumn)
}
catch
{
}
}
#Update keyword with best bets
$BestBetKey.Update()
}
}
Import-SearchKeywords -SiteUrl http://abc:34842 -CSVFilePath C:\FinalCSV.csv –RemoveOldKeywords



No comments:

Post a Comment