Subscribe:

Labels

Thursday, September 27, 2018

Creating Managed Metadata properties


#-----------------------------------------------------------------------------
# Name:             create-managed-properties.ps1 
# Description:      This script will create a list of managed properties in
#                                 the SharePoint 2013 Search Service Application
#                    
# Usage:            Run the script passing the paramter SearchApplication
# By:               Riccardo Celesti blog.riccardocelesti.it
#-----------------------------------------------------------------------------



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

$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 "C:\Ramesh\test.csv"
write-host $fullpath

$ssa = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application"

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}
    }
    Write-Host "MP - " + $_.Name;
      
    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)
            $mp.Update()

            $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
    }
}

No comments:

Post a Comment