Welcome to EMC Consulting Blogs Sign in | Join | Help

Matt Lally's Blog

Create SharePoint 2010 Search Crawl and Managed Properties using PowerShell

The following PowerShell script can be used to add Search Crawl properties and Managed properties.  The properties are added based on vales read from an XML file.  This script comes in handy if you have multiple properties you need to add to multiple environments.

PowerShell Script
  1. #script reads from xml file to add crawled properties and managed properties
  2. cls
  3. #add sharepoint cmdlets
  4. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
  5. {    
  6.       Add-PsSnapin Microsoft.SharePoint.PowerShell
  7. }
  8. #get the XML file
  9. [System.Xml.XmlDocument] $XmlDoc = new-object System.Xml.XmlDocument
  10. $file = resolve-path(".\searchprop.xml")
  11. if (!$file)
  12. {
  13.         Write-Host "Could not find the configuration file specified. Aborting." -ForegroundColor red    
  14.         Break
  15. }
  16. write-host "Parsing file: " $file
  17. $XmlDoc = [xml](Get-Content $file)
  18. #get the node containing the name of the search service application where you want to add properties
  19. $sa = $XmlDoc.SearchProperties.ServiceName
  20. $searchapp = Get-SPEnterpriseSearchServiceApplication $sa
  21. #loop through crawled properties to check or add -- don't add if it already exists
  22. $CrawledPropNodeList = $XmlDoc.SearchProperties.CrawledProperties
  23. foreach ($CrawledPropNode in $CrawledPropNodeList.CrawledProperty)
  24. {
  25.     $SPCrawlProp = $CrawledPropNode.Name
  26.     $SPCrawlPropType = $CrawledPropNode.Type
  27.     
  28.     #Create Crawled Property if it doesn't exist
  29.     if (!(Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name $SPCrawlProp -ea "silentlycontinue"))
  30.     {
  31.         switch ($SPCrawlPropType)
  32.         {
  33.         "Text" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 31 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
  34.         "Integer" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 20 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}  
  35.         "Decimal" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 5 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}  
  36.         "DateTime" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 64 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
  37.         "YesNo" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 11 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
  38.         default {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 31 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
  39.         }
  40.     }
  41. }
  42. #now that the crawled properties exist, loop through managed properties and add
  43. $PropertyNodeList = $XmlDoc.SearchProperties.ManagedProperties
  44. foreach ($PropertyNode in $PropertyNodeList.ManagedProperty)
  45. {
  46.     $SharePointProp = $PropertyNode.Name
  47.     $SharePointPropType = $PropertyNode.Type
  48.     $SharePointPropMapList = $PropertyNode.Map
  49.     #add managed property
  50.     #remove it if it already exists
  51.     if ($mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $SharePointProp -ea "silentlycontinue")
  52.     {
  53.          #$mp | Remove-SPEnterpriseSearchMetadataManagedProperty -Confirm
  54.         $mp.DeleteAllMappings()
  55.         $mp.Delete()
  56.         $searchapp.Update()
  57.     }
  58.     New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $SharePointProp -Type $SharePointPropType
  59.     $mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $SharePointProp
  60.     #add multiple crawled property mappings
  61.     foreach ($SharePointPropMap in $SharePointPropMapList)
  62.     {
  63.         $SPMapCat = $SharePointPropMap.Category
  64.         $SPMapName = $SharePointPropMap.InnerText
  65.         $cat = Get-SPEnterpriseSearchMetadataCategory –SearchApplication $searchapp –Identity $SPMapCat
  66.         $prop = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $cat -Name $SPMapName
  67.         New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -CrawledProperty $prop -ManagedProperty $mp
  68.     }
  69. }

Here’s a sample of the XML file where the properties to be added can be read from:

searchprop.xml Sample File
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <SearchProperties>
  3.     <ServiceName>Search Service Application</ServiceName>
  4.   <CrawledProperties>
  5.     <CrawledProperty Type="Text" Name="ows_SiteDescription" />
  6.     <CrawledProperty Type="Decimal" Name="ows_MemberCount" />
  7.     <CrawledProperty Type="Text" Name="ows_SiteURL" />
  8.     <CrawledProperty Type="Decimal" Name="ows_SiteHits" />
  9.     <CrawledProperty Type="YesNo" Name="ows_ProjSensitive" />
  10.     <CrawledProperty Type="DateTime" Name="ows_ProjStartDate" />
  11.     <CrawledProperty Type="Text" Name="ows_ProjectName" />
  12.   </CrawledProperties>
  13.     <ManagedProperties>
  14.         <ManagedProperty Type="1" Name="XYZSiteDesc">
  15.             <Map Category="SharePoint">ows_SiteDescription</Map>
  16.         </ManagedProperty>
  17.         <ManagedProperty Type="3" Name="XYZMembers">
  18.             <Map Category="SharePoint">ows_MemberCount</Map>
  19.         </ManagedProperty>
  20.         <ManagedProperty Type="1" Name="XYZSiteUrl">
  21.             <Map Category="SharePoint">ows_SiteURL</Map>
  22.         </ManagedProperty>
  23.         <ManagedProperty Type="3" Name="XYZSiteHits">
  24.             <Map Category="SharePoint">ows_SiteHits</Map>
  25.         </ManagedProperty>
  26.     <ManagedProperty Type="5" Name="XYZProjectSecurity">
  27.       <Map Category="SharePoint">ows_ProjSensitive</Map>
  28.     </ManagedProperty>
  29.     <ManagedProperty Type="4" Name="XYZProjectStartDate">
  30.       <Map Category="SharePoint">ows_ProjStartDate</Map>
  31.     </ManagedProperty>
  32.     <ManagedProperty Type="1" Name="XYZProjectName">
  33.       <Map Category="Basic">urn:schemas.microsoft.com:fulltextqueryinfo:displaytitle</Map>
  34.       <Map Category="SharePoint">ows_Title</Map>
  35.     </ManagedProperty>
  36.   </ManagedProperties>
  37. </SearchProperties>
Note:  The PowerShell script and XML file should be placed in same folder prior to running.

References:

Technet link for cmdlet New-SPEnterpriseSearchMetadataCrawledProperty to add a new crawl property.

Technet link for cmdlet New-SPEnterpriseSearchMetadataManagedProperty to add new managed property.

Technet link for cmdlet New-SPEnterpriseSearchMetadataMapping to add new mapping to a managed property.

Managed Property type key values are as follows:
  • Text = 1
  • Integer = 2
  • Decimal = 3
  • DateTime = 4
  • YesNo = 5
  • Binary = 6
Published Tuesday, December 20, 2011 3:25 PM by matt.lally

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Nico de Jong said:

Just what the doctor ordered - Nice script, nice work, great community contribution!

December 22, 2011 9:57 AM
 

Adil said:

You are great dude..thanks..may God bless you..Thanks!

November 22, 2012 9:23 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems