Sunday, February 22, 2015

Create Crawled Property for Custom Column - SharePoint 2013


Create Site column "TestName" using following powershell script 
#Get site and web object
$site = Get-SPSite -Identity "http://MySiteurl"
$web = $site.RootWeb
#Assign fieldXML variable with XML string for site column
$fieldXML = '<Field Type="Text"
Name="TestName"
Description="This is a test date column."
DisplayName="Test Name"
StaticName="TestName"
Group="Test Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'
#Output XML to console
write-host $fieldXML
#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML)
#Example of changing the site column settings after creation
#Configure Test Date column to be Date Only instead of Date & Time
$dtField = $web.Fields["Test Name"]
$dtField.Update()
#Dispose of Web and Site objects
$web.Dispose()
$site.Dispose()
Open  Search Service application  in central admin and run incremental crawl. 
Under Crawled properties search for "TestName"
If missing custom site columns in crawled properties, you should:
  1. verify your custom fields is in use (in existing item or at least associated with visible content type)
  2. check your search settings and crawled category properties
  3. do a Search Index Reset
  4. perform a full crawl
Even after full crawl if you can't find your column in crawled properties then create it using following powershell script.

add-pssnapin microsoft.sharepoint.powershell

$searchapp = Get-SPEnterpriseSearchServiceApplication

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

$crawlprop = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $cat -Limit 1

New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name "ows_TestName" -PropSet $crawlprop.PropSet -Category $crawlprop.CategoryName -IsNameEnum $false -VariantType $crawlprop.VariantType -IsMappedToContents $false

Now search again in crawled properties. you will find your "TestName" column.