SharePoint Search and PowerShell Automation Script

I was working at a client where we had to rebuild their search service to try and alleviate some issues that they were experiencing and as part of this I came to the realization that manually creating managed properties has got to stop. The problem is that there is no easy way to move service scoped properties from one server to another. Also as a developer, the fact that this is not easily deployed bugs me. I want to be able to deploy the properties consistently. I don’t want to have to go back and create them again and wait for another full crawl because I missed one or typed the name in wrong. What’s worse is there is no way of  putting this configuration into version control to create a baseline configuration.

The fact that you can’t do this at the application level is a real problem. It is nice that you now do an export and import for configurations at the site collection and web level.

To solve this I setup a fairly simple PowerShell that I can simply specify the information and it will create all the properties that I need. To use this script  you just need to change the relevant information in the array at the beginning of the script and specify your Search Service Application name and then let her rip. This script will also add mapped crawl properties if the managed property already exists and you specify a crawled property that is not already mapped.

1. You need to get the search application by name.

$searchapp = Get-SPEnterpriseSearchServiceApplication “Search Service Application”

2. Now that you have the search application let’s setup our array that will be used for creating the properties.

$data = @(@{“CrawledProperties” = @(“CrawledPropertyName1″,”CrawledPropertyName2”); “ManagedPropertyName1” = “ManagedPropertyName1”; “Type” = 1; “Scoping” = $true; “Queryable”=$true;”Retrievable”=$true;”Refinable”=$true;”MulipleValues”=$false},
@{“CrawledProperties” = @(“CrawledPropertyName3”); “ManagedProperty” = “ManagedPropertyName2”; “Type” = 1; “Scoping” = $true; “Queryable”=$true;”Retrievable”=$true;”Refinable”=$true;”MulipleValues”=$false});

This array corresponds with the options found on the edit managed property page.

  • CrawledProperties – As it’s name implies you can have more than one. You can even have none if you want if you want to just create the managed properties now and map them later, just empty the array.
  • ManagedProperty – The name of the managed property that you want to create.
  • Type – Integer value to determine the data type of the property. The list of possible values can be found at http://technet.microsoft.com/en-us/library/ff608089.aspx. This article also list the other parameters available for the PowerShell commandlet that creates the property. Comes in handy if you want to add more functionality.
  • Scoping – Whether this property should be usable for scopes/result sources.
  • Queryable – Can this property be referenced in queries.
  • Retrievable – Can the data stored in this property be retrieved as part of search
  • Refinable – Can this property be used as a refiner
  • MultipleValues – Will this property contain multiple values

After the initial setup the script just loops through the data and creates the requested properties. As mentioned if the managed property already exists it will try and add the crawled properties to the map. If they already exist then the map script will ignore the already existing mapped crawled properties.

Feel free to let me know if there are any suggestions on improvements or problems.

Here is the script in all its glory.

# Create SharePoint Search Managed Properties
# Author: Ryan Schouten
# Created: October 2013
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$data = @(@{"CrawledProperties" = @("CrawledPropertyName1","CrawledPropertyName2"); "ManagedProperty" = "ManagedPropertyName1"; "Type" = 1; "Scoping" = $true; "Queryable"=$true;"Retrievable"=$true;"Refinable"=$true;"MulipleValues"=$false},
 @{"CrawledProperties" = @("CrawledPropertyName3"); "ManagedProperty" = "ManagedPropertyName2"; "Type" = 1; "Scoping" = $true; "Queryable"=$true;"Retrievable"=$true;"Refinable"=$true;"MulipleValues"=$false});
foreach($item in $data)
{
   $crawledProperties = $item["CrawledProperties"];
   $managedproperty = (Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $item["ManagedProperty"] -ErrorAction SilentlyContinue);
   if ($managedproperty -eq $null)
   {
       $managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $item["ManagedProperty"] -Type $item["Type"] -EnabledForScoping $item["Scoping"] -Queryable $item["Queryable"] -Retrievable $item["Retrievable"]

     if($item["Refinable"] -eq $true)
     {
       $managedproperty.Refinable = $true;
     }
     if($item["MulipleValues"] -eq $true)
     {
         $managedproperty.HasMultipleValues = $true;
     } 
     $managedproperty.Update();

     Write-Host "Managed Property " + $item["ManagedProperty"] + " created.";
   }
   else
   {
     Write-Host "Managed Property " + $item["ManagedProperty"] + " already exists.";
   }
   foreach($crawledName in $crawledProperties)
   {
     Write-Host "Crawled Property: $crawledName";
     $crawledProperty = Get-SPEnterpriseSearchMetadataCrawledProperty -Name $crawledName -SearchApplication $searchapp
     New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.