/// <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); } }
public void EnsureGlymaResultTypesSynced() { string displayProperties = GetDisplayTemplateDisplayProperties(GlymaDisplayTemplateUrl); foreach (ResultItemType currentResultType in _resultTypeManager.GetResultItemTypes(_searchOwner, false)) { if (currentResultType.DisplayTemplateUrl.Equals(GlymaDisplayTemplateUrl, StringComparison.OrdinalIgnoreCase)) { // Ensure the managed properties of the result type is always in-sync with the display template referenced by the DisplayTemplateUrl property. if (!currentResultType.DisplayProperties.Equals(displayProperties, StringComparison.OrdinalIgnoreCase)) { currentResultType.DisplayProperties = displayProperties; _resultTypeManager.UpdateResultItemType(currentResultType); } } } }
/// <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> /// 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; }