0

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/>";
        }
    }
});

1 Answer 1

0

This way it works. Result scopes are not really scopes. There must be a better way than casting with Federation Manager, but anyway it works.

public string showAllScopes() {      
    string retStr = "<b>Demo reuslts</b><br/>";      
    SPSite site = new SPSite(SPContext.Current.Site.Url);

    // 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);
    // get the current scopes defined in the search server
    Scopes scopes = new Scopes(searchServiceApplication);
    RemoteScopes remScopes = new RemoteScopes(spService);   

    // get 2013 result scopes
    SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
    SearchServiceApplicationProxy farmProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Search Service Application");
    FederationManager instance = new FederationManager(farmProxy);
    SearchObjectFilter filter = new SearchObjectFilter(SearchObjectLevel.SPWeb);
    bool includeInactive = false;
    IEnumerable<Source> returnValue = default(IEnumerable<Source>);
    returnValue = instance.ListSources(filter, includeInactive);    

    // remote scopes            
    retStr += "<br/><br/>Remote Scopes<br/>";
    foreach (Scope scope in remScopes.AllScopes)
    {
        retStr += showScope(scope);
    }

    // shared scopes           
    retStr += "<br/><br/>Shared Scopes<br/>";
    foreach (Scope scope in scopes.GetSharedScopes())
    {
        retStr += showScope(scope);
    }

    // web scopes
    retStr += "<br/><br/>Web Scopes<br/>";
    foreach (Scope scope in scopes.GetScopesForSite(site.PrimaryUri))
    {
        retStr += showScope(scope);
    }

    // result Sources
    retStr += "<br/><br/>Result Scopes<br/>";                         
    foreach (Source scope in returnValue)
    {
        retStr += showScope(scope);
    }

    return retStr;}

and the helper method

private string showScope(dynamic scope) {
    string retStr = "";          
    retStr += "<br/>" + " ScopeName: " + scope.Name +
                    " / description: " + scope.Description;

    return retStr;
}

ok, to be 100%, here are the usings. Maybe some are not really needed

using Microsoft.Office.Server.Search.Administration; using Microsoft.Office.Server.Search.Administration.Query; using Microsoft.Office.Server.Search.Query; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.WebPartPages; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Dynamic; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.