Friday, May 6, 2016

Mapping SharePoint 2013 Crawled Property to Managed Property for Search-driven Applications

Search-driven applications are a great way to utilize the powerful SharePoint search features to surface content. SharePoint offers sleek methods to extend search to create search-based solutions and applications.
When you use the Search Results web part to display the search-driven content, you might want to sort or filter the search results. These results can be sorted and filtered on managed properties. However, you have your data fields already in-use as crawled properties. To get the Search Results web part to work with your crawled properties, you need to map those crawled properties to managed properties. You can do this from the Site Collection Settings by going to Search Schema.
If you want to do this programmatically, the following PowerShell script will do it for you.

How to use the script:

  • Save the script as “MapCrwlProp2MgdProp.ps1”
  • Open PowerShell and go to the directory where the script is saved.
  • Modify the parameters and run the following command “.\MapCrwlProp2MgdProp.ps1 -CrwProp “Crawled Property” -MgdProp “ManagedProperty” -DataType 2”
The script checks if the Managed Property exists or not. If it doesn’t exist, it creates the managed property and maps it to the crawled property, simple eh!

<# Example: .\MapCrwlProp2MgdProp.ps1 -CrwProp "Crawled Property" -MgdProp "ManagedProperty" -DataType 2 
Parameters: DataType 1 - Text
2 - Integer
3 - Decimal
4 - Date & Time
5 - Yes/No
6 -
Double precision float
7 - Binary 
CrwProp - Crawled Property 
MgdProp - Managed Property 
Variables:
$SSAName = Search Service
Appication Name. By default, it is "Search Service Application". If it is different in your case, please change it in the script.
$ManagedMetadataCategory = Managed Metadata Category. By default, it is "SharePoint". If it is different in your case, please change it in the script. #>

Param(
[Parameter(Mandatory=$true)]
[string]$DataType,

[Parameter(Mandatory=$true)]
[string]$CrwProp,

[Parameter(Mandatory=$true)]
[string]$MgdProp
)

$SSAName = "Search Service Application"
$ManagedMetadataCategory = "SharePoint"

Function MapManagedProperty ($CrwProp, $MgdProp, $DataType)
{
$ssa = Get-SPEnterpriseSearchServiceApplication -Identity $SSAName
$cat = Get-SPEnterpriseSearchMetadataCategory -SearchApplication $ssa -Identity $ManagedMetadataCategory
$cct = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $ssa -Name $CrwProp -Category $cat

If ((Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Identity $MgdProp -ErrorAction SilentlyContinue) -eq $null)
{
Write-Host "Managed Property" + $MgdProp + "does not exist. The script will attempt to create it."
$mct = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $ssa -Name $MgdProp -Type $DataType -Queryable $true
}

New-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -CrawledProperty $cct -ManagedProperty $MgdProp
}

#Ensure SharePoint PowerShell snapin is loaded
if((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null){
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

MapManagedProperty $CrwProp $MgdProp $DataType

Write-Host "Property mapping is completed" -ForegroundColor Green

No comments:

Post a Comment