/// <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); }
public void EnsureResultSource_WhenRevertingAppendedQuery_ShouldRevertToPreviousQuery() { const string ResultSourceName = "Test Result Source"; const string Query = "{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}"; const string AppendedQuery = "{?{|owstaxidmetadataalltagsinfo:{User.SPSResponsibility}}}"; using (var testScope = SiteTestScope.BlankSite()) { // Arrange var resultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = Query, UpdateMode = ResultSourceUpdateBehavior.OverwriteResultSource }; var appendedResultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = AppendedQuery, UpdateMode = ResultSourceUpdateBehavior.AppendToQuery }; var revertedResultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = AppendedQuery, UpdateMode = ResultSourceUpdateBehavior.RevertQuery }; using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope()) { var searchHelper = injectionScope.Resolve <ISearchHelper>(); var ssa = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection); var federationManager = new FederationManager(ssa); var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, testScope.SiteCollection.RootWeb); // Act searchHelper.EnsureResultSource(testScope.SiteCollection, resultSourceInfo); searchHelper.EnsureResultSource(testScope.SiteCollection, appendedResultSourceInfo); searchHelper.EnsureResultSource(testScope.SiteCollection, revertedResultSourceInfo); // Assert var source = federationManager.GetSourceByName(ResultSourceName, searchOwner); Assert.IsNotNull(source); Assert.IsNotNull(source.QueryTransform); Assert.AreEqual(ResultSourceName, source.Name); Assert.AreEqual(Query, source.QueryTransform.QueryTemplate); } } }
protected Source GetCurrentSource(Microsoft.SharePoint.SPSite site, SearchResultDefinition definition, out FederationManager federationManager, out SearchObjectOwner searchOwner) { var context = SPServiceContext.GetContext(site); var searchAppProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy; federationManager = new FederationManager(searchAppProxy); searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, site.RootWeb); return(federationManager.GetSourceByName(definition.Name, searchOwner)); }
protected Source GetCurrentSource(Microsoft.SharePoint.SPSite site, SearchResultDefinition definition, out FederationManager federationManager, out SearchObjectOwner searchOwner) { var context = SPServiceContext.GetContext(site); var searchAppProxy = context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy; federationManager = new FederationManager(searchAppProxy); searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, site.RootWeb); return federationManager.GetSourceByName(definition.Name, searchOwner); }
/// <summary> /// Deletes the result source. /// </summary> /// <param name="contextSite">Current site collection</param> /// <param name="resultSourceName">Name of the result source.</param> /// <param name="level">The level.</param> public void DeleteResultSource(SPSite contextSite, string resultSourceName, SearchObjectLevel level) { var searchApp = this.GetDefaultSearchServiceApplication(contextSite); var federationManager = new FederationManager(searchApp); var searchOwner = new SearchObjectOwner(level, contextSite.RootWeb); var resultSource = federationManager.GetSourceByName(resultSourceName, searchOwner); if (resultSource != null) { federationManager.RemoveSource(resultSource); } }
public void EnsureResultSource_WhenSortingByRank_ShouldApplySpecifiedRankingModel() { const string ResultSourceName = "Test Result Source"; // Arrange using (var testScope = SiteTestScope.BlankSite()) { var resultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = "{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}", SortSettings = new Dictionary <string, SortDirection>() { { BuiltInManagedProperties.Rank.Name, SortDirection.Descending } }, RankingModelId = BuiltInRankingModels.DefaultSearchModelId, UpdateMode = ResultSourceUpdateBehavior.OverwriteResultSource }; using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope()) { var searchHelper = injectionScope.Resolve <ISearchHelper>(); var ssa = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection); var federationManager = new FederationManager(ssa); var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, testScope.SiteCollection.RootWeb); // Act searchHelper.EnsureResultSource(testScope.SiteCollection, resultSourceInfo); // Assert var source = federationManager.GetSourceByName(ResultSourceName, searchOwner); Assert.IsNotNull(source); Assert.AreEqual(ResultSourceName, source.Name); Assert.AreEqual(BuiltInRankingModels.DefaultSearchModelId.ToString(), source.QueryTransform.OverrideProperties["RankingModelId"]); } } }
public void EnsureResultSource_WhenSortingByRank_ShouldApplySpecifiedRankingModel() { const string ResultSourceName = "Test Result Source"; // Arrange using (var testScope = SiteTestScope.BlankSite()) { var resultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = "{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}", SortSettings = new Dictionary<string, SortDirection>() { { BuiltInManagedProperties.Rank.Name, SortDirection.Descending } }, RankingModelId = BuiltInRankingModels.DefaultSearchModelId, UpdateMode = ResultSourceUpdateBehavior.OverwriteResultSource }; using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope()) { var searchHelper = injectionScope.Resolve<ISearchHelper>(); var ssa = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection); var federationManager = new FederationManager(ssa); var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, testScope.SiteCollection.RootWeb); // Act searchHelper.EnsureResultSource(testScope.SiteCollection, resultSourceInfo); // Assert var source = federationManager.GetSourceByName(ResultSourceName, searchOwner); Assert.IsNotNull(source); Assert.AreEqual(ResultSourceName, source.Name); Assert.AreEqual(BuiltInRankingModels.DefaultSearchModelId.ToString(), source.QueryTransform.OverrideProperties["RankingModelId"]); } } }
public void EnsureResultSource_WhenRevertingAppendedQuery_ShouldRevertToPreviousQuery() { const string ResultSourceName = "Test Result Source"; const string Query = "{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}"; const string AppendedQuery = "{?{|owstaxidmetadataalltagsinfo:{User.SPSResponsibility}}}"; using (var testScope = SiteTestScope.BlankSite()) { // Arrange var resultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = Query, UpdateMode = ResultSourceUpdateBehavior.OverwriteResultSource }; var appendedResultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = AppendedQuery, UpdateMode = ResultSourceUpdateBehavior.AppendToQuery }; var revertedResultSourceInfo = new ResultSourceInfo() { Name = ResultSourceName, Level = SearchObjectLevel.SPSite, Query = AppendedQuery, UpdateMode = ResultSourceUpdateBehavior.RevertQuery }; using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope()) { var searchHelper = injectionScope.Resolve<ISearchHelper>(); var ssa = searchHelper.GetDefaultSearchServiceApplication(testScope.SiteCollection); var federationManager = new FederationManager(ssa); var searchOwner = new SearchObjectOwner(SearchObjectLevel.SPSite, testScope.SiteCollection.RootWeb); // Act searchHelper.EnsureResultSource(testScope.SiteCollection, resultSourceInfo); searchHelper.EnsureResultSource(testScope.SiteCollection, appendedResultSourceInfo); searchHelper.EnsureResultSource(testScope.SiteCollection, revertedResultSourceInfo); // Assert var source = federationManager.GetSourceByName(ResultSourceName, searchOwner); Assert.IsNotNull(source); Assert.IsNotNull(source.QueryTransform); Assert.AreEqual(ResultSourceName, source.Name); Assert.AreEqual(Query, source.QueryTransform.QueryTemplate); } } }
/// <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> /// Get search Results Source by name /// </summary> /// <param name="name"></param> /// <returns></returns> public Source GetResultsSource(string name) { FederationManager fedManager = new FederationManager(_searchProxy); return(fedManager.GetSourceByName(name, _searchOwner)); }