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
- #script reads from xml file to add crawled properties and managed properties
- cls
- #add sharepoint cmdlets
- if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
- {
- Add-PsSnapin Microsoft.SharePoint.PowerShell
- }
- #get the XML file
- [System.Xml.XmlDocument] $XmlDoc = new-object System.Xml.XmlDocument
- $file = resolve-path(".\searchprop.xml")
- if (!$file)
- {
- Write-Host "Could not find the configuration file specified. Aborting." -ForegroundColor red
- Break
- }
- write-host "Parsing file: " $file
- $XmlDoc = [xml](Get-Content $file)
- #get the node containing the name of the search service application where you want to add properties
- $sa = $XmlDoc.SearchProperties.ServiceName
- $searchapp = Get-SPEnterpriseSearchServiceApplication $sa
- #loop through crawled properties to check or add -- don't add if it already exists
- $CrawledPropNodeList = $XmlDoc.SearchProperties.CrawledProperties
- foreach ($CrawledPropNode in $CrawledPropNodeList.CrawledProperty)
- {
- $SPCrawlProp = $CrawledPropNode.Name
- $SPCrawlPropType = $CrawledPropNode.Type
- #Create Crawled Property if it doesn't exist
- if (!(Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name $SPCrawlProp -ea "silentlycontinue"))
- {
- switch ($SPCrawlPropType)
- {
- "Text" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 31 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
- "Integer" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 20 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
- "Decimal" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 5 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
- "DateTime" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 64 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
- "YesNo" {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 11 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
- default {$crawlprop = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category SharePoint -VariantType 31 -Name $SPCrawlProp -IsNameEnum $false -PropSet "00130329-0000-0130-c000-000000131346"}
- }
- }
- }
- #now that the crawled properties exist, loop through managed properties and add
- $PropertyNodeList = $XmlDoc.SearchProperties.ManagedProperties
- foreach ($PropertyNode in $PropertyNodeList.ManagedProperty)
- {
- $SharePointProp = $PropertyNode.Name
- $SharePointPropType = $PropertyNode.Type
- $SharePointPropMapList = $PropertyNode.Map
- #add managed property
- #remove it if it already exists
- if ($mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $SharePointProp -ea "silentlycontinue")
- {
- #$mp | Remove-SPEnterpriseSearchMetadataManagedProperty -Confirm
- $mp.DeleteAllMappings()
- $mp.Delete()
- $searchapp.Update()
- }
- New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $SharePointProp -Type $SharePointPropType
- $mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $SharePointProp
- #add multiple crawled property mappings
- 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
- }
- }
Here’s a sample of the XML file where the properties to be added can be read from:
searchprop.xml Sample File
- <?xml version="1.0" encoding="utf-8"?>
- <SearchProperties>
- <ServiceName>Search Service Application</ServiceName>
- <CrawledProperties>
- <CrawledProperty Type="Text" Name="ows_SiteDescription" />
- <CrawledProperty Type="Decimal" Name="ows_MemberCount" />
- <CrawledProperty Type="Text" Name="ows_SiteURL" />
- <CrawledProperty Type="Decimal" Name="ows_SiteHits" />
- <CrawledProperty Type="YesNo" Name="ows_ProjSensitive" />
- <CrawledProperty Type="DateTime" Name="ows_ProjStartDate" />
- <CrawledProperty Type="Text" Name="ows_ProjectName" />
- </CrawledProperties>
- <ManagedProperties>
- <ManagedProperty Type="1" Name="XYZSiteDesc">
- <Map Category="SharePoint">ows_SiteDescription</Map>
- </ManagedProperty>
- <ManagedProperty Type="3" Name="XYZMembers">
- <Map Category="SharePoint">ows_MemberCount</Map>
- </ManagedProperty>
- <ManagedProperty Type="1" Name="XYZSiteUrl">
- <Map Category="SharePoint">ows_SiteURL</Map>
- </ManagedProperty>
- <ManagedProperty Type="3" Name="XYZSiteHits">
- <Map Category="SharePoint">ows_SiteHits</Map>
- </ManagedProperty>
- <ManagedProperty Type="5" Name="XYZProjectSecurity">
- <Map Category="SharePoint">ows_ProjSensitive</Map>
- </ManagedProperty>
- <ManagedProperty Type="4" Name="XYZProjectStartDate">
- <Map Category="SharePoint">ows_ProjStartDate</Map>
- </ManagedProperty>
- <ManagedProperty Type="1" Name="XYZProjectName">
- <Map Category="Basic">urn:schemas.microsoft.com:fulltextqueryinfo:displaytitle</Map>
- <Map Category="SharePoint">ows_Title</Map>
- </ManagedProperty>
- </ManagedProperties>
- </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