Reader Level:
Articles

Programmatically create, modify and delete search scope display group in SharePoint 2010

By Vijai Anand on March 04, 2011
In this article we will be seeing how to create, modify and delete scope display group using c# in SharePoint 2010.
  • 0
  • 0
  • 11012

In this article we will be seeing how to create, modify and delete scope display group using c# in SharePoint 2010.

Create scope display group:

In the SharePoint site you can create a new display group for the scope and associate the scope with the group.

Through UI you can go to Site Actions => Site Settings =>Site Administration => Search Scopes => Display group => Create a new display group.

SearScope1.gif

The same thing can be achieved using c# code.

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 SearchScopeDisplayGroup
{
    class Program
    {
        static void Main(string[] args)
        {          
            using (SPSite site = new SPSite("http://servername:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SearchContext searchContext = SearchContext.GetContext(site);
                    Scopes scopes = new Scopes(searchContext);
                    ScopeDisplayGroupCollection displayGroupColl = scopes.AllDisplayGroups;
                    ScopeDisplayGroup displayGroup = displayGroupColl.Create("CustomDisplayGroup", "Custom Display Group", new Uri(site.Url), true);
                    displayGroup.Add(scopes.GetSharedScope("CustomSiteScope"));
                    displayGroup.Add(scopes.GetSharedScope("CustomScope"));                   
                    displayGroup.Default = scopes.GetSharedScope("CustomScope");
                    displayGroup.Update();                   
                }
            }
        }
    }
}


Modify scope display group:

Here we will be modifying the existing display group using C#.

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 SearchScopeDisplayGroup
{
    class Program
    {
        static void Main(string[] args)
        {          
            using (SPSite site = new SPSite("http://servername:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SearchContext searchContext = SearchContext.GetContext(site);
                    Scopes scopes = new Scopes(searchContext);                  
                    ScopeDisplayGroup displayGroup = scopes.GetDisplayGroup(new Uri(site.Url), "CustomDisplayGroup");
                    displayGroup.Name = "CustomDisplayGroupNew";
                    displayGroup.Description = "Custom Display Group New";
                    displayGroup.Remove(scopes.GetSharedScope("CustomScope"));
                    displayGroup.Add(scopes.GetSharedScope("BNew"));
                    displayGroup.Update();
                }
            }
        }
    }
}


SearScope2.gif

Delete scope display group:


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 SearchScopeDisplayGroup
{
    class Program
    {
        static void Main(string[] args)
        {          
            using (SPSite site = new SPSite("http://servername:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SearchContext searchContext = SearchContext.GetContext(site);
                    Scopes scopes = new Scopes(searchContext);
                    ScopeDisplayGroup displayGroup = scopes.GetDisplayGroup(new Uri(site.Url), "CustomDisplayGroup");
                    displayGroup.Delete();                
                }
            }
        }
    }
}

Vijai Anand

Vijai Anand has been working in IT industry for over 5 years. He holds Bachelor's degree in Electronics and Communication Engineering. Vijai write articles and blogs related to SharePoint 2013, SharePoint 2010, Silv... Read more

COMMENT USING

Trending up