Monday, October 25, 2010

Create Sharepoint Search Scope Programmatically C#

Scopes can be created through Model ( Programmatically C#)

Step by Step procedure to Create Scope Programatically.

SPSecurity.RunWithElevatedPrivileges(delegate()

{

//Gets the SearchContext object. It is the entry point to Enter the Search Administration.

SearchContext context = SearchContext.GetContext(site);

//Get all the Scopes available in the Context.

Scopes scopes = new Scopes(context);

string siteUrl = site.Url;

Uri siteUri = new Uri(siteUrl);

//Gets the Scope Collection

ScopeCollection sspScopes = scopes.AllScopes;

//Gets the Scope Manager object by using the Context Object

Scopes scopeManager = new Scopes(context);

//Adds Scope Information in Search Drop Down

Microsoft.Office.Server.Search.Administration.Scope newScope = sspScopes.Create(strName, strDescription, siteUri, true, RESULTS_PAGE_URL, ScopeCompilationType.AlwaysCompile);

objCreatedScope.objScope = newScope;

//Creates a Rule for the Scope. We are creating URL Rule.

newScope.Rules.CreateUrlRule(ScopeRuleFilterBehavior.Include, UrlScopeRuleType.Folder, strScopeURL);

//Gets the Display Groups

ScopeDisplayGroupCollection scopeDisplayGroups = scopes.AllDisplayGroups;

//Gets the Search Drop down Display Group

ScopeDisplayGroup scopeDisplayGroup = scopes.GetDisplayGroup(siteUri, SEARCH_DROPDOWN_GROUP_NAME);

scopeDisplayGroup.Add(newScope);

scopeDisplayGroup.Update();

//Adds Scope Information in Advanced Group

ScopeDisplayGroup scopeDisplayGroup1 = scopes.GetDisplayGroup(siteUri, ADVANCED_SEARCH);

scopeDisplayGroup1.Add(newScope);

scopeDisplayGroup1.Update();

//Complies the Scope once Created

scopes.Update();

scopes.StartCompilation();

scopes.Update();

});

No comments: