Showing posts with label Search Scopes. Show all posts
Showing posts with label Search Scopes. Show all posts

Monday, July 22, 2013

Get all seach scopes for current site collection


In this article we will be seeing about search scopes in SharePoint 2010.|

In this article:
  • Add namespaces
  • Get search context
  • Get all scopes
  • Skip default scopes
  • Add scopes in dropdown
Get all the search scopes:

Add following namespaces.

using Microsoft.Office.Server;
using  Microsoft.Office.Server.Search.Administration;

Use following method to populate all scopes of current site collection in dropdown.

private void GetScopes()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
// Get the search context object.
SearchContext context = SearchContext.GetContext(site);
Scopes scopes = new scopes(context);
foreach(Scope scope in scopes.GetSharedScopes())
{
// Skip following default scopes
// Global Query Exclusion - Everything that should be omitted from all search by default
// Rank Demoted Sites - Sites whose rank will be demoted in click - distance calculation.

if(scope.Name != "Global Query Exclusion" && scope.Name !="Rank demoted Sites")
// Show scopes in dropdown
ddlscopes.Items.Add(scope.Name);
}
}
});
}