How to create managed properties at site collection level in SharePoint2013

Posted by ybbest on YBBest See other posts from YBBest or by ybbest
Published on Sun, 26 Aug 2012 03:17:44 +0000 Indexed on 2012/08/27 21:58 UTC
Read the original article Hit count: 442

In SharePoint2013, you can create managed properties at site collection. Today, I’d like to show you how to do so through PowerShell.

1. Define your managed properties and crawled properties and managed property Type in an external csv file. PowerShell script will read this file and create the managed and the mapping.

2. As you can see I also defined variant Type, this is because you need the variant type to create the crawled property. In order to have the crawled properties, you need to do a full crawl and also make sure you have data populated for your custom column. However, if you do not want to a full crawl to create those crawled properties, you can create them yourself by using the PowerShell; however you need to make sure the crawled properties you created have the same name if created by a full crawl.

Managed properties type:
Text = 1
Integer = 2
Decimal = 3
DateTime = 4
YesNo = 5
Binary = 6

Variant Type:
Text = 31
Integer = 20
Decimal = 5
DateTime = 64
YesNo = 11

3. You can use the following script to create your managed properties at site collection level, the differences for creating managed property at site collection level is to pass in the site collection id.

param( 
	[string] $siteUrl="http://SP2013/",
	[string] $searchAppName = "Search Service Application",
    $ManagedPropertiesList=(IMPORT-CSV ".\ManagedProperties.csv")
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$searchapp = $null

function AppendLog
{
	param ([string] $msg,            
			[string] $msgColor)

	$currentDateTime = Get-Date
	$msg = $msg + " --- " + $currentDateTime
	
	if (!($logOnly -eq $True))
	{
		# write to console
		Write-Host -f $msgColor $msg
	}

	# write to log file
	Add-Content $logFilePath $msg
}

$scriptPath = Split-Path $myInvocation.MyCommand.Path
$logFilePath = $scriptPath + "\CreateManagedProperties_Log.txt"

function CreateRefiner
{param ([string] $crawledName, [string] $managedPropertyName, [Int32] $variantType, [Int32] $managedPropertyType,[System.GUID] $siteID)
	
	$cat = Get-SPEnterpriseSearchMetadataCategory –Identity SharePoint -SearchApplication $searchapp
	
	$crawledproperty = Get-SPEnterpriseSearchMetadataCrawledProperty -Name $crawledName -SearchApplication $searchapp -SiteCollection $siteID

	if($crawledproperty -eq $null)
	{
		Write-Host
		AppendLog "Creating Crawled Property for $managedPropertyName" Yellow
		$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -VariantType $variantType -SiteCollection $siteID -Category $cat -PropSet "00130329-0000-0130-c000-000000131346" -Name $crawledName -IsNameEnum $false
	}

	$managedproperty = Get-SPEnterpriseSearchMetadataManagedProperty -Identity $managedPropertyName -SearchApplication $searchapp -SiteCollection $siteID  -ErrorAction SilentlyContinue

	if($managedproperty -eq $null)
	{
		Write-Host
		AppendLog "Creating Managed Property for $managedPropertyName" Yellow
		$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -Name $managedPropertyName -Type $managedPropertyType -SiteCollection $siteID -SearchApplication $searchapp -Queryable:$true -Retrievable:$true -FullTextQueriable:$true -RemoveDuplicates:$false -RespectPriority:$true -IncludeInMd5:$true 
	}

	$mappedProperty = $crawledproperty.GetMappedManagedProperties() | ?{$_.Name -eq $managedProperty.Name } 
	
	if($mappedProperty -eq $null)
	{
		Write-Host
		AppendLog "Creating Crawled -> Managed Property mapping for $managedPropertyName" Yellow
		New-SPEnterpriseSearchMetadataMapping -CrawledProperty $crawledproperty -ManagedProperty $managedproperty -SearchApplication $searchapp -SiteCollection $siteID
	}
	
	$mappedProperty = $crawledproperty.GetMappedManagedProperties() | ?{$_.Name -eq $managedProperty.Name }  #Get-FASTSearchMetadataCrawledPropertyMapping -ManagedProperty $managedproperty
}

$searchapp = Get-SPEnterpriseSearchServiceApplication $searchAppName 
$site= Get-SPSite $siteUrl
$siteId=$site.id

Write-Host "Start creating Managed properties"
$i = 1
FOREACH ($property in $ManagedPropertiesList) { 
    $propertyName=$property.managedPropertyName
    $crawledName=$property.crawledName
    $managedPropertyType=$property.managedPropertyType
    $variantType=$property.variantType
    Write-Host $managedPropertyType
    Write-Host "Processing managed property $propertyName  $($i)..."
	$i++
    CreateRefiner $crawledName $propertyName $variantType $managedPropertyType $siteId
	Write-Host "Managed property created " $propertyName
}

Key Concepts

Crawled Properties: Crawled properties are discovered by the search index service component when crawling content.

Managed Properties: Properties that are part of the Search user experience, which means they are available for search results, advanced search, and so on, are managed properties.

Mapping Crawled Properties to Managed Properties: To make a crawled property available for the Search experience—to make it available for Search queries and display it in Advanced Search and search results—you must map it to a managed property.

References

Administer search in SharePoint 2013 Preview

Managing Metadata


© YBBest or respective owner

Related posts about search

Related posts about sharepoint