SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Vijai Anand Articles | SharePoint March 04, 2011
In this article we will be seeing how to create, modify and delete scope display group using c# in SharePoint 2010.
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 

In SharePoint 2010 the SearchContext API is obsolete. You should update your code to work with the new API's. I have created an example of how to do this at http://www.sharepointconfig.com/2011/04/creating-sharepoint-2010-search-scopes-programmatically/

Posted by Ari Bakker Apr 08, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor