Export/Import Crawled Properties

Leave a comment

February 3, 2014 by Ravi C Khambhati

If you have ever worked with SharePoint search you must have come across this scenario that in order to create crawl property you have crawl. Also if you have fresh site collection you will not have data and in that case crawl will not create crawl property so in order to create crawl property you have to have data as well.

So I came up with the export/import process in which you can export all crawl property from one server(DEV Machine) once you have that you can create these crawl property to any server and you do not have to crawl to create crawl property.

Step 1: Upload/Add data to your library/list
Step 2: Start full crawl
Step 3: Run below PowerShell script to export crawl property. This script will create xml file with all crawl properties.

param([String]$serviceApplication, [String]$category)

function WriteToFile
{
	param([string]$fileName, [string]$content)
	
	$fileObj = New-Object System.IO.StreamWriter($fileName, $true)
	try
	{
		$fileObj.WriteLine($content)
	}
	finally
	{
		if($fileObj -ne $null)
		{
			$fileObj.Dispose()	
		}
	}
}

function GetUniqueFileNameWithCurrentPath
{
	param([string]$prefix, [string]$fileFormat, [string]$extention)
	
	$currentPath = Get-Location | select Path
	$fileName = [datetime]::Now.ToString($fileFormat)
	$filePath = $currentPath.Path + "\" + $prefix + "_" + $fileName + "." + $extention
	
	return $filePath
}

CLS

if ( [String]::IsNullOrEmpty($serviceApplication) )
{
	Write-Host "Input parameter missing: Service Application Name" -ForegroundColor Red
	return
}

if ( [String]::IsNullOrEmpty($category) )
{
	Write-Host "Input parameter missing: Category" -ForegroundColor Red
	return
}

$searchapp = Get-SPEnterpriseSearchServiceApplication -Identity $serviceApplication
$cat = Get-SPEnterpriseSearchMetadataCategory -SearchApplication $searchapp -Identity $category

