Search This Blog

Monday, February 23, 2015

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

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.



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



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

No comments:

Post a Comment