Creating Managed & Crawled Properties using PowerShell

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.

<?xml version="1.0" encoding="utf-8"?>
      <CrawledProperty DataType="Text" Category="People" VariantType="31" MultiValue="No" PropSetId="00110329-0000-0110-c000-000000111146" Name="urn:schemas-microsoft-com:sharepoint:portal:profile:EmployeeType" />
      <ManagedProperty Type="1" Name="Assistant">
         <Map Category="People">urn:schemas-microsoft-com:sharepoint:portal:profile:Assistant</Map>

To find the values you need to provide for the different attributes, you can look at the documentation for New-SPEnterpriseSearchMetadataCrawledProperty and New-SPEnterpriseSearchMetadataManagedProperty.


Once you have this file, you can feed it into the following script:

 Add managed properties to the Search Service Application.

 Add managed properties to the Search Service Application.

 File Name: Create-ManagedProperty.ps1
 Author   : Bart Kuppens
 Version  : 1.0

 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.

 PS > .\Create-ManagedProperty.ps1 -ConfigFile "c:\temp\managedproperty.xml" -SearchAppName "Enterprise Search Service"
 [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the URL of the Web Application.")] 
 [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the name of the Search Service Application.")] 

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    

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")
   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

By Bart

Bart is a certified SharePoint consultant / architect at CTG Belgium NV with a broad professional experience in IT, a background in software development with a specialisation in Microsoft products and technologies and a solid knowledge and experience in Microsoft SharePoint Products and Technologies. He started as a COBOL developer on a mainframe environment and grew into software development for Windows platforms. Participated in projects varying from migrations of existing applications to development of Web applications and Windows applications. Became fascinated by the SharePoint 2007 platform and strongly believed in the added business value of this platform. Is since then fully committed to SharePoint and focuses on SharePoint implementations, migrations, integrations, design and coaching. Stays on top of new developments within the SharePoint technology stack and related technologies.

%d bloggers like this: