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

Search SharePoint 2010以编程方式添加新的搜索范围,search,sharepoint-2010,Search,Sharepoint 2010,您可以通过powershell添加新的搜索范围: 新建搜索企业搜索范围-搜索应用程序“搜索服务应用程序”-名称新闻搜索范围-说明“新建搜索测试范围”-DisplayInAdminUI 1–OwningSiteUrl 但有人知道如何通过OM实现这一点的示例代码吗?(使用Microsoft.SharePoint.Administration命名空间而不是Microsoft.Office.Server.Search) 任何指点都很好 问候 钼 *更新** 忽略错误处理/日志记录等,这样做是否有效: S

您可以通过powershell添加新的搜索范围: 新建搜索企业搜索范围-搜索应用程序“搜索服务应用程序”-名称新闻搜索范围-说明“新建搜索测试范围”-DisplayInAdminUI 1–OwningSiteUrl

但有人知道如何通过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); 

而不是搜索上下文的ssp方式

SearchContext searchctx=null
searchctx=SearchContext.GetContext(web.Site)
Scopes Scopes=新范围(searchctx)
Scope currentScope=scopes.AllScopes.Create( “CurrentScopeName”,string.Empty,新Uri(web.Site.Url),true, string.Format(“{0}/Search/Pages/Results.aspx”,web.Site.Url), ScopeCompileType.AlwaysCompile)
currentScope.Update()
currentScope.Rules.CreateUrlRule(ScopeRuleFilterBehavior.Include, UrlScopeRuleType.Domain、web.Site.Url)
ScopeDisplayGroup=scopes.GetDisplayGroup(新Uri(web.Url), “搜索下拉列表”);添加组(当前范围); group.Update()
scopes.startComplation()


SearchContext
在SharePoint 2010 Server中被标记为过时,因此不建议使用此方法

另外,请参见

scopes.Update();