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 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
- }
- $ManagedProp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $metaProp.name -ErrorAction SilentlyContinue
- 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
- }
- }
- }
- $SearchApplicationName = “Search Service Application”
- $configfile = “ManagedProperties.xml”
- $executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
- $file = “$executingScriptDirectory/$configfile”
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.

Deepak SolankiPosted Sep 20, 2015, 7:19 AM
Thank you very much for your support and appreciation.
Kaviya BalasubramanianPosted Sep 14, 2015, 5:00 AM
Thank you Deepak
Kaviya BalasubramanianPosted Sep 11, 2015, 2:57 AM
Hi Deepak, I knew that we can create this property manually in sharepoint online but I want to know about how we can achieve this sharepoint online via jsom or ps?
Ankit BansalPosted Sep 10, 2015, 1:03 AM
good one..thanks...
Pankaj Kumar ChoudharyPosted Sep 9, 2015, 9:40 PM
Nice Start Deepak And welcome to c#-Corner community.......
Santhakumar MunuswamyPosted Sep 9, 2015, 3:19 PM
Thanks for nice article
Mohammed IbrahimPosted Sep 9, 2015, 2:03 PM
nice share
Sumit DeshmukhPosted Sep 9, 2015, 9:43 AM
Nice article..Thanks for Sharing sir
Karthikeyan KPosted Sep 9, 2015, 9:02 AM
Good one sir...Thanks for sharing us