SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

You can add a new search scope via PowerShell:
New-SPEnterpriseSearchQueryScope -SearchApplication "Search Service Application" -Name NewSearchScope -Description "New Search Test Scope" -DisplayInAdminUI 1–OwningSiteUrl http://servername/sites/

But does anyone know of sample code that shows how to do this via OM? (using the Microsoft.SharePoint.Administration namespace rather than Microsoft.Office.Server.Search)?

Any pointers will be great.

***Update**** ignoring error handling / logging etc, is this a valid way of doing this:

SPSite siteColl = (SPSite)properties.Feature.Parent;

Microsoft.SharePoint.Administration.SPServiceApplication spserviceapp = siteColl.SearchServiceInstance.Service.Applications["Search Service Application"];

SearchServiceApplication searchserviceapp = (SearchServiceApplication)spserviceapp;

ScopeInfo scopeInfo = new ScopeInfo(); 
scopeInfo.Name = "Scope test1";
scopeInfo.Description = "Scope test1";
scopeInfo.DisplayInAdminUI = true;
scopeInfo.CompilationType = ScopeCompilationType.AlwaysCompile;
scopeInfo.CompilationState = ScopeCompilationState.NeedsCompile;            
int statusCode = 0;
int scopeId = searchserviceapp.AddScope(scopeInfo, out statusCode);
if (scopeId > 0)
    RuleInfo ruleInfo;
    ruleInfo = new RuleInfo(); 
    ruleInfo.FilterBehavior = ScopeRuleFilterBehavior.Include;
    ruleInfo.RuleType = ScopeRuleType.AllContent;
    int ruleId = searchserviceapp.AddRule(ruleInfo, scopeId);

Rather than the ssp way of searchcontext?

SearchContext searchctx = null;    
searchctx = SearchContext.GetContext(web.Site);    
Scopes scopes = new Scopes(searchctx);    
Scope currentScope = scopes.AllScopes.Create(
        "CurrentScopeName", string.Empty, new Uri(web.Site.Url), true,
        string.Format("{0}/Search/Pages/Results.aspx", web.Site.Url),
        UrlScopeRuleType.Domain, web.Site.Url);    
ScopeDisplayGroup group = scopes.GetDisplayGroup(new Uri(web.Url),
        "Search Dropdown");

share|improve this question
up vote 1 down vote accepted

You need to use the ScopeCollection.Create method in the Microsoft.Office.Server.Search.Administration namespace. The Microsoft.SharePoint.Administration namespace is related to SharePoint Foundation 2010, which does not include SharePoint Enterprise search.

share|improve this answer

you can include scopeInfo.consumer = "shared" so that scope is shared.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.