$filePath = GetUniqueFileNameWithCurrentPath "CrawlProperty" "yyyy-MM-dd_hhmmss" "xml"
$content = "<?xml version=""1.0"" encoding=""utf-8"" ?>" + `
			"<CrawlProperties><SearchServiceApplication>" + $serviceApplication + "</SearchServiceApplication>"
WriteToFile $filePath $content

$crawlProperties = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $cat

foreach($crawlProperty in $crawlProperties)
{
	$crawlPropertyName = [System.Security.SecurityElement]::Escape($crawlProperty.Name)
	Write-Host $crawlPropertyName
	
	$content = [String]::Format("<CrawlProperty><Name>{0}</Name><CategoryName>{1}</CategoryName><Propset>{2}</Propset>" + `
	"<IsMappedToContents>{3}</IsMappedToContents><IsNameEnum>{4}</IsNameEnum><SchemaId>{5}</SchemaId>" + `
	"<VariantType>{6}</VariantType></CrawlProperty>", `
	$crawlPropertyName, $crawlProperty.CategoryName, $crawlProperty.Propset, $crawlProperty.IsMappedToContents, $crawlProperty.IsNameEnum,`
	$crawlProperty.SchemaId, $crawlProperty.VariantType)
	
	WriteToFile $filePath $content
}

$content = "</CrawlProperties>"
WriteToFile $filePath $content

Write-Host "Exported to '$filePath'" -ForegroundColor Green

Sample export output

<?xml version="1.0" encoding="utf-8" ?>
<CrawlProperties>
  <SearchServiceApplication>Search Service Application</SearchServiceApplication>
  <CrawlProperty>
    <Name>2147418090</Name>
    <CategoryName>SharePoint</CategoryName>
    <Propset>00020329-0000-0000-c000-000000000046</Propset>
    <IsMappedToContents>False</IsMappedToContents>
    <IsNameEnum>False</IsNameEnum>
    <SchemaId>0</SchemaId>
    <VariantType>0</VariantType>
  </CrawlProperty>
  <CrawlProperty>
    Other crawl properties
  </CrawlProperty>
</CrawlProperties>

Step 4: Now you can edit exported xml file created in above step if you want. You can remove some of the entries if you do not want that to be part of your script.
Step 5: Log in to any server and run below PowerShell script with two input parameter.
First: Exported XML File
Second: Remove if exists(Boolean). If $true then script will recreate crawl property else will not create one

param([String]$fileName, $removeIfExist)

cls

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{    
      Add-PSSnapin Microsoft.SharePoint.PowerShell
}

if ( [String]::IsNullOrEmpty($fileName) )
{
	Write-Host "Input 'File' parameter is missing" -ForegroundColor Red
	return
}

if ( $removeIfExist -eq $null )
{
	Write-Host "Input 'RemoveIfExist' parameter is missing" -ForegroundColor Red
	return
}

Write-Host 
Write-Host "FileName:" $fileName -ForegroundColor Green

try
{
	[xml]$root = Get-Content $fileName
	
	$serviceApplicationName = $root.CrawlProperties.SearchServiceApplication
	Write-Host "Service Application:" $serviceApplicationName -ForegroundColor Green
	
	$serarchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $serviceApplicationName -ErrorAction SilentlyContinue
	
	if ( $serarchServiceApplication -eq $null )
	{
		Write-Host "Service Application not found" -ForegroundColor Red
		return
	}
	
	foreach($crawlProperty in $root.CrawlProperties.CrawlProperty)
	{
		$crawlPropertyName = $crawlProperty.Name
		$crawlPropertyCategoryName = $crawlProperty.CategoryName
		
		$crawlPropertyCategory = Get-SPEnterpriseSearchMetadataCategory -Identity $crawlPropertyCategoryName -SearchApplication $serviceApplicationName  
    	$cp = Get-SPEnterpriseSearchMetadataCrawledProperty -Name $crawlPropertyName -SearchApplication $serviceApplicationName -Category $crawlPropertyCategory
		
		if ( $cp -ne $null -and $removeIfExist -eq $true )
		{
			Write-Host "Removing $crawlPropertyName" -ForegroundColor Green
			
			$mappings = Get-SPEnterpriseSearchMetadataMapping -SearchApplication $serviceApplicationName -CrawledProperty $cp  
	        if ($mappings)
			{  
	            $mappings | Remove-SPEnterpriseSearchMetadataMapping -Confirm:$false
	        }  
	        else  
	        {  
	            Write-Host "No mappings found for '$crawlPropertyName'." -foregroundcolor yellow  
	        }
			$cp.IsMappedToContents = $false
	        $cp.Update()  
	        $crawlPropertyCategory.DeleteUnmappedProperties()
			Write-Host "Removed" -ForegroundColor Green
		}
		else
		{
			Write-Host "'$crawlPropertyName' already exist" -foregroundcolor yellow
		}
		
		if ( $removeIfExist -eq $true )
		{
			Write-Host "Creating crawl property $crawlPropertyName" -ForegroundColor Green
			
			$crawlPropertyPropset = $crawlProperty.Propset
			$crawlPropertyIsNameEnum = [Convert]::ToBoolean($crawlProperty.IsNameEnum)
			$crawlPropertyVariantType = $crawlProperty.VariantType
			$crawlPropertyIsMappedToContents = [Convert]::ToBoolean($crawlProperty.IsMappedToContents)
			
			$newCP = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $serviceApplicationName -Name $crawlPropertyName -PropSet $crawlPropertyPropset `
			-Category $crawlPropertyCategoryName -IsNameEnum $crawlPropertyIsNameEnum -VariantType $crawlPropertyVariantType -IsMappedToContents $crawlPropertyIsMappedToContents
			
			Write-Host "Created" -ForegroundColor Green
		}
	}
	
	Write-Host "COMPLETED" -ForegroundColor Green
}
catch [Exception]
{
	Write-Host "Error in creating crawl property" -ForegroundColor Red
	Write-Host $_.Exception
}

The same way you can also export / import managed property.

Share your comments/suggestions

Google+

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: