This is the second part of the article series, regarding explicitly creating crawl properties and later map in to managed properties.
- Step 1 : Create Crawl Properties using PowerShell
- Step 2 : Create Managed Properties and map to Crawl Properties using a feature
In this article I will show how to create managed properties and map to explicitly created crawl properties using a custom feature.
1. Create classes for managed property and crawl property collections
- public class CrawlPropertyInfo
- {
- public string Category { get; set; }
- public string Name { get; set; }
- public ManagedDataType Type { get; set; }
- }
- public class ManagedPropertyInfo
- {
- public string ManagedPropertyName { get; set; }
- public ManagedDataType Type { get; set; }
- public string Description { get; set; }
- public List<CrawlPropertyInfo> CrawledProperties { get; set; }
- }
2. Create method to map crawl properties to managed properties
- public static void SetManagedPropMappings(List<ManagedPropertyInfo> managedPropertyList)
- {
- try
- {
- SPServiceContext serviceContext =
- SPServiceContext.GetContext
- (SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);
- var searchProxy = serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy))
- as SearchServiceApplicationProxy;
- SearchServiceApplicationInfo info = searchProxy.GetSearchServiceApplicationInfo();
- SearchServiceApplication application = SearchService.Service.SearchApplications.
- GetValue<SearchServiceApplication>(info.SearchServiceApplicationId);
- SearchObjectOwner searchOwner = new SearchObjectOwner(SearchObjectLevel.Ssa);
- Schema schema = new Schema(application);
- ManagedPropertyCollection properties = schema.AllManagedProperties;
- foreach (ManagedPropertyInfo managedPropertyInfo in managedPropertyList)
- {
- var prop = from pr in schema.AllManagedProperties
- where pr.Name == managedPropertyInfo.ManagedPropertyName
- select pr;
- ManagedProperty currentProperty = null;
- //create if managed property is null
- if (null == prop || prop.Count() == 0)
- {
- currentProperty = properties.Create(managedPropertyInfo.ManagedPropertyName, managedPropertyInfo.Type);
- }
- foreach (CrawlPropertyInfo crawledProperty in managedPropertyInfo.CrawledProperties)
- {
- List<CrawledPropertyInfo> crawledProperties = application.GetAllCrawledProperties(crawledProperty.Name, crawledProperty.Category, 0, searchOwner);
- if (prop != null || prop.Count() > 0)
- {
- currentProperty = prop.First();
- CrawledPropertyInfo ci = crawledProperties[0];
- MappingCollection mpc = currentProperty.GetMappings();
- int i;
- for (i = mpc.Count - 1; i >= 0; i--)
- {
- mpc.RemoveAt(i);
- }
- currentProperty.Update();
- Mapping map = new Mapping();
- map.CrawledPropertyName = ci.Name;
- map.CrawledPropset = ci.Propset;
- map.ManagedPid = currentProperty.PID;
- mpc.Add(map);
- currentProperty.SetMappings(mpc);
- currentProperty.Update();
- }
- }
- }
- }
- catch (Exception ex)
- {
- //handle error
- }
- }
3. From the feature activated in feature receiver, call above method with managed properties
- public override void FeatureActivated(SPFeatureReceiverProperties properties)
- {
- SPSite site = properties.Feature.Parent as SPSite;
- if (null != site)
- {
- List<ManagedPropertyInfo> managedPropertyList = new List<ManagedPropertyInfo>();
- ManagedPropertyInfo testProperty = new ManagedPropertyInfo
- {
- ManagedPropertyName = "TestManagedProperty",
- Type = ManagedDataType.Text,
- CrawledProperties = new List<CrawlPropertyInfo> {
- new CrawlPropertyInfo{
- Name = "ows_TestCrawlProperty",
- Category = "SharePoint",
- Type = ManagedDataType.Text
- }
- }
- };
- managedPropertyList.Add(testProperty);
- SetManagedPropMappings(managedPropertyList);
- }
- }
4. After activating the feature you can see a managed property is created and mapped to a crawl property
1 comment:
hi Dinusha,
How you make the Call of FeatureActivated functions, I not quite understand what kind of app you made Example, thks
Post a Comment