Tuesday, November 1, 2011

How to remove SharePoint crawled properties

Recently I've been trying to remove crawled properties in one of my projects. The problem is that SharePoint 2010 doesn't have out of the box functionality to remove crawled properties. I tried to delete the whole category with all crawled properties but SharePoint didn't let me do that saying that it's not possible to delete not empty category. It's pretty obvious, but just in case I checked category deletion in Reflector:










So no luck here as well. It throws exception when finds crawled properties within this category after it tried to delete them.


I started to dig into this issue and found the following blog post that helped me to find a solution:

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/26/how-to-delete-crawled-properties.aspx

Unfortunately the solution above doesn't work as expected, even when there are no managed properties mapped to this crawled property. The thing is that the stored procedure that deletes unmapped properties (proc_MSS_DeleteCrawledPropertiesUnmappedForCategory) performs the following check - CP.IsMappedToContent = 0:

  1. Delete dbo.MSSCrawledProperties   
  2.    Where CrawledPropertyId in  
  3.    ( Select CP.CrawledPropertyId   
  4.       From MSSCrawledProperties as CP  
  5.       INNER JOIN dbo.MSSCrawledPropCategory as C  
  6.          on CP.Propset = C.Propset  
  7.       WHERE C.CategoryName = @CategoryName   
  8.         AND CP.IsMappedToContent = 0  
  9.         AND NOT EXISTS   
  10.           ( Select * from MSSSchemaPropertyMappings   
  11.             Where CrawledPropertyId = CP.CrawledPropertyId )   
  12.    )  
But for some crawled properties this value is always set to 1 even without mapped managed properties. So we need to set this value to 0 somehow. Fortunately Powershell comes to the rescue. There is IsMappedToContents property of crawled property object. And it's possible to set it to 0. Here is full piece of code with minimum checks that allows to remove crawled property.
  1. $searchAppName = "Search Service Application"  
  2. $categoryName = "Business Data"  
  3.   
  4. function RemoveCrawledProperty($crawledPropertyName)  
  5. {  
  6.     $category = Get-SPEnterpriseSearchMetadataCategory -Identity $categoryName -SearchApplication $searchAppName  
  7.     $crawledProperty =  
  8.         Get-SPEnterpriseSearchMetadataCrawledProperty -Name $crawledPropertyName -SearchApplication $searchAppName -Category $category  
  9.     if ($crawledProperty)  
  10.     {  
  11.         $mappings = Get-SPEnterpriseSearchMetadataMapping -SearchApplication $searchAppName -CrawledProperty $crawledProperty  
  12.         if ($mappings)  
  13.         {  
  14.             $mappings | Remove-SPEnterpriseSearchMetadataMapping -Confirm:$false  
  15.         }  
  16.         else  
  17.         {  
  18.             Write-Host "No mappings found for '$crawledPropertyName'." -foregroundcolor yellow  
  19.         }  
  20.         $crawledProperty.IsMappedToContents = $false  
  21.         $crawledProperty.Update()  
  22.         $category.DeleteUnmappedProperties()  
  23.     }  
  24.     else  
  25.     {  
  26.         Write-Host "Crawled property '$crawledPropertyName' not found." -foregroundcolor yellow  
  27.     }  
  28. }  
Hope it will be helpful to someone.

5 comments:

  1. great work!
    thanks

    ReplyDelete
  2. Thanks, helpful to me.

    ReplyDelete
  3. Insane stuff ! finally stumbled on THE SOLUTION !!!!! many thanks

    ReplyDelete
  4. Hi Vadim,
    I get an error in PS console as "Cannot convert 'System.Object[]' to the type 'Microsoft.Office.Server.Search.Cmdlet.CrawledPropertyPipeBind' required by parameter 'CrawledProperty'."

    I checked on "Get-SPEnterpriseSearchMetadataMapping" in msdn, it doesnt seem to have a parameter for -CrawledProperty . It only has -ManagedProperty
    I'm using SP 2013. Would you be able to shed some light on this...

    ReplyDelete