Monday, December 12, 2011

Powershell and search metadata properties

Scenario:
While working with one of client we created a quick script for creating / recreating metadata properties for search. Thought of sharing :)

This script will drop exiting property with same name [ if exist ] and create it again. Also I have provided samples of different field types.

Code:


#####################################################################
# Main Function
#####################################################################

function PromptUser {param($message)

write-host -f Red $message

write-host -f Red "Press enter to continue ... and N to exit"

$keyentered = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

If ($keyentered.VirtualKeyCode -eq 78)
{
exit
}
}

function DropMetadataProperties {param ($ServiceApplicationName, $MetadataPropertyName)

$category = Get-SPEnterpriseSearchMetadataCategory –Identity "Business Data" -SearchApplication $searchapp

If (Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $MetadataPropertyName -ea "silentlycontinue")
{
write-host -f Green "MetaProperty already exists, we delete it so it can be reacreated."
$prop = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $MetadataPropertyName
$prop.DeleteAllMappings()
$prop.Delete()
$searchapp.Update()
}

if (!$?)
{
PromptUser("An error has occurred")
}
}

function CreateMetadataProperties {param ( $ServiceApplicationName, $MetadataPropertyName, $CrawledProperty, $VariantType, $Type, $HasMultipleValues, $IsSortable)

$category = Get-SPEnterpriseSearchMetadataCategory –Identity "Business Data" -SearchApplication $searchapp

$crawledproperty = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType $VariantType -Name $CrawledProperty
If (!($crawledproperty -eq $null))
{
$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $MetadataPropertyName -Type $Type

If ($IsSortable -eq $True)
{
$managedproperty.MaxCharactersInPropertyStoreIndex = 64
$managedproperty.Update()
}

If ($HasMultipleValues -eq $True)
{
$managedproperty.HasMultipleValues = $true
$managedproperty.Update()
}


New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty
}
else
{
write-host -f Red "Crawl property is missing, you might need to recrawl your content source to get it."
}

if (!$?)
{
PromptUser("An error has occurred")
}
}

###################################################################################
# Drop metadata properties
###################################################################################

$searchapp = Get-SPEnterpriseSearchServiceApplication
if (!$?)
{
PromptUser("Could not find a valid Search Service Application.")
EXIT
}

$searchAppName = $searchapp.Name


# Client Metadata properties
DropMetadataProperties $searchAppName "xxClientName"

###################################################################################
# Create metadata properties
###################################################################################

# VariantType = (31 for string , 20 for integer , 11 for boolean , 64 for datetime )
# Type = (1 for string , 2 for integer , 5 for Yes/No , 4 for datetime )

### Text Field
DropMetadataProperties $searchAppName "xxClientName"
CreateMetadataProperties $searchAppName "xxClientName" "Client.ClientName" 31 1 $False $True

### Integer Field
DropMetadataProperties $searchAppName "xxClientID"
CreateMetadataProperties $searchAppName "xxClientID" "Clients.ClientID" 20 2 $False $False

### YES/No Field
DropMetadataProperties $searchAppName "xxIsActive"
CreateMetadataProperties $searchAppName "xxIsActive" "Clients.IsActive" 11 5 $False $False

### Date Field
DropMetadataProperties $searchAppName "xxEndDate"
CreateMetadataProperties $searchAppName "xxEndDate" "Clients.EndDate" 64 4 $False $False
Please do leave comments on if it worked for you or any suggestion to improve it.

1 comments:

Nadeem Yousuf January 30, 2012 at 6:24 AM  

Great script. Makes life easier as it is time consuming to create metadata properties manually. One modification I made to the script was to allow for multiple mappings:
$managedproperty = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $MetadataPropertyName
If ($managedproperty -eq $null)
{
$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $MetadataPropertyName -Type $Type
}