Showing posts with label Sharepoint. Show all posts
Showing posts with label Sharepoint. Show all posts

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

});

Create a Sharepoint Search Scope by Out of the Box

Scopes can be Created by 2 ways in Sharepoint.

1. By Out of the Box

2. Through Programmatically (C#)

I. By Out of the Box.

The step by step to create scope through Out of the box is as follows.

a. In the Site Actions -> Site Settings -> Modify All Site Settings.

b. Under site collection Administration -> Select Search Scopes

c. Select New Scope

d. Enter the Details and Press OK.