About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Monday, 25 August 2014

Powershell Script - Create custom SharePoint 2013 search managed properties

The following script will create managed properties for search. The script will accept following input paramerers

1. Search Service application - Name of your search service application

Note: The script will refer .csv file which has input data. The column header format would be as follows, add your properties under this header and save file as .csv format.The file shoulb be available in the same directory.


Steps involved

1. Save following script as psCreateSearchManagedProperties.ps1

# Name:             psCreateSearchManagedProperties.ps1 
# Description:      This script will create a list of managed properties in
#                    the SharePoint 2013 Search Service Application
# Usage:            Run the script passing paramter SearchServiceApplication
# Created By:       Vamsi Mohan Mitta
# Date:             01/07/2014


if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){
    asnp Microsoft.SharePoint.Powershell

function Get-ScriptDirectory
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path

$currentPhysicalPath = Get-ScriptDirectory

$logfile=$currentPhysicalPath + "\log.log"

Start-Transcript $logfile

$objShell = New-Object -Com Shell.Application
$folder = $objShell.BrowseForFolder(0,"Please select the folder containing your CSV file:",0)
$pathfld = $folder.Self.Path
$fullpath = Join-Path $pathfld "managed-properties.csv"

$ssa = Get-SPEnterpriseSearchServiceApplication $SearchApplication

Import-Csv -Delimiter ";" -Path $fullpath | % {

    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}
    if ((Get-SPEnterpriseSearchMetadataManagedProperty $_.Name -SearchApplication $ssa -ea SilentlyContinue) -eq $null){
        try {
            New-SPEnterpriseSearchMetadataManagedProperty -Name $_.Name -SearchApplication $ssa -Type $type -Retrievable:([bool]::Parse($_.Retrieve)) -Queryable:([bool]::Parse($_.Query)) -SafeForAnonymous:([bool]::Parse($_.Safe)) -EA Stop
            $mp = Get-SPEnterpriseSearchMetadataManagedProperty $_.Name -SearchApplication $ssa
            $mp.Searchable = [bool]::Parse($_.Search)
            $mp.Refinable = [bool]::Parse($_.Refine)
            $mp.Sortable = [bool]::Parse($_.Sort)
            $mp.HasMultipleValues = [bool]::Parse($_.Multivalue)

            $cps = ($_.Properties).split(",")

            Write-Host "Managed property $($_.Name) successfully created." -ForegroundColor Green

            foreach ($cpname in $cps){
                try {
                    $cp = Get-SPEnterpriseSearchMetadataCrawledProperty $cpname -SearchApplication $ssa -EA Stop

                    try {
                        New-SPEnterpriseSearchMetadataMapping -SearchApplication $ssa -ManagedProperty $mp -CrawledProperty $cp

                        Write-Host "Mapping between Managed property $($_.Name) and crawled property $($cp.Name) completed successfully." -ForegroundColor Green
                    } catch {
                        Write-Host "Unable to map managed property $($_.Name) and crawled property $($cp.Name). $($error[0].Exception.Message)." -ForegroundColor Magenta
                } catch {
                    Write-Host "Crawled property $($cp.Name) not found. $($error[0].Exception.Message)." -ForegroundColor Magenta
        } catch {
            Write-Host "Unable to create managed property $($_.Name). $($error[0].Exception.Message)." -ForegroundColor Magenta
    } else {
        Write-Host "Managed property $($_.Name) already exists." -ForegroundColor DarkYellow

Echo Finish

Step2: Create batch file to run the script. Save file in .bat format.

cd /d %~dp0
powershell -file ./psCreateSearchManagedProperties.ps1 -SearchApplication "Search"

Happy powershell coding... please feel free to comment...

No comments:

Post a Comment