SharePoint 2010以编程方式添加新的搜索范围

时间:2011-01-17 17:22:50

标签: search sharepoint-2010

您可以通过powershell添加新的搜索范围: New-SPEnterpriseSearchQueryScope -SearchApplication“Search Service Application” - Name NewSearchScope -Description“New Search Test Scope”-DisplayInAdminUI 1-OwningSiteUrl http://servername/sites/

但有没有人知道示例代码如何通过OM执行此操作? (使用Microsoft.SharePoint.Administration命名空间而不是Microsoft.Office.Server.Search)

任何指针都会很棒

问候 沫

*更新* * 忽略错误处理/日志记录等,这是一种有效的方法:

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); 

而不是searchcontext的ssp方式?

SearchContext searchctx = null;
searchctx = SearchContext.GetContext(web.Site);
范围范围=新范围(searchctx);
范围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),         “搜索下拉”); group.Add(currentScope);     group.Update();
scopes.StartCompilation();

scopes.Update();

1 个答案:

答案 0 :(得分:0)

SearchContext在SharePoint 2010 Server中标记为已过时,因此是推荐的方法。

另请参阅this answer on SharePoint Overflow