Tuesday, December 20, 2011

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

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

No comments:

Post a Comment