Search scopes in SharePoint Server 2007 are used to narrow the search results returned to users executing a search query. Search scopes can be shared or locally defined. You can use different rules in the definition of a search scope, from simply scoping based on a content source to more complex scoping with conditions using custom metadata. You can view search scopes in the browser with scope pickers. Scope pickers are connected to a display group listing the scopes to be displayed.
Administrators can create search scopes in the browser but Office SharePoint Server 2007 exposes a new search administration object model. The code samples in this article show the steps to create a shared search scope and deliver it as part of the search experience on a collaboration portal.
You must execute the sample code on a server running Microsoft Office SharePoint Server 2007. Also, the code requires references to the following files:
The following namespaces are used:
Connecting to Shared Services Provider and the Search Context
It is important to understand how to add references to the Shared Services Provider and the Search Service. This understanding is essential before beginning work with search scopes. The following code sample uses the name "SharedServices1" as an example Shared Services Provider.
private ServerContext serverctx = null;
private SearchContext searchctx = null;
private void Form1_Load(object sender, EventArgs e)
serverctx = ServerContext.GetContext("SharedServices1");
searchctx = SearchContext.GetContext(serverctx);
Listing Existing Search Scopes
List shared search scopes by instantiating an object of the Scopes class using a reference to the SearchContext as argument in the constructor. Next, the GetSharedScopes class is used in a loop over all of the scopes.
Scopes scopes = new Scopes(searchctx);
foreach (Scope scope in scopes.GetSharedScopes())
TreeNode node = treeViewScopes.Nodes.Add(scope.Name);
Retrieving Information about the Rules
Rules determine the search scope, and you can retrieve all the rules with their details. The following code example shows the three different types of rules available in the object model.
foreach (TreeNode node in treeViewScopes.Nodes)
foreach (ScopeRule rule in scope.Rules)
if (rule is PropertyQueryScopeRule)
PropertyQueryScopeRule prule = (PropertyQueryScopeRule)rule;
TreeNode childnode = node.Nodes.Add("Property Query Rule: ");
childnode.Text += prule.Property.Name + " = " + prule.Value;
if (rule is AllContentScopeRule)
AllContentScopeRule arule = (AllContentScopeRule)rule;
node.Nodes.Add("All Content Rule");
if (rule is UrlScopeRule)
UrlScopeRule urule = (UrlScopeRule)rule;
TreeNode childnode = node.Nodes.Add("URL Rule: ");
childnode.Text += urule.MatchingString;
Creating a Shared Search Scope
Create a shared search scope by adding a new Scope instance to the ScopeCollection. The ScopeCollection is exposed as the AllScopes property at the level of the Scopes instance. After you create the shared search scope, use the CreateUrlRule method to associate one or more rules to the scope.
Scope newScope = scopes.AllScopes.Create
(textBoxName.Text, string.Empty, null, true,
Compiling Shared Search Scopes
You need to compile search scopes before you can use them. This involves flagging items that match the scope rules in the physical index file. The compilation starts at the level of the Scopes instance.
Listing Display Groups
Search scopes are visualized in scope pickers on SharePoint sites. A scope picker displays the scopes listed in one of the display groups. The following code retrieves all display groups for a specific site collection:
foreach (ScopeDisplayGroup group in scopes.AllDisplayGroups)
if (group.OwningSite.Url == textBoxURL.Text)
Associating a Shared Search Scope with a Display Group
You must add a search scope to the display group for the scope picker to show it.
ScopeDisplayGroup group = scopes.GetDisplayGroup
Search scopes narrow the search results for users. You can define them at two levels within a SharePoint Server 2007 environment:
You can create shared search scopes at the level of the Shared Services Provider. You can use them in all site collections within your server farm that are hosted on a Microsoft Internet Information Services (IIS) Web Application bound to that Shared Services Provider.
You can also create local search scopes at the level of the individual site collection. These scopes cannot be re-used in other site collections.
A search scope is defined by one or more rules. Microsoft Office SharePoint Server 2007 introduces new types of rules to guide the search scopes exposed by different types in the object model:
The AllContentScopeRule class represents a scope rule that includes all items in the index.
You can use the PropertyQueryScopeRule class to create a scope rule that matches content items based on a property to value comparison
The UrlScopeRule class represents a scope rule that matches content items based on the display URL of the item.
You must compile search scopes before using them. To compile search scopes, you flag each item in the index that matches the rules as they are defined for the search scope.
After updating your scopes, you can view them by including them in a display group that is connected to one or more of the scope pickers in the user interface. A scope picker is a drop-down list that is used by an end-user to select either specific search scopes or contextual search scopes such as the "Search This Site" scope.