Have you ever created and mapped a decent amount of managed properties in SharePoint using the UI? If you have, you probably know that it’s not the most exciting job. Lots of clicking, waiting, reloading,…
If you are someone like me… someone who doesn’t like doing things manually… and you are reading this, you are probably expecting to find an automated way of creating those things. And you will not be dissappointed. I’ll even throw in something extra: add “crawled properties”.
Normally, crawled properties are added during the crawling process. I’ll show you how you can add those properties manually using PowerShell without having the need to kick-off a crawl before those properties show up.
Configuration File
First of all, the script which is shown here, requires an XML file which has the crawled and managed properties you want to add. You can find a sample of such an XML below.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <SearchConfiguration> <CrawledProperties> <CrawledProperty DataType="Text" Category="People" VariantType="31" MultiValue="No" PropSetId="00110329-0000-0110-c000-000000111146" Name="urn:schemas-microsoft-com:sharepoint:portal:profile:EmployeeType" /> </CrawledProperties> <ManagedProperties> <ManagedProperty Type="1" Name="Assistant"> <Map Category="People">urn:schemas-microsoft-com:sharepoint:portal:profile:Assistant</Map> </ManagedProperty> </ManagedProperties> </SearchConfiguration> |
To find the values you need to provide for the different attributes, you can look at the documentation for New-SPEnterpriseSearchMetadataCrawledProperty and New-SPEnterpriseSearchMetadataManagedProperty.
Script
Once you have this file, you can feed it into the following script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<# .SYNOPSIS Add managed properties to the Search Service Application. .DESCRIPTION Add managed properties to the Search Service Application. .NOTES File Name: Create-ManagedProperty.ps1 Author : Bart Kuppens Version : 1.0 .PARAMETER ConfigFile Specifies the location and name of the config file which contains the properties that need to be created. .PARAMETER SearchAppName Specifies the name of the Search Service Application. .EXAMPLE PS > .\Create-ManagedProperty.ps1 -ConfigFile "c:\temp\managedproperty.xml" -SearchAppName "Enterprise Search Service" #> [CmdletBinding()] param( [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the URL of the Web Application.")] [string]$ConfigFile, [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the name of the Search Service Application.")] [string]$SearchAppName ) if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) { Write-Host "Loading SharePoint cmdlets..." Add-PsSnapin Microsoft.SharePoint.PowerShell } #get the XML file if (!$ConfigFile) { Write-Host "Could not find the configuration file specified. Aborting execution!" -ForegroundColor red Break } Write-Host "Parsing file: " $file [System.Xml.XmlDocument]$XmlDoc = [xml](Get-Content $file) $searchapp = Get-SPEnterpriseSearchServiceApplication $SearchAppName # Add Crawled Properties if they don't exist yet $CrawledPropNodeList = $XmlDoc.SearchConfiguration.CrawledProperties foreach ($CrawledPropNode in $CrawledPropNodeList.CrawledProperty) { $SPCrawlProp = $CrawledPropNode.Name $SPCrawlPropType = $CrawledPropNode.DataType $SPCrawlPropCategory = $CrawledPropNode.Category $SPCrawlPropVariantType = $CrawledPropNode.VariantType $SPCrawlPropSetId = $CrawledPropNode.PropSetId if (!(Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name $SPCrawlProp -ea "silentlycontinue")) { $crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $SPCrawlPropCategory -VariantType $SPCrawlPropVariantType -Name $SPCrawlProp -IsNameEnum $false -PropSet $SPCrawlPropSetId } } # Add Managed Properties $PropertyNodeList = $XmlDoc.SearchConfiguration.ManagedProperties foreach ($PropertyNode in $PropertyNodeList.ManagedProperty) { $SharePointProp = $PropertyNode.Name $SharePointPropType = $PropertyNode.Type $SharePointPropMapList = $PropertyNode.Map if ($mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $SharePointProp -ea "silentlycontinue") { $mp.DeleteAllMappings() $mp.Delete() $searchapp.Update() } New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $SharePointProp -Type $SharePointPropType $mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $SharePointProp foreach ($SharePointPropMap in $SharePointPropMapList) { $SPMapCat = $SharePointPropMap.Category $SPMapName = $SharePointPropMap.InnerText $cat = Get-SPEnterpriseSearchMetadataCategory –SearchApplication $searchapp –Identity $SPMapCat $prop = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $cat -Name $SPMapName New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -CrawledProperty $prop -ManagedProperty $mp } } |