1

I created a Managed Property using the SharePoint 2013 UI, then I re-index and all works well.
Now I have been asked to create the same Managed Property using PowerShell script, but I have no idea where to begin. I would be very glad if someone will show me some example.

1 Answer 1

2

The following is a script from MSDN web site to create managed properties based on a CSV file:

Param([Parameter(Mandatory=$true)]  
      [String]  
      $SearchApplication 
)  

if ((gsnp Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null){ 
    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 "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) 
            $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 
    } 
}

The CSV file structure should be the following (First line defines the columns, by the way):

Name;Properties;Type;Sort;Retrieve;Refine;Search;Multivalue;Query;Safe;Abstract;
ows_Comments,ows_r_MTXT_Comments,ows_Abstract,ows_r_MTXT_Abstract;Text;FALSE;TRUE;FALSE;TRUE;FALSE;FALSE;TRUE
Anno;ows_Anno,ows_taxId_Anno;Text;TRUE;TRUE;TRUE;TRUE;FALSE;TRUE;TRUE

You run the script as follows:

./create-managed-properties.ps1 -SearchApplication "Search Service Application"

Credit: MSDN

3
  • Thanks you for the help. I get this exception: Import-Csv : The given path's format is not supported. At C:\Users\administrator\Desktop\create-managed-properties.ps1.ps1:17 char:1 + Import-Csv -Delimiter ";" -Path $fullpath | % { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (:) [Import-Csv], NotSupportedException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
    – Ado
    Commented Sep 22, 2016 at 8:56
  • Debug the script with PowerShell ISE. Put a breakpoint on line 15 and let me know what the $fullpath value is. Make sure your CSV file is named managed-properties.csv. Commented Sep 22, 2016 at 9:06
  • I get this: Set-PSBreakpoint : Cannot find path 'C:\Users\administrator\Desktop\create-managed-properties.ps1' because it does not exist.
    – Ado
    Commented Sep 22, 2016 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.