6

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);
}
searchserviceapp.Update(true); 

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),
        ScopeCompilationType.AlwaysCompile);    
currentScope.Update();    
currentScope.Rules.CreateUrlRule(ScopeRuleFilterBehavior.Include,
        UrlScopeRuleType.Domain, web.Site.Url);    
ScopeDisplayGroup group = scopes.GetDisplayGroup(new Uri(web.Url),
        "Search Dropdown");
group.Add(currentScope);
group.Update();    
scopes.StartCompilation();

scopes.Update();

2 Answers 2

1

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.

0

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

Your Answer

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