To get a useful SharePoint search experience, create custom Search Facets, or target specific metadata we are going to use Managed Properties mapped with Crawled Properties. We’ve started on upgrading a new SharePoint (SP) 2010 system that we currently have and it’s going to include SP Search with Longitude. Part of the Search project is to identify and configure the Content Source(s). Another part of the Search Project is to leverage the Search Facets to help increase the user experience.
Intro
Like my previous PowerShell (PS) how-to’s, this one will leverage scripting to help me create Managed Properties. PowerShell is a powerful tool that we can use to automate a bunch of different tasks. I used PS to create a script that would
- Create new Managed Properties
- Map Crawled Properties to new Managed Properties
- Kick off a Full Crawl
**My script is designed to specifically create SharePoint Site Managed Properties and map Crawled Properties, but it can be modified to create and map other types with minimal work if needed.
This script accompanies an XML file that identifies the Managed Properties and Crawled Properties that are associated with each Source. I used this script the help me bash out multiple Managed Properties and mapping Crawled Properties at the same time.
*One caveat though is that the Crawled Properties have to exist, or the mapping will not work.
Getting Started
The first section of the script configures the connection to the to the XML document. Then it runs through each <managedproperty> element from the XML document. If it notices that there is an existing Managed Property, it adds the Crawled Properties. If the script doesn’t find any matches, then it creates the new Managed Property then adds the Crawled Properties.
$xmlData.ManagedProperties.ManagedProperty | ForEach-Object {
$newPropName=$_.Name
$newPropDescription=$_.Description
$newPropType=$_.Type
#Checks the existance of the Managed Property.
$propExist=Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Identity $newPropName
if($propExist -ne $null)
{
Write-Host -ForegroundColor red ----------------------------------------/
write-host -f red It looks like the Managed Property $newPropName already exists. Mapping the Crawled Properties.
Write-Host -ForegroundColor red ----------------------------------------/
MapCrawledProps
}
else
{
Write-Host
Write-Host -ForegroundColor Blue Creating Managed Property named $newPropName
New-SPEnterpriseSearchMetadataManagedProperty -Name $newPropName -SearchApplication $ssa -Type $newPropType
MapCrawledProps
}
}
When I find that need to add a Managed Property or need to update a Managed Property, I loop through each Crawled Property configuration and map it to the current Managed Property.
$newCrawledProp | ForEach-Object {
try
{
New-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $managedProp -CrawledProperty $newCrawledProp
write-host -f black Created Crawled Property mapping --$newCrawledPropName--.
Write-Host -ForegroundColor DarkGreen -----
Write-Host
}
catch [system.exception]
{
Write-Host -ForegroundColor Magenta An error occured!
write-host -ForegroundColor Magenta $error[0].Exception.Message
}
}
After the Managed Properties and Crawled Properties are taken care of, I run a Full Crawl to ensure the changes took place.
#Run a full crawl to ensure changes took place.
$contentsourceInfo = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa -Identity $contentSource
$contentsourceInfo.StartFullCrawl()
How to use
Save the PS script along with the XML configuration document in the same file folder. The PS script looks in its current folder for the configuration file.
Once you have the XML document populated with your Managed Properties, rules, and other properties, you can go ahead and run the PS script.
Meat and Potatoes
XML Config
<?xml version=”1.0″ encoding=”utf-8″ ?>
<ManagedProperties>
<SSAName>Search Service Application</SSAName>
<ContentSource>My Crawled Sites QA</ContentSource>
<ManagedProperty Name=”MyContentTypes” Type=”1″ Description=”” UseInScope=”No” Optimize=”” > <!–1 = Text,2 = Integer,3 = Decimal,4 = DateTime,5 = YesNo,6 = Binary,7 = Double –>
<CrawledProperties Name=”ContentType”/>
<CrawledProperties Name=”ows_ContentType”/>
</ManagedProperty>
– <ManagedProperty Name=”MyLocID” Type=”3″ Description=”” UseInScope=”No” Optimize=”” >
<CrawledProperties Name=”ows_LocId”/>
</ManagedProperty>
<ManagedProperty Name=”MyPrimaryDate” Type=”4″ Description=”” UseInScope=”No” Optimize=”” >
<CrawledProperties Name=”ows_PrimaryDate”/>
</ManagedProperty>
</ManagedProperties>
PowerShell Script
#--------
#Author: Marion Owen x.8923
#Description: This creates managed properties used for SP Search and maps Crawled Properties to the Managed Property.
#Ref: http://technet.microsoft.com/en-us/library/ff608089.aspx
#--------
#—————-Get the xml file—————————————————————if((Get-PSSnapin | Where {$_.Name -eq “Microsoft.SharePoint.PowerShell”}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$xmlConfig = Resolve-Path “ManagedProperties.xml” #This is the configuration XML located in the same folder.
[xml]$xmlData=Get-Content $xmlConfig
#—————-Create New Scope Function ———————————————
Function CreateNewManagedProperty()
{
$ssa=Get-SPEnterPriseSearchServiceApplication -Identity $xmlData.SSAName
$contentSource = $xmlData.ManagedProperties.ContentSource
$xmlData.ManagedProperties.ManagedProperty | ForEach-Object {
$newPropName=$_.Name
$newPropDescription=$_.Description
$newPropType=$_.Type
#Checks the existance of the Managed Property.
$propExist=Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Identity $newPropName
if($propExist -ne $null)
{Write-Host -ForegroundColor red —————————————-/
write-host -f red It looks like the Managed Property $newPropName already exists. Mapping the Crawled Properties.
Write-Host -ForegroundColor red —————————————-/
MapCrawledProps
}
else
{
Write-Host
Write-Host -ForegroundColor Blue Creating Managed Property named $newPropName
New-SPEnterpriseSearchMetadataManagedProperty -Name $newPropName -SearchApplication $ssa -Type $newPropType
MapCrawledProps
}
}
#Run a full crawl to ensure changes took place.
$contentsourceInfo = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa -Identity $contentSource
$contentsourceInfo.StartFullCrawl()
}
#—————-Create New Scope Rules Function ———————————————
Function MapCrawledProps()
{
write-host -f black Creating Crawled Property mapping…..
$_.CrawledProperties| ForEach-Object {
$newCrawledPropName = $_.Name
$newCrawledProp = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $ssa -Name $newCrawledPropName -Limit 1
$managedProp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Identity $newPropName
$newCrawledProp | ForEach-Object {
try
{
New-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $managedProp -CrawledProperty $newCrawledProp
write-host -f black Created Crawled Property mapping –$newCrawledPropName–.
Write-Host -ForegroundColor DarkGreen —–
Write-Host
}
catch [system.exception]
{
Write-Host -ForegroundColor Magenta An error occured!
write-host -ForegroundColor Magenta $error[0].Exception.Message
}
}
}
}
CreateNewManagedProperty
Write-Host “Completed. Press any key to continue …”
$x = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)
References
- http://technet.microsoft.com/en-us/library/ff608089.aspx
- http://technet.microsoft.com/en-us/library/ff608017.aspx
- http://technet.microsoft.com/en-us/library/ff607730.aspx