in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

Creating Enterprise Search Metadata Property Mappings with PowerShell

One of the things that drove me absolutely nuts about Enterprise Search in MOSS 2007 was that there was no built-in way to export your managed property mappings and install them on a new server.  A third party utility on CodePlex helped, but it was still less than ideal.  With SharePoint 2010, well you still really can’t export your property mappings to a file, but you do get a lot of flexibility using PowerShell.  By taking a proactive approach, we can use PowerShell instead of the UI to create our property mappings. We can then use these same scripts later when its time to move these settings to production.  The SDK does a pretty good job with examples on how to use all of the commands we need, but I found a few minor errors and omissions.  We’ll look at some of the individual commands and then put it all together.  Our goal at the end of this is to have a .ps1 script that you can store in your source control system.

The first thing we need to do is get a reference to our search service application.  By default, it just so happens to be called Search Service Application.  However, if you installed it manually, or are using FAST, it is likely to be called something different.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"

We assign this to a variable called $searchapp (or whatever you want) so that we can reference it later in the script.  In today’s example, I have some site columns on a document library that I want to use as managed properties.  In this case I have site columns called TestProperty1 and TestProperty2.  If I were to perform a full crawl, Enterprise Search would automatically create crawled properties called ows_TestProperty1 and ows_TestProperty2.  However, I want to save myself some time and skip that full crawl.  This is especially valuable when you have a large index.  Luckily, I can just create these crawled properties using PowerShell using the New-SPEnterpriseSearchMetadataCrawledProperty command (i know that’s a lot to type).  The syntax is pretty simple, but there are a few gotchas.  First the VariantType parameter isn’t entirely obvious.  I know 31 is text, but I’m not sure exactly what value to use on other types.  The link from the SDK give you quite a few values (note they are all in hex), but I still need to figure out the rest of the values sometime.  The other thing is the PropSet parameter is marked as optional when in fact it is required.  I’m really not sure what value you should use here and when so I just used the same GUID most crawled properties used (viewable from the UI), 00130329-0000-0130-c000-000000131346.  We also have to get a reference to the crawled property category that we want (in this case SharePoint) so we call Get-SPEnterpriseSearchMetadataCategory first and pass that to the Category attribute.   Here is what the commands looks like to create TestProperty2.

$category = Get-SPEnterpriseSearchMetadataCategory –Identity SharePoint -SearchApplication $searchapp

$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name ows_TestProperty2 -IsNameEnum $false

You can verify that it was created in SharePoint using the UI.

EnterpriseSearchCrawledProperty

Now, we need to create a managed property using the New-SPEnterpriseSearchMetadataManagedProperty command.  The documentation on the Type parameter for this command is a bit sketchy too.  Currently, it lists the six managed property types Text, Integer, Decimal, DateTime, YesNo, Binary, but it doesn’t say what the value for each one is (the parameter requires an int).  From the example, they give I was able to determine that 1 is a test.  At some point, I’ll go find the enum in the SDK and see what the rest of the values are.  The rest of the command is pretty simple, just specify a name and pass a reference to the service application.

$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name TestProperty2 -Type 1

You may have noticed I am assigning the result of each New command to a variable.  This is so that we can pass that in to the New-SPEnterpriseSearchMetadataMapping command.  Just pass in references to the service application, managed property, and crawled property and you are done.

New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty

Here is what the command looks like in PowerShell.  It simply gives you info on what was created.

PowerShellCreateManagedPropertyMapping

If you need to map multiple crawled properties to a managed property simply call the command repeatedly with a reference to each crawled property.  Once you finish, you can verify in the UI that the managed property was mapped successfully.

PowerShelllManagedProperty

At this point, you need to do a full crawl to make use of your managed properties.  Here is what the whole script looks like put together with multiple mappings.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"

$category = Get-SPEnterpriseSearchMetadataCategory –Identity SharePoint -SearchApplication $searchapp

$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name ows_TestProperty1 -IsNameEnum $false

$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name TestProperty1 -Type 1

New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty

 

$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name ows_TestProperty2 -IsNameEnum $false

$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name TestProperty2 -Type 1

New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty

$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name ows_TestProperty2a -IsNameEnum $false

New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty

It seems like a lot but it really isn’t.  I create two managed properties (TestProperty1 and TestProperty2).  In the case of TestProperty2, I actually map two crawled properties to it. 

As far as I am concerned this is the only way to go from now on when it comes to doing property mappings.  Once you have the script down, it’s much faster than clicking through the UI and of course you can run it on other servers.  Be sure and give it a try as you start setting up search on all of your new SharePoint 2010 deployments.

Comments

 

Creating Crawled Properties « i:0#.f|SharePoint|Development said:

Pingback from  Creating Crawled Properties « i:0#.f|SharePoint|Development

June 29, 2010 3:05 PM
 

Greg said:

Thanks for posting this article.  I found it very helpful in creating 30+ metadata properties that I thought I was going to have to do through the GUI.

The one parameter I still don't understand how to set is the one to check the option for "Add managed property to custom results set retrieved on each query".  

October 11, 2010 9:37 AM
 

jaloplo said:

This is a good article but I tried one thing that doesn't work properly. If you want to assign some crawled properties don't follow what the article says because you'll get an error.

March 16, 2011 6:47 PM
 

CoreyRoth said:

@jalopo what error are you receiving?

March 22, 2011 11:28 PM
 

jaloplo said:

Sorry about my comment. It can be deleted if you want beacuse the powershell sentence really works. I apologize about the comment but I took the previous command.

March 23, 2011 9:22 AM
 

M.L.F. said:

I found this to work for getting the int value of the managed property type

[Microsoft.Office.Server.Search.Administration.Manag

edDataType]::YesNo.value__

April 28, 2011 1:56 PM
 

Pagial said:

If you are mapping DateTime fields, VariantType should be 64. 31 wont work.

August 11, 2011 5:59 AM
 

Brad said:

Hi Corey,

When you create a crawled property you do have the option in SP2010 of creating its category of type People or of Type Sharepoint. I made this mistake when playing around in code, so I had 2 indetically named crawled properties that look the same just different PropSets. I think a problem will then occur when you try to map the crawled property mapping to a managed property as sharepoint does not know which of the 2 identically named crawled properties to use and i get an error.

New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $pManagedProp -CrawledProperty $pcrawledproperty

i.e. how can you be more specific on the crawled property, the help says you can use a GUID but this cannot be the propset as several crawled props can have the same one.

August 17, 2011 7:57 AM
 

CoreyRoth said:

@Pagial thanks for the clarification.

August 30, 2011 10:55 AM
 

Brad said:

Hi Corey,

Just updating / supplying solution to my earlier quetion on August 17th, What i needed to do to fix this was  first of all specify the category as "people" and then use this category again when i retrieved the crawled property. Without specifying category sharepoint got confused as it hit 2 properties of the same name, one for people, one for sharepoint.

thanks for the great blogs.

Brad

$cat = Get-SPEnterpriseSearchMetadataCategory -SearchApplication $searchapp -Identity People

$crawledproperty = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $psearchapp -Name $pcrawledprop -Category $cat

September 2, 2011 9:41 AM
 

Mark Stokes said:

Hey Corey,

I have worked out the Int32 values for the -Type attribute when creating new Managed Properties.  They are on my blog here:

www.sharepointstudio.com/.../Post.aspx

Hope this helps.

@MarkStokes

September 26, 2011 10:46 AM
 

CoreyRoth said:

Good work Mark! That's helpful!

September 27, 2011 6:04 PM
 

MattiP said:

Hi Corey,

Thanks for this article, this was just what I needed.

There was a question earlier about "Add managed property to custom results set retrieved on each query". I have also try to find out how to set that with Powershell. It's the only thing I'm missing. Have you or anyone found a solution for that?

Matti

November 9, 2011 10:51 PM
 

Juan Romero said:

In order to toggle the ""Add managed property to custom results set retrieved on each query"", you need to set the PutInPropertyBlob to 0 (unchecked) or 1 (checked)

$mp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $name) -ea "silentlycontinue")

$mp.PutInPropertyBlob = 1

$mp.Update()

March 1, 2012 4:58 PM
 

Puneet said:

Hi Juan Romero,

Thanks a lot for this information. Very helpful.

Kind Regards,

Puneet

April 10, 2012 10:33 AM
 

Praveen said:

HI Corey ,

I`m in big problem , I have exported the Managed ,Crawled Propertes from moss 2007  using Codeplex tool .but how to move this Managed ,Crawled Propertes to Sharepoint 2010 ?, am bit new powershell scripting ....any sugesstion ,any help......my email

July 26, 2012 11:53 PM
 

CoreyRoth said:

@Praveen I am afraid there is no good way to move managed properties from 2007 to 2010.  It usually is a manual operation.

August 6, 2012 11:14 AM
 

Name of the blog said:

Data Type values for creating Managed Properties from PowerShell

March 23, 2013 4:33 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an Applications Architect at Infusion specializing in ECM and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems