/// <summary> /// Ensure a search result source /// </summary> /// <param name="ssa">The search service application.</param> /// <param name="resultSourceName">The result source name</param> /// <param name="level">The search object level.</param> /// <param name="searchProvider">The search provider for the result source.</param> /// <param name="contextWeb">The SPWeb to retrieve the search context.</param> /// <param name="query">The search query in KQL format.</param> /// <param name="properties">Query properties.</param> /// <param name="overwrite">if set to <c>true</c> [overwrite].</param> /// <param name="isDefaultResultSourceForOwner">Whether this result source will be flagged as the default for the current search owner</param> /// <returns> /// The result source. /// </returns> private static Source InnerEnsureResultSource(SearchServiceApplication ssa, string resultSourceName, SearchObjectLevel level, string searchProvider, SPWeb contextWeb, string query, QueryTransformProperties properties, bool overwrite, bool isDefaultResultSourceForOwner) { var federationManager = new FederationManager(ssa); var searchOwner = new SearchObjectOwner(level, contextWeb); var resultSource = federationManager.GetSourceByName(resultSourceName, searchOwner); if (resultSource != null && overwrite) { federationManager.RemoveSource(resultSource); } if (resultSource == null || overwrite) { resultSource = federationManager.CreateSource(searchOwner); resultSource.Name = resultSourceName; resultSource.ProviderId = federationManager.ListProviders()[searchProvider].Id; resultSource.CreateQueryTransform(properties, query); resultSource.Commit(); if (isDefaultResultSourceForOwner) { federationManager.UpdateDefaultSource(resultSource.Id, searchOwner); } } return resultSource; }
/// <summary> /// Ensure a result source /// </summary> /// <param name="contextSite">The context SPSite object</param> /// <param name="resultSourceInfo">The result source configuration object</param> /// <returns>The name of the result source</returns> public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo) { Source resultSource = null; var updateMode = resultSourceInfo.UpdateMode; var sortCollection = new SortCollection(); if (resultSourceInfo.SortSettings != null) { foreach (var sortSetting in resultSourceInfo.SortSettings) { sortCollection.Add(sortSetting.Key, sortSetting.Value); } } var queryProperties = new QueryTransformProperties(); queryProperties["SortList"] = sortCollection; // If the SortCollection contains "Rank" as one of its keys, specifiy the ranking model to be used. If a ranking model is // specified but sorting by Rank is not in the sort setting, throw an exception. if (resultSourceInfo.RankingModelId != Guid.Empty) { if ((resultSourceInfo.SortSettings != null && !resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name)) || resultSourceInfo.SortSettings == null) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "You can't specify a ranking model id ({0}) if you are not sorting by rank. Make sure to include Rank as the first Sorting Key in the sort settings if you want to use a ranking model.", resultSourceInfo.RankingModelId)); } queryProperties["RankingModelId"] = resultSourceInfo.RankingModelId.ToString(); } else if (resultSourceInfo.SortSettings != null && resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name)) { queryProperties["RankingModelId"] = BuiltInRankingModels.DefaultSearchModelId.ToString(); } // Get the search service application for the current site var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite); if (searchServiceApplication != null) { if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource)) { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, true, resultSourceInfo.IsDefaultResultSourceForOwner); } else { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, false, resultSourceInfo.IsDefaultResultSourceForOwner); var searchQuery = string.Empty; if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery)) { searchQuery = resultSourceInfo.Query; } else if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery)) { if (resultSource.QueryTransform != null) { // Check if appended query is already found on the current result source query template // Note: remain case sensitive because the revert query option is also case sensitive. if (!resultSource.QueryTransform.QueryTemplate.Contains(resultSourceInfo.Query)) { searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query; } } else { searchQuery = resultSourceInfo.Query; } } else if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery)) { if (resultSource.QueryTransform != null) { searchQuery = resultSource.QueryTransform.QueryTemplate.Replace(resultSourceInfo.Query, string.Empty).Trim(); } } resultSource.CreateQueryTransform(queryProperties, searchQuery); resultSource.Commit(); } } return resultSource; }
/// <summary> /// Ensure a search result source /// </summary> /// <param name="ssa">The search service application.</param> /// <param name="resultSourceName">The result source name</param> /// <param name="level">The search object level.</param> /// <param name="searchProvider">The search provider for the result source.</param> /// <param name="contextWeb">The SPWeb to retrieve the search context.</param> /// <param name="query">The search query in KQL format.</param> /// <param name="properties">Query properties.</param> /// <param name="overwrite">if set to <c>true</c> [overwrite].</param> /// <param name="isDefaultResultSourceForOwner">Whether this result source will be flagged as the default for the current search owner</param> /// <returns> /// The result source. /// </returns> private static Source InnerEnsureResultSource(SearchServiceApplication ssa, string resultSourceName, SearchObjectLevel level, string searchProvider, SPWeb contextWeb, string query, QueryTransformProperties properties, bool overwrite, bool isDefaultResultSourceForOwner) { var federationManager = new FederationManager(ssa); var searchOwner = new SearchObjectOwner(level, contextWeb); var resultSource = federationManager.GetSourceByName(resultSourceName, searchOwner); if (resultSource != null && overwrite) { federationManager.RemoveSource(resultSource); } if (resultSource == null || overwrite) { resultSource = federationManager.CreateSource(searchOwner); resultSource.Name = resultSourceName; resultSource.ProviderId = federationManager.ListProviders()[searchProvider].Id; resultSource.CreateQueryTransform(properties, query); resultSource.Commit(); if (isDefaultResultSourceForOwner) { federationManager.UpdateDefaultSource(resultSource.Id, searchOwner); } } return(resultSource); }
/// <summary> /// Ensure a result source /// </summary> /// <param name="contextSite">The context SPSite object</param> /// <param name="resultSourceInfo">The result source configuration object</param> /// <returns>The name of the result source</returns> public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo) { Source resultSource = null; var updateMode = resultSourceInfo.UpdateMode; var sortCollection = new SortCollection(); if (resultSourceInfo.SortSettings != null) { foreach (var sortSetting in resultSourceInfo.SortSettings) { sortCollection.Add(sortSetting.Key, sortSetting.Value); } } var queryProperties = new QueryTransformProperties(); queryProperties["SortList"] = sortCollection; // If the SortCollection contains "Rank" as one of its keys, specifiy the ranking model to be used. If a ranking model is // specified but sorting by Rank is not in the sort setting, throw an exception. if (resultSourceInfo.RankingModelId != Guid.Empty) { if ((resultSourceInfo.SortSettings != null && !resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name)) || resultSourceInfo.SortSettings == null) { throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "You can't specify a ranking model id ({0}) if you are not sorting by rank. Make sure to include Rank as the first Sorting Key in the sort settings if you want to use a ranking model.", resultSourceInfo.RankingModelId)); } queryProperties["RankingModelId"] = resultSourceInfo.RankingModelId.ToString(); } else if (resultSourceInfo.SortSettings != null && resultSourceInfo.SortSettings.ContainsKey(BuiltInManagedProperties.Rank.Name)) { queryProperties["RankingModelId"] = BuiltInRankingModels.DefaultSearchModelId.ToString(); } // Get the search service application for the current site var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite); if (searchServiceApplication != null) { if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource)) { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, true, resultSourceInfo.IsDefaultResultSourceForOwner); } else { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, false, resultSourceInfo.IsDefaultResultSourceForOwner); var searchQuery = string.Empty; if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery)) { searchQuery = resultSourceInfo.Query; } else if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery)) { if (resultSource.QueryTransform != null) { // Check if appended query is already found on the current result source query template // Note: remain case sensitive because the revert query option is also case sensitive. if (!resultSource.QueryTransform.QueryTemplate.Contains(resultSourceInfo.Query)) { searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query; } } else { searchQuery = resultSourceInfo.Query; } } else if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery)) { if (resultSource.QueryTransform != null) { searchQuery = resultSource.QueryTransform.QueryTemplate.Replace(resultSourceInfo.Query, string.Empty).Trim(); } } resultSource.CreateQueryTransform(queryProperties, searchQuery); resultSource.Commit(); } } return(resultSource); }
/// <summary> /// Ensure a result source /// </summary> /// <param name="contextSite">The context SPSite object</param> /// <param name="resultSourceInfo">The result source configuration object</param> /// <returns>The name of the result source</returns> public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo) { Source resultSource = null; var updateMode = resultSourceInfo.UpdateMode; var sortCollection = new SortCollection(); if (resultSourceInfo.SortSettings != null) { foreach (KeyValuePair <string, SortDirection> sortSetting in resultSourceInfo.SortSettings) { sortCollection.Add(sortSetting.Key, sortSetting.Value); } } var queryProperties = new QueryTransformProperties(); queryProperties["SortList"] = sortCollection; // Get the search service application for the current site var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite); if (searchServiceApplication != null) { if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource)) { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, true, resultSourceInfo.IsDefaultResultSourceForOwner); } else { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, false, resultSourceInfo.IsDefaultResultSourceForOwner); string searchQuery = string.Empty; if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery)) { searchQuery = resultSourceInfo.Query; } if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery)) { if (resultSource.QueryTransform != null) { var rgx = new Regex(resultSourceInfo.Query); if (!rgx.IsMatch(resultSource.QueryTransform.QueryTemplate)) { searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query; } } else { searchQuery = resultSourceInfo.Query; } } if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery)) { if (resultSource.QueryTransform != null) { var rgx = new Regex(resultSourceInfo.Query); searchQuery = rgx.Replace(resultSource.QueryTransform.QueryTemplate, string.Empty); } } resultSource.CreateQueryTransform(queryProperties, searchQuery); resultSource.Commit(); } } return(resultSource); }
/// <summary> /// Ensure a result source /// </summary> /// <param name="contextSite">The context SPSite object</param> /// <param name="resultSourceInfo">The result source configuration object</param> /// <returns>The name of the result source</returns> public Source EnsureResultSource(SPSite contextSite, ResultSourceInfo resultSourceInfo) { Source resultSource = null; var updateMode = resultSourceInfo.UpdateMode; var sortCollection = new SortCollection(); if (resultSourceInfo.SortSettings != null) { foreach (KeyValuePair<string, SortDirection> sortSetting in resultSourceInfo.SortSettings) { sortCollection.Add(sortSetting.Key, sortSetting.Value); } } var queryProperties = new QueryTransformProperties(); queryProperties["SortList"] = sortCollection; // Get the search service application for the current site var searchServiceApplication = this.GetDefaultSearchServiceApplication(contextSite); if (searchServiceApplication != null) { if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteResultSource)) { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, true, resultSourceInfo.IsDefaultResultSourceForOwner); } else { resultSource = InnerEnsureResultSource( searchServiceApplication, resultSourceInfo.Name, resultSourceInfo.Level, resultSourceInfo.SearchProvider, contextSite.RootWeb, resultSourceInfo.Query, queryProperties, false, resultSourceInfo.IsDefaultResultSourceForOwner); string searchQuery = string.Empty; if (updateMode.Equals(ResultSourceUpdateBehavior.OverwriteQuery)) { searchQuery = resultSourceInfo.Query; } if (updateMode.Equals(ResultSourceUpdateBehavior.AppendToQuery)) { if (resultSource.QueryTransform != null) { var rgx = new Regex(resultSourceInfo.Query); if (!rgx.IsMatch(resultSource.QueryTransform.QueryTemplate)) { searchQuery = resultSource.QueryTransform.QueryTemplate + " " + resultSourceInfo.Query; } } else { searchQuery = resultSourceInfo.Query; } } if (updateMode.Equals(ResultSourceUpdateBehavior.RevertQuery)) { if (resultSource.QueryTransform != null) { var rgx = new Regex(resultSourceInfo.Query); searchQuery = rgx.Replace(resultSource.QueryTransform.QueryTemplate, string.Empty); } } resultSource.CreateQueryTransform(queryProperties, searchQuery); resultSource.Commit(); } } return resultSource; }