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.
The following is the approach to write a PS script that can be used to create one or more managed properties using an input XML file.
- Create an xml file ‘ManagedProperties.xml’ as below and provide details.
- <managedproperties>
- <managedproperty name=”myManagedProp1″ crawlproperty =”myCrawledProp1″ type=”1″ />
- <managedproperty name=”myManagedProp2″ crawlproperty =”myCrawledProp1″ type=”2″ />
- </managedproperties>
- Write the script in any suitable editor and save it as “ManagedProperties.ps1”.
Function – ManagedProperty Creation
Author – Deepak Solanki
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***” - Make sure that PS script and XML file are in same directory.
- Ensure that you have provided correct “$SearchApplicationName” in PS script.
- Open the PowerShell/SharePoint PowerShell console and run the PS script.
- While copying XML and script from this blog, verify if special characters (“-$ etc) are correct.