private static void EnsureManagedProperties(SPSite site, IEnumerable <ManagedPropertyDefinition> definitions) { SearchServiceApplication searchApplication = GetSearchServiceApplication(site); if (searchApplication != null) { Schema schema = new Schema(searchApplication); IEnumerable <CrawledProperty> allCrawledProperties = schema.AllCategories["SharePoint"].GetAllCrawledProperties().OfType <CrawledProperty>(); foreach (ManagedPropertyDefinition definition in definitions) { int variantType = VariantTypeDictionary[definition.DataType]; CrawledProperty rawProperty = allCrawledProperties.FirstOrDefault(v => v.Name == definition.CrawledPropertyName); if (rawProperty == null || rawProperty.GetMappedManagedProperties().GetEnumerator().MoveNext()) { continue; } ManagedProperty property; try { property = schema.AllManagedProperties[definition.MappedPropertyName]; } catch (KeyNotFoundException) { property = schema.AllManagedProperties.Create(definition.MappedPropertyName, definition.DataType); } MappingCollection mappings = property.GetMappings(); Mapping mapping = new Mapping(); mapping.CrawledPropset = rawProperty.Propset; mapping.CrawledPropertyName = rawProperty.Name; mapping.ManagedPid = property.PID; mappings.Add(mapping); property.SetMappings(mappings); property.Update(); } FlushCache(site); } }