I was teaching a FAST for SharePoint Workshop today and we were doing labs on creating and mapping crawled properties to managed properties with the FAST Administration UI and via PowerShell. But one student wanted to know how to do this in code, hence this blog post.
Instead of having a deployment script creating your properties, you could have an event receiver which creates your mappings once a site or list is provisioned. Or when some other action is going on.
So how do you go about doing this?
Well, first off Crawled and Managed Properties for FAST are a part of the index schema and is accessible via the following two namespaces:
which are the namespaces used for working with the FAST components.
Below is a quick example showing how this can be done in code, with comments along the way to guide the process. It’s all pretty straight forward.
const string propertyName = “productname”;
// Get a reference to the Index Schema via the SchemaContext class
SchemaContext schemaContext = new SchemaContext();
Schema schema = schemaContext.Schema;
// Assign a guid to the custom Crawled Propery Category
Guid myGuid = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
// Create if it does not exist
catGroup = schema.AllCategories.Create(“My Crawled Properties”, myGuid);
catGroup = schema.AllCategories[myGuid];
// Create a new crawled property if it doesn’t exist
CrawledProperty cp = catGroup.GetAllCrawledProperties().Where(
c => c.Name == propertyName && c.VariantType == 31 && c.Propset == myGuid).FirstOrDefault();
if (cp == null) cp = catGroup.CreateCrawledProperty(propertyName, myGuid, 31);
// Create a new managed property if it doesn’t exist
mp = schema.AllManagedProperties.Create(propertyName, “”, ManagedType.Text);
mp = schema.AllManagedProperties[propertyName];
// Create a new crawled to managed property mapping if it doesn’t exit
CrawledPropertyMapping mapping = mp.GetCrawledPropertyMappings();
// Get a reference to the default full text index (first one)
FullTextIndex fti = schema.AllFullTextIndecies.First();
// Get the full text index mapping from the Managed Property
FullTextIndexMappingCollection fullTextIndexMappingCollection = mp.GetFullTextIndexMappings();
FullTextIndexMapping indexMapping =
fullTextIndexMappingCollection.Where(ftpMap => ftpMap.FullTextIndex.Name == fti.Name).FirstOrDefault();
// Set a mapping to importance level 6, if it’s not already set to something else
if (indexMapping == null || !fullTextIndexMappingCollection.Contains(indexMapping))