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.

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.
  1. <managedproperties>  
  2. <managedproperty name=”myManagedProp1″ crawlproperty =”myCrawledProp1″ type=”1″ />   
  3. <managedproperty name=”myManagedProp2″ crawlproperty =”myCrawledProp1″ type=”2″ />   
  4. </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

  1. [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)  
  2. $snapin = ”Microsoft.SharePoint.PowerShell”  
  3. if (get - pssnapin $snapin - ea“silentlycontinue”)  
  4. {  
  5.     write - host - f Green“PSsnapin $snapin is loaded”  
  6. }  
  7. elseif(get - pssnapin $snapin - registered - ea“silentlycontinue”)   
  8. {  
  9.     write - host - f Green“PSsnapin $snapin is registered”Add - PSSnapin $snapin write - host - f Green“PSsnapin $snapin is loaded”  
  10. }   
  11. else   
  12. {  
  13.     write - host - f orange“PSSnapin $snapin not found” - foregroundcolor Red  
  14. }
Function definition
  1. function createManagedProperty($xmlFile, $searchApplicationName)   
  2. {  
  3.     $configXml = [xml](get - content $xmlFile)  
  4.     $searchapp = Get - SPEnterpriseSearchServiceApplication $searchApplicationName  
  5.     $category = Get - SPEnterpriseSearchMetadataCategory - Identity SharePoint - SearchApplication $searchapp  
  6.   
  7.     foreach($metaProp in $configXml.managedproperties.managedproperty)   
  8.     {  
  9.         Write - Host - ForegroundColor Green“Creating property: ”$metaProp.Name $crawledPropName = $metaProp.crawlproperty  
  10.     }
Check if Managed property already exist
  1. $ManagedProp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $metaProp.name -ErrorAction SilentlyContinue 
Delete existing managed property
  1. if ($ManagedProp - ne $null)   
  2. {  
  3.     $ManagedProp.DeleteAllMappings()  
  4.     Remove - SPEnterpriseSearchMetadataManagedProperty - SearchApplication $searchapp - Identity $metaProp.name - Confirm: $false  
  5. }   
  6. else {##Create Managed property  
  7.     New - SPEnterpriseSearchMetadataManagedProperty - SearchApplication $searchapp - Name $metaProp.name - Type $metaProp.type - Queryable $true - Retrievable $true  
  8. }  
  9. $ManagedProp = Get - SPEnterpriseSearchMetadataManagedProperty - SearchApplication $searchapp - Identity $metaProp.name  
  10.  
  11. ##Check  
  12. if Crawled Property is available  
  13.   
  14. $crawledproperty = Get - SPEnterpriseSearchMetadataCrawledProperty - SearchApplication $searchapp - Name $crawledPropName - ErrorAction SilentlyContinue  
  15.  
  16. ##Map managed property to crawl property  
  17.   
  18. if ($crawledproperty - ne $null)   
  19. {  
  20.     New - SPEnterpriseSearchMetadataMapping - SearchApplication $searchapp - ManagedProperty $ManagedProp - CrawledProperty $crawledproperty  
  21. }  
  22. else  
  23. {  
  24.     Write - Host - ForegroundColor Red“Crawled property does not exist: ”$crawledPropName  
  25. }  
  26. }  
  27. }
Declaration of the constants
  1. $SearchApplicationName = “Search Service Application”  
  2. $configfile = “ManagedProperties.xml”  
  3. $executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent  
  4. $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.

Up Next
    Ebook Download
    View all
    Learn
    View all