Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

regards Mo

*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 then 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();
share|improve this question

2 Answers 2

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

 
discard

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