/// <summary> /// Update search Result Type with specified data /// </summary> /// <param name="resultTypeId">Existing Result Type Id</param> /// <param name="type">Data transfer object used for updating</param> /// <returns></returns> public ResultItemType UpdateResultType(int resultTypeId, ResultTypeDTO type) { var resultTypesManager = new ResultItemTypeManager(_searchProxy); var resultType = resultTypesManager.GetResultItemTypeByID(resultTypeId, _searchOwner); resultType.Name = type.Name; resultType.DisplayTemplateUrl = type.DisplayTemplateUrl; resultType.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.UpdateResultItemType(resultType)); }
public SearchConfigurationManager(SPSite siteCollection) { _siteCollection = siteCollection; _searchService = GetSearchServiceApplicationProxy(); _searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, _siteCollection.RootWeb); _resultTypeManager = new ResultItemTypeManager(_searchService); }
/// <summary> /// Delete a result type in the site collection /// </summary> /// <param name="site">The site</param> /// <param name="resultType">The result type object</param> public void DeleteResultType(SPSite site, ResultTypeInfo resultType) { ResultItemType resType = null; var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, site.RootWeb); var resultTypeManager = new ResultItemTypeManager(this.GetDefaultSearchServiceApplication(site)); var existingResultTypes = resultTypeManager.GetResultItemTypes(searchOwner, true); // Get the existing result type resType = existingResultTypes.FirstOrDefault(r => r.Name.Equals(resultType.Name)); if (resType != null) { resultTypeManager.DeleteResultItemType(resType); } }
/// <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; }