开发者

SharePoint 2010 add a new search scope programmatically

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 =开发者_如何学Go 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();


SearchContext is marked as obsolete in SharePoint 2010 Server, so it is not the recommended way of doing it.

Also, see this answer on SharePoint Overflow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