/// <summary> /// Ensure a Result Type in a site collection /// </summary> /// <param name="site">The site collection</param> /// <param name="resultType">The result type info object</param> /// <returns>The result type item</returns> public ResultItemType EnsureResultType(SPSite site, ResultTypeInfo resultType) { var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, site.RootWeb); var resultSource = this.GetResultSourceByName(site, resultType.ResultSource.Name, resultType.ResultSource.Level); var resultTypeManager = new ResultItemTypeManager(this.GetDefaultSearchServiceApplication(site)); var existingResultTypes = resultTypeManager.GetResultItemTypes(searchOwner, true); // Get the existing result type var resType = existingResultTypes.FirstOrDefault(r => r.Name.Equals(resultType.Name)); if (resType == null) { resType = new ResultItemType(searchOwner); resType.Name = resultType.Name; resType.SourceID = resultSource.Id; resType.DisplayTemplateUrl = resultType.DisplayTemplate.ItemTemplateTokenizedPath; var properties = resultType.DisplayProperties.Select(t => t.Name).ToArray(); resType.DisplayProperties = string.Join(",", properties); resType.RulePriority = resultType.Priority; // Create rules var rules = resultType.Rules.Select( this.CreateCustomPropertyRule) .ToList(); resType.Rules = new PropertyRuleCollection(rules); typeof(ResultItemType).GetProperty("OptimizeForFrequentUse") .SetValue(resType, resultType.OptimizeForFrequenUse); // Add the result type resultTypeManager.AddResultItemType(resType); } return(resType); }
/// <summary> /// Ensure search Result Type /// </summary> /// <param name="type"></param> /// <returns></returns> public ResultItemType EnsureResultType(ResultTypeDTO type) { var resultTypesManager = new ResultItemTypeManager(_searchProxy); var resultType = resultTypesManager.GetResultItemTypes(_searchOwner, true).FirstOrDefault(rt => rt.Name.Equals(type.Name, StringComparison.OrdinalIgnoreCase)); if (resultType != null) { return(UpdateResultType(resultType.ID, type)); } resultType = new ResultItemType(_searchOwner) { Name = type.Name, DisplayTemplateUrl = type.DisplayTemplateUrl, RulePriority = type.Priority }; if (!string.IsNullOrEmpty(type.ResultSource)) { var source = GetResultsSource(type.ResultSource); resultType.SourceID = source.Id; } var rules = new PropertyRuleCollection(); foreach (var builtInRule in type.BuiltInRules) { rules.Add(GetBuiltInPropertyRule(builtInRule)); } foreach (var rule in type.CustomRules) { rules.Add(CreatePropertyRule(rule.ManagedPropertyName, rule.Operator, rule.Values)); } resultType.Rules = rules; return(resultTypesManager.AddResultItemType(resultType)); }
/// <summary> /// Ensure a Result Type in a site collection /// </summary> /// <param name="site">The site collection</param> /// <param name="resultType">The result type info object</param> /// <returns>The result type item</returns> public ResultItemType EnsureResultType(SPSite site, ResultTypeInfo resultType) { var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, site.RootWeb); var resultSource = this.GetResultSourceByName(site, resultType.ResultSource.Name, resultType.ResultSource.Level); var resultTypeManager = new ResultItemTypeManager(this.GetDefaultSearchServiceApplication(site)); var existingResultTypes = resultTypeManager.GetResultItemTypes(searchOwner, true); // Get the existing result type var resType = existingResultTypes.FirstOrDefault(r => r.Name.Equals(resultType.Name)); if (resType == null) { resType = new ResultItemType(searchOwner); resType.Name = resultType.Name; resType.SourceID = resultSource.Id; resType.DisplayTemplateUrl = resultType.DisplayTemplate.ItemTemplateTokenizedPath; var properties = resultType.DisplayProperties.Select(t => t.Name).ToArray(); resType.DisplayProperties = string.Join(",", properties); resType.RulePriority = resultType.Priority; // Create rules var rules = resultType.Rules.Select( this.CreateCustomPropertyRule) .ToList(); resType.Rules = new PropertyRuleCollection(rules); typeof(ResultItemType).GetProperty("OptimizeForFrequentUse") .SetValue(resType, resultType.OptimizeForFrequenUse); // Add the result type resultTypeManager.AddResultItemType(resType); } return resType; }