Search Scopes in SharePoint 2010


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

In this article:

  • Get all the search scopes
  • Modify a search scope
  • Delete a scope
  • Get all the rule types of a particular scope
  • Create a custom scope
  • Create All content rule

Get all the search scopes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Administration;

namespace SearchScopes
{
    class Program
    {
        static void Main(string[] args)
        {
            string ssa = "Search Service Application";|
            SearchContext searchContext = SearchContext.GetContext(ssa);
            Scopes scopes = new Scopes(searchContext);
            foreach (Scope scope in scopes.GetSharedScopes())
            {
                Console.WriteLine(scope.Name);
            }
            Console.ReadLine();
        }
    }
}

ShareScope1.gif

Modify a search scope:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Administration;

namespace SearchScopes
{
    class Program
    {
        static void Main(string[] args)
        {
            string ssa = "Search Service Application";
            SearchContext searchContext = SearchContext.GetContext(ssa);
            Scopes scopes = new Scopes(searchContext);
            Scope scope = scopes.GetSharedScope("B");
            scope.Name = "BNew";
            scope.Description = "Modified scope";
           scopes.Update();
        }
    }
}

ShareScope2.gif

Delete a scope:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Administration;

namespace SearchScopes
{
    class Program
    {
        static void Main(string[] args)
        {
            string ssa = "Search Service Application";
            SearchContext searchContext = SearchContext.GetContext(ssa);
            Scopes scopes = new Scopes(searchContext);|
            Scope scope = scopes.GetSharedScope("A");
            scope.Delete();
        }
    }
}

Get all the rule types of a particular scope:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Administration;

namespace SearchScopes
{
    class Program
    {
        static void Main(string[] args)
        {
            string ssa = "Search Service Application";
            SearchContext searchContext = SearchContext.GetContext(ssa);
            Scopes scopes = new Scopes(searchContext);
            Scope scope = scopes.GetSharedScope("BNew");
            foreach (ScopeRule rule in scope.Rules)
            {
                Console.WriteLine(rule.RuleType.ToString());               
            }
            Console.ReadLine();
        }
    }
}

ShareScope3.gif

Create a custom scope:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Administration;

namespace SearchScopes
{
    class Program
    {
        static void Main(string[] args)
        {
            string ssa = "Search Service Application";
            SearchContext searchContext = SearchContext.GetContext(ssa);
            Scopes scopes = new Scopes(searchContext);         
            Scope newScope = scopes.AllScopes.Create("CustomScope", "Custom Scope", null, true, null, ScopeCompilationType.AlwaysCompile)            scopes.Update();
        }
    }
}

ShareScope4.gif

Create All content rule:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Administration;

namespace SearchScopes
{
    class Program
    {
        static void Main(string[] args)
        {
            string ssa = "Search Service Application";
            SearchContext searchContext = SearchContext.GetContext(ssa);
            Scopes scopes = new Scopes(searchContext);
            Scope scope = scopes.GetSharedScope("BNew");
            scope.Rules.CreateAllContentRule();
            scope.Update();
        }
    }
}

ShareScope5.gif