4

I have a set of crawled properties I want to map to managed properties using powershell. But for some strange reason they can't be found when using PS, but they show up in the UI and I can make them manually, which I'm trying to avoid.

Edit: Same issue being brought up here

I'm using this script: https://gallery.technet.microsoft.com/office/Powershell-script-to-09ffa974

enter image description here

  • 1
    You will have to post your code Batman. – Bunzab Mar 22 '17 at 16:33
  • I've updated OP with more information – Batman Mar 22 '17 at 17:54
  • Seems to be an issue with SP 2016. My advice would be to raise a MS support call. – Bunzab Mar 23 '17 at 8:11
1

Properties you see in Central Admin are called "implicit".
They are not created in corresponding table in Search database yet (actually, I couldn't even find, where they are stored).
In your case, there are several ways to create them:

  1. PowerShell:

    New-SPEnterpriseSearchMetadataCrawledProperty -Name $propName -PropSet $propSetId -Category SharePoint -IsNameEnum $false -VariantType 0 -SearchApplication $searchServiceApp

  2. Open property in Central Admin, add mapping (any), save. This will create property in database and you will be able to use it in PowerShell afterwards.

  • My crawled properties are already picked up in CA. I just can't reach them in PowerShell. – Batman Mar 23 '17 at 2:05
  • And I explained you why... and possible solutions. If it sounds too weird for you, just try to add mapping via CA to one one of them. Suddenly, it'll become available in PowerShell. – Alex Burdin Mar 23 '17 at 8:11
  • So instead of waiting for a full crawl to find my properties, I can just create them ahead of time and they'll match up correctly if the naming is the same? – Batman Mar 23 '17 at 14:50
  • Yes, but not only name should be the same, also PropertySet and Category. Both can be found when you click on property in CA. – Alex Burdin Mar 24 '17 at 2:50
3

For SharePoint 2016 you can take implicit search crawled properties through Search Service Application proxy.

$crawledPropertyName = "Crawled property name"
$searchApplicationName = "Search application name"

$searchApp = Get-SPEnterpriseSearchServiceApplication -Identity $searchApplicationName 
$searchAppProxy = Get-SPEnterpriseSearchServiceApplicationProxy -Identity $($searchApp.Name + " Proxy")

$owner = [Microsoft.Office.Server.Search.Administration.SearchObjectOwner]::new([Microsoft.Office.Server.Search.Administration.SearchObjectLevel]::Ssa)
$categories = $searchApp.GetAllCategories($owner)

$crawledProperties = New-Object "System.Collections.Generic.List[Object]"
foreach($category in $categories)
{
    $crawledProperties.AddRange($searchAppProxy.QueryCrawledProperties($category, $crawledPropertyName, 1000, [GUID]"00000000-0000-0000-0000-000000000000", [string]::Empty, $true, $false, $owner))
}

$crawledPropertyInfoImplicit = $crawledProperties | Where-Object { $_.Name -eq $crawledPropertyName }
$crawledPropertyInfo = $searchApp.PromoteImplicitCrawledProperty($crawledPropertyInfoImplicit, $owner)

$crawledProperty = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchApp -name $crawledPropertyName
0

Had the same issue and used Alex's answer to update the PS1 code that the OP was using. Here's my updated version if it can help someone:

-----------------------------------------------------------------------------

Name: export-import-mp.ps1

Description: This script has three switch to:

- export a list of managed properties (optionally filtered)

- import a list of managed properties with crawled property mapping

- delete a list of managed properties

Usage: Run the script passing paramters ServiceApp, Import|Export|Delete and Filter

By: Riccardo Celesti blog.riccardocelesti.it

# #

REF: https://gallery.technet.microsoft.com/office/Powershell-script-to-09ffa974

.\export-import-mp.ps1 -serviceapp "Search Service Application" -export -filter tq-

.\export-import-mp.ps1 -serviceapp "Search Service Application" -import

-----------------------------------------------------------------------------

Param([Parameter(Mandatory=$true)] [String]$serviceapp, [Parameter(Mandatory=$false,ParameterSetName='Export')] [Parameter(Mandatory=$true,ParameterSetName ="Delete")] [String]$filter, [Parameter(ParameterSetName='Import')] [switch]$import, [Parameter(ParameterSetName='Export')] [switch]$export, [Parameter(ParameterSetName='Delete')] [switch]$delete )

if ((gsnp MIcrosoft.SharePoint.Powershell -ea SilentlyContinue) -eq $null){ asnp MIcrosoft.SharePoint.Powershell -ea Stop }

$logfile = ".\export-managed-properties.csv" $importlog = ".\import-managed-properties.log" $importlogerr = ".\import-managed-properties-errors.log"

function ManagedPropertyTypes($type){ switch ($type){
"text" {$type = 1}
"integer" {$type = 2}
"decimal" {$type = 3} "DateTime" {$type = 4} "YesNo" {$type = 5} "Binary" {$type = 6} "Double" {$type = 7} default {$type = 1} }

return $type

}

if ((Get-SPEnterpriseSearchServiceApplication $serviceapp -ea SilentlyContinue) -eq $null){ Write-Host "Enterprise Search Service Application $serviceapp has not been found" -ForegroundColor Red exit } else { $ssa = Get-SPEnterpriseSearchServiceApplication $serviceapp

Write-Host "Enterprise Search Service Application $serviceapp has been found" -ForegroundColor Green

}

if ($export){ if ((Get-ChildItem -Name $logfile -ea SilentlyContinue) -ne $null){ Clear-Content $logfile ac $logfile "Name,Description,ManagedType,Searchable,FullTextQueriable,Queryable,Retrievable,Refinable,Sortable,HasMultipleValues,SafeForAnonymous,Mapping"; } else { ac $logfile "Name,Description,ManagedType,Searchable,FullTextQueriable,Queryable,Retrievable,Refinable,Sortable,HasMultipleValues,SafeForAnonymous,Mapping"; }

if ($filter -ne $null){
    Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Limit All | ?{$_.Name -like "$($filter)*"} | %{

        $mp = $_;
        $mpmap = @();

        Get-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $mp.Name | %{
            $mpmap += $_.CrawledPropertyName
        }

        ac $logfile "$($mp.Name),$($mp.Description),$(ManagedPropertyTypes $mp.ManagedType),$($mp.Searchable),$($mp.FullTextQueriable),$($mp.Queryable),$($mp.Retrievable),$($mp.Refinable),$($mp.Sortable),$($mp.HasMultipleValues),$($mp.SafeForAnonymous),$($mpmap -join '|')"

    }
} else {
    Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Limit All | %{

        $mp = $_;
        $mpmap = @();

        Get-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $mp.Name | %{
            $mpmap += $_.CrawledPropertyName
        }

        ac $logfile "$($mp.Name),$($mp.Description),$(ManagedPropertyTypes $mp.ManagedType),$($mp.Searchable),$($mp.FullTextQueriable),$($mp.Queryable),$($mp.Retrievable),$($mp.Refinable),$($mp.Sortable),$($mp.HasMultipleValues),$($mp.SafeForAnonymous),$($mpmap -join '|')"
    }
}

}

if ($import){ if ((Get-ChildItem $logfile -ea SilentlyContinue) -eq $null){ Write-Host "The export file has not been found" -ForegroundColor Red exit } else {

    Import-Csv $logfile -Delimiter "," | %{
        $mp = $_;

        if ((Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Identity $mp.Name -ea SilentlyContinue) -eq $null){
            try {
                $newmp = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Name $mp.Name -Description $mp.Description -Type $mp.ManagedType -FullTextQueriable:([bool]::Parse($mp.FullTextQueriable)) -Retrievable:([bool]::Parse($mp.Retrievable)) -Queryable:([bool]::Parse($mp.Queryable)) -SafeForAnonymous:([bool]::Parse($mp.SafeForAnonymous)) -EA Stop
                ac $importlog "$(Get-Date),$($mp.Name),[INF],Managed Property,"

                #$ump = Get-SPEnterpriseSearchMetadataManagedProperty $mp.Name -SearchApplication $ssa  
                $newmp.Searchable = ([bool]::Parse($mp.Searchable))
                $newmp.Refinable = ([bool]::Parse($mp.Refinable))
                $newmp.Sortable = ([bool]::Parse($mp.Sortable))
                $newmp.HasMultipleValues = ([bool]::Parse($mp.HasMultipleValues))
                $newmp.Update() 
                } catch {
                Write-Host "Something went wrong :) $($Error[0].Exception.Message)" -ForegroundColor Red
                ac $importlog "$(Get-Date),$($mp.Name),[ERR],"
                ac $importlogerr "$(Get-Date),$($mp.Name),[ERR],Managed Property,$($Error[0].Exception.Message)"
            }
        } else {
            $newmp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Identity $mp.Name -ea SilentlyContinue
            Write-Host "Managed property $($mp.Name) already exists" -ForegroundColor Yellow
            ac $importlog "$(Get-Date),$($mp.Name),[WRN],Managed Property,Managed property already exists"
        }
        if (($mp.Mapping).Length -gt 0){

            $cps = $mp.Mapping

            foreach ($term in $cps.split("|")){

                Write-Host "Processing Crawled Property $($term) on Managed Property $($mp.Name)" -ForegroundColor Cyan

                if ((Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $ssa -Name $term -EA SilentlyContinue) -eq $null)
                {
                    try{
                        New-SPEnterpriseSearchMetadataCrawledProperty -Name $term -PropSet "00130329-0000-0130-c000-000000131346" -Category SharePoint -IsNameEnum $false -VariantType 0 -SearchApplication $ssa
                    }
                    catch{
                        Write-Host "Crawled property $($term) does not exists and cannot create" -ForegroundColor Red
                        ac $importlog "$(Get-Date),$($mp.Name),[ERR],Crawled Property,$term"
                        ac $importlogerr "$(Get-Date),$($mp.Name),[ERR],Crawled Property,$term does not exists"
                    }
                }

                $cp = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $ssa -Name $term

                try {
                    New-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $newmp -CrawledProperty $cp[0] -EA Stop
                    ac $importlog "$(Get-Date),$($mp.Name),[INF],Metadata Mapping,$term"

                    if($cp -is [system.array]){
                        ac $importlog "$(Get-Date),$($mp.Name),[WRN],Metadata Mapping,$term,More than one crawled property selected only the first has been used" 
                    }
                } catch {
                    Write-Host "Something went wrong :) $($Error[0].Exception.Message)" -ForegroundColor Red
                    ac $importlog "$(Get-Date),$($mp.Name),[ERR],Metadata Mapping,$term"
                    ac $importlogerr "$(Get-Date),$($mp.Name),[ERR],Metadata Mapping,$($Error[0].Exception.Message)"
                }
            }
        }

    }
}

}

if ($delete){ Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Limit All | ?{$.Name -like "$($filter)*"} | %{ $mp = $; $mp.DeleteAllMappings(); $mp.Delete(); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.