I have been trying to get to show all search scopes in sharepoint 2013. I can get remote Scopes, shared Scopes, and Local(web/site) Scopes, but I do not see the custom scopes that a user creates. I have been trying with use of SearchQueryAndSiteSettingsServiceProxy but I get an Access Denied error. Does anyone knows how to enumurate all Scopes, including custom scopes? Thanks
SPSite siteLowPrivileges = new SPSite(SPContext.Current.Site.Url);
SPSecurity.RunWithElevatedPrivileges(
delegate() {
using(SPSite site = new SPSite(siteLowPrivileges.ID)) {
// Get the service context for a selected site
SPServiceContext spService = SPServiceContext.GetContext(site);
// Access the service proxy instance of search application proxy.
SearchServiceApplicationProxy searchServiceAppProxy = spService.GetDefaultProxy(typeof(SearchServiceApplicationProxy))as SearchServiceApplicationProxy;
// Service Application Info object to retrieve the application id for the search service.
SearchServiceApplicationInfo searchSerivceAppInfo = searchServiceAppProxy.GetSearchServiceApplicationInfo();
// Retrieve the search application instance for the specified id.
SearchServiceApplication searchServiceApplication = SearchService.Service.SearchApplications.GetValue < SearchServiceApplication > (searchSerivceAppInfo.SearchServiceApplicationId);
// try to get 2013 result scopes
SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue < SearchQueryAndSiteSettingsServiceProxy > ();
SearchServiceApplicationProxy farmProxy = settingsProxy.ApplicationProxies.GetValue < SearchServiceApplicationProxy > ("Search Service Application");
SearchServiceApplicationInfo searchSerivceFarmAppInfo = farmProxy.GetSearchServiceApplicationInfo();
SearchServiceApplication searchServiceFarmApplication = SearchService.Service.SearchApplications.GetValue < SearchServiceApplication > (searchSerivceFarmAppInfo.SearchServiceApplicationId);
// get the current scopes defined in the search server
Scopes scopes = new Scopes(searchServiceApplication);
RemoteScopes remScopes = new RemoteScopes(spService);
Scopes resultScopes = new Scopes(searchServiceFarmApplication);
// show remote scopes
retStr += "<br/><br/>Remote Scopes<br/>";
foreach(Scope scope in remScopes.AllScopes) {
retStr += showScope(scope);
}
// show shared scopes
retStr += "<br/><br/>Shared Scopes<br/>";
foreach(Scope scope in scopes.GetSharedScopes()) {
retStr += showScope(scope);
}
// show web scopes
retStr += "<br/><br/>Web Scopes<br/>";
foreach(Scope scope in scopes.GetScopesForSite(site.PrimaryUri)) {
retStr += showScope(scope);
}
// show result scopes
retStr += "<br/><br/>Result Scopes<br/>";
try {
// ERROR HERE when accessing .AllScopes
foreach(Scope scope in resultScopes.AllScopes) {
retStr += showScope(scope);
}
} catch (Exception ex) {
// Exception of type 'System.UnauthorizedAccessException'
retStr += "<br/><b> error: " + ex.Message + "</b><br/>";
}
}
});