Skip Ribbon Commands
Skip to main content

Anders Rask on SharePoint

:

Anders Rask on SharePoint > Posts > PowerShell function to export mappings and crawled / managed properties
July 11
PowerShell function to export mappings and crawled / managed properties

​I recently was tasked with moving a few mappings, crawled and managed properties from our own test site to the customers integration test environment.

To avoid typo's (and manual labour as such:-) I decided to throw together a few lines of PowerShell to do the tedious work for me.

You could also use this script to document your currently deployed Managed Properties and mappings.

Now it is quite forward to import the managed properties etc. into a new farm using the OOB cmdlets (Hint: Get-Command New-SPEnterpriseSearchMetadata*) now that you have the XML.

<#
.SYNOPSIS
Exports Search Metadata Mappings to an XmlDocument
.DESCRIPTION
This method can be used to retrieve Search Metadata Mappings from an existing farm.
The syntax can also be used as documentation of a farm
.EXAMPLE
$managedProperties | % {
New-Object PSobject -Property @{
Mapping = Get-SPEnterpriseSearchMetadataMapping -SearchApplication (Get-SPEnterpriseSearchServiceApplication) -ManagedProperty $_
ManagedPropertyName = $_.Name }
} | Export-SPEnterpriseSearchMetadataMapping | % {
$node = $xmlMappings.ImportNode($_.Mapping,$false)
$xmlMappings.DocumentElement.AppendChild($node)
} | Out-Null
.LINK
Export-SPEnterpriseSearchMetadataMapping
#>
function Export-SPEnterpriseSearchMetadataMapping
{
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
    [Microsoft.Office.Server.Search.Administration.Mapping] $Mapping,
    [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
    [string] $ManagedPropertyName
)
 
    process
    {
        [xml]$xml = "<Mapping />"
        $xml.DocumentElement.SetAttribute('ManagedProperty', $ManagedPropertyName)
        $xml.DocumentElement.SetAttribute('CrawledProperty', $Mapping.CrawledPropertyName)
        $xml.DocumentElement.SetAttribute('PropertySet', $Mapping.CrawledPropset)
        $xml.DocumentElement.SetAttribute('VariantType', $Mapping.CrawledPropertyVariantType)
        $xml
    }
}
 
<#
.SYNOPSIS
Exports Search Metadata Managed Properties to an XmlDocument
.DESCRIPTION
This method can be used to retrieve Search Metadata Managed Properties from an existing farm.
The syntax can also be used as documentation of a farm
.EXAMPLE
$managedProperties | Export-SPEnterpriseSearchMetadataManagedProperty | % {
$node = $xmlManagedProperties.ImportNode($_.ManagedProperty,$false)
$xmlManagedProperties.DocumentElement.AppendChild($node)
} | Out-Null
.LINK
Export-SPEnterpriseSearchMetadataManagedProperty
#>
function Export-SPEnterpriseSearchMetadataManagedProperty
{
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
    [Microsoft.Office.Server.Search.Cmdlet.ManagedPropertyPipeBind] $ManagedProperty
)
process
{
 
    [xml]$xml = "<ManagedProperty />"
    $xml.DocumentElement.SetAttribute('Name', $_.Name)
    $xml.DocumentElement.SetAttribute('Description', $_.Description)
    $xml.DocumentElement.SetAttribute('Type', [int]$_.ManagedType)
    $xml.DocumentElement.SetAttribute('MaxCharactersInPropertyStoreIndex', $_.MaxCharactersInPropertyStoreIndex)
    $xml.DocumentElement.SetAttribute('FullTextQueriable', $_.FullTextQueriable)
    $xml.DocumentElement.SetAttribute('HasMultipleValues', $_.HasMultipleValues)
    $xml.DocumentElement.SetAttribute('Retrievable', $_.Retrievable)
    $xml.DocumentElement.SetAttribute('EnabledForScoping', $_.EnabledForScoping)
    $xml.DocumentElement.SetAttribute('RemoveDuplicates', $_.RemoveDuplicates)
    $xml.DocumentElement.SetAttribute('PutInPropertyBlob', $_.PutInPropertyBlob)
    $xml.DocumentElement.SetAttribute('QueryPropertyBlob', $_.QueryPropertyBlob)
    $xml.DocumentElement.SetAttribute('RespectPriority', $_.RespectPriority)
    $xml
}
 
<#
.SYNOPSIS
Exports Search Metadata Crawled Properties to an XmlDocument
.DESCRIPTION
This method can be used to retrieve Search Metadata Crawled Properties from an existing farm.
The syntax can also be used as documentation of a farm
.EXAMPLE
$CrawledProperty | Export-SPEnterpriseSearchMetadataCrawledProperty | % {

 

$node = $xmlCrawledProperties.ImportNode($_.CrawledProperty,$false)

 

$xmlCrawledProperties.DocumentElement.AppendChild($node)
} | Out-Null
.LINK
Export-SPEnterpriseSearchMetadataCrawledProperty
#>
function Export-SPEnterpriseSearchMetadataCrawledProperty
{
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
    [Microsoft.Office.Server.Search.Cmdlet.CrawledPropertyPipeBind] $CrawledProperty
)
process
{
    [xml] $xml = "<CrawledProperty />"
    $xml.DocumentElement.SetAttribute('Name', $_.Name)
    $xml.DocumentElement.SetAttribute('Category', $_.CategoryName)
    $xml.DocumentElement.SetAttribute('PropertySet', $_.PropSet)
    $xml.DocumentElement.SetAttribute('VariantType', $_.VariantType)
    $xml.DocumentElement.SetAttribute('IsMappedToContents', $_.IsMappedToContents)
    $xml
}
}
 
<#
.SYNOPSIS
Exports Search Metadata to an XmlDocument
.DESCRIPTION
This method can be used to retrieve Search Metadata from an existing farm.
The syntax can also be used as documentation of a farm
.EXAMPLE
[xml]$SearchMetadata = Export-SPEnterpriseSearchMetadata -CrawledProperty $crawledProperties -ManagedProperty $managedProperties

 

$SearchMetadata.OuterXml | Out-File -FilePath "MetadataMappings.xml" -Encoding UTF8

 

.LINK
Export-SPEnterpriseSearchMetadata
#>
function Export-SPEnterpriseSearchMetadata
{
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true)]
    [Microsoft.Office.Server.Search.Administration.CrawledProperty[]] $CrawledProperty,
    [Parameter(Mandatory=$true)]
    [Microsoft.Office.Server.Search.Administration.ManagedProperty[]] $ManagedProperty
)
process
{
    [xml]$xml = "<Metadata />"
    [xml]$xmlCrawledProperties = "<CrawledProperties />"
 
    # append CrawledProperty to CrawledProperties node
    $CrawledProperty | Export-SPEnterpriseSearchMetadataCrawledProperty | % {
    $node = $xmlCrawledProperties.ImportNode($_.CrawledProperty,$false)
    $xmlCrawledProperties.DocumentElement.AppendChild($node)
    } | Out-Null
    # append to MetaData
    $xml.DocumentElement.AppendChild($xml.ImportNode($xmlCrawledProperties.CrawledProperties, $true)) | Out-Null
 
    [xml]$xmlManagedProperties = "<ManagedProperties />"
    # append ManagedProperty to ManagedProperties node
    $managedProperties | Export-SPEnterpriseSearchMetadataManagedProperty | % {
    $node = $xmlManagedProperties.ImportNode($_.ManagedProperty,$false)
    $xmlManagedProperties.DocumentElement.AppendChild($node)
    } | Out-Null
    # append to Metadata
    $xml.DocumentElement.AppendChild($xml.ImportNode($xmlManagedProperties.ManagedProperties, $true)) | Out-Null
 
    [xml]$xmlMappings = "<Mappings />"
 
    foreach ( $managedProperty in $managedProperties )
    {
        $mappings = Get-SPEnterpriseSearchMetadataMapping -SearchApplication (Get-SPEnterpriseSearchServiceApplication) -ManagedProperty $managedProperty
        # A managed property can have multiple mappings
        if ( $mappings -is [Array] )
        {
        foreach ($mapping in $mappings)
        {
            $xmlMapping = $mapping | Export-SPEnterpriseSearchMetadataMapping -ManagedPropertyName $managedProperty.Name
            $node = $xmlMappings.ImportNode($xmlMapping.Mapping,$false)
$xmlMappings.DocumentElement.AppendChild($node) | Out-Null
        }
    }
    else
    {
        $xmlMapping = Export-SPEnterpriseSearchMetadataMapping -ManagedPropertyName $managedProperty.Name -Mapping $mappings
        $node = $xmlMappings.ImportNode($xmlMapping.Mapping,$false)
        $xmlMappings.DocumentElement.AppendChild($node) | Out-Null
    }
}
 
    # append to Metadata
    $xml.DocumentElement.AppendChild($xml.ImportNode($xmlMappings.Mappings, $true)) | Out-Null
    $xml
}
}

 

 
Find example syntax of calling the advanced functions below
 
# get crawled properties
$crawledProperties = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication (Get-SPEnterpriseSearchServiceApplication) | ?{$_.Name -like "ows_yourCrawledPropertyPrefix*"}
 
# get managed properties
$managedProperties = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication (Get-SPEnterpriseSearchServiceApplication) | ?{$_.Name -like "SomePrefix*" -or $_.Name -Like "SomeOtherManagedProperty" }
[xml]$SearchMetadata = Export-SPEnterpriseSearchMetadata -CrawledProperty $crawledProperties -ManagedProperty $managedProperties
 
$SearchMetadata.OuterXml | Out-File -FilePath "SearchProperties2.xml" -Encoding UTF8
 
 
 

 

I have attached the complete script here (remember to remove .txt from extension!)

ExportCrawledProperties.ps1.txtExportCrawledProperties.ps1.txt

   

Comments

Very useful

Thank you for providing this!  Only required on change to export for 2013:

 $mappings = Get-SPEnterpriseSearchMetadataMapping -SearchApplication (Get-SPEnterpriseSearchServiceApplication) -ManagedProperty $managedProperty.Id
 on 1/23/2014 12:14 AM

input object cannot be bound

After creating the cmdlet and running cmd below I get the following error.

Export-SPEnterpriseSearchMetadataMapping : The input object cannot be bound because it did not contain the information
required to bind all mandatory parameters:  Mapping
 on 10/23/2015 11:23 AM

Add Comment

Title


Body *


Migrated Source URL


Commentator Name


Commentator Email


BotCheck *


Are you human? What is the sum of fifty-two minus ten?

Attachments