Sunday, July 6, 2014

PowerShell : Create Managed Properties in SharePoint Search Service Application

A common requirement in SharePoint search is to create Managed Properties and map it to crawled properties so that it can be used in search query.
Below is the approach to write a PS script that can be used to create one or more managed properties using an input XML file.
1. Create an xml file ‘ManagedProperties.xml’ as below and provide details.




2. Write the script in any suitable editor and save it as ”ManagedProperties.ps1′
# ******************************************************************* #
# Function – ManagedProperty Creation #
# Author – Deepak Solanki [deepaksinghsolanki@gmail.com]
# ********************************************************************
######################################################
# make sure to load SharePoint snapin if not defined #######################################################[System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$snapin=”Microsoft.SharePoint.PowerShell”
if (get-pssnapin $snapin -ea “silentlycontinue”) 
{    
write-host -f Green “PSsnapin $snapin is loaded” 
}
elseif (get-pssnapin $snapin -registered -ea “silentlycontinue”) 
{    
write-host -f Green “PSsnapin $snapin is registered”     Add-PSSnapin $snapin     write-host -f Green “PSsnapin $snapin is loaded” 
}
else 
{     
write-host -f orange “PSSnapin $snapin not found” -foregroundcolor Red 
}

#################################################################
# Function definition #
function createManagedProperty($xmlFile,$searchApplicationName) 
{    
$configXml = [xml]( get-content $xmlFile)    
$searchapp = Get-SPEnterpriseSearchServiceApplication $searchApplicationName
$category = Get-SPEnterpriseSearchMetadataCategory -Identity SharePoint -SearchApplication $searchapp
 foreach($metaProp in  $configXml.managedproperties.managedproperty)
{        
Write-Host -ForegroundColor Green “Creating property: ” $metaProp.Name         $crawledPropName = $metaProp.crawlproperty           
##Check if Managed property already exist        
$ManagedProp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication  $searchapp -Identity $metaProp.name -ErrorAction SilentlyContinue
  ##Delete existing managed property        
if($ManagedProp -ne $null)         
{            
$ManagedProp.DeleteAllMappings()            
Remove-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $metaProp.name -Confirm:$false         
}   
else   
{    
##Create Managed property    
New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $metaProp.name -Type $metaProp.type -Queryable $true -Retrievable $true    
     
$ManagedProp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication  $searchapp -Identity $metaProp.name      
##Check if Crawled Property is available        
$crawledproperty = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name $crawledPropName -ErrorAction SilentlyContinue     
##Map managed property to crawl property        
if($crawledproperty -ne $null)         
{            
New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $ManagedProp -CrawledProperty $crawledproperty         
}   
else   
{    
Write-Host -ForegroundColor Red “Crawled property does not exist: ” $crawledPropName     
}           
}
}
###########################################
# Declaration of the constants            #
###########################################
$SearchApplicationName = “Search Service Application”
$configfile = “ManagedProperties.xml”
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$file = “$executingScriptDirectory/$configfile”
###Actual execution starts here###
Write-Host -foregroundcolor Green “***Script Started***”
createManagedProperty -searchApplicationName $SearchApplicationName -xmlFile $file
Write-Host -foregroundcolor Green “***Script Completed***”

3. Make sure that PS script and XML file are in same directory.
4. Ensure that you have provided correct “$SearchApplicationName” in PS script.
5. Open the PowerShell/SharePoint PowerShell console and run the PS script.
6. While Copying XML and script from this blog, verify if special characters (“-$ etc) are correct.
Happy Scripting!!!


No comments:

Post a Comment