in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, MOSS 2007, ASP.NET, LINQ, and Visual Studio 2012.

How to: Create a Scope Display Group using the API

Yesterday, I talked about an issue with the SearchBox Web Part resulting from not having the Scope Display Group it used defined on my site collection.  I mentioned, the best way to fix this was to have your feature create the scope display group for you, so here is how its done.  To accomplish this, you need to start by getting a SearchContext object to get access to the Scopes object which is used to manipulate anything related to scopes.  Once you have an instance of the Scopes object the AllDisplayGroups property returns a ScopeDisplayGroupCollection.  It is this collection that you can use to create a scope display group.  Unfortunately, however it does not contain anything to find an existing group other than an integer indexer.  This means your choices for determining if the scope already exists are a for loop or a try/catch around the Create method.  I am not sure why the SharePoint API team decided to make it so difficult to determine if an item exists in a collection.  Here is what the code looks like.

// need the collection to modify scope settings

using (SPSite currentSiteCollection = new SPSite("http://mossserver"))

{

    // get a search context and get access to the scope manager

    SearchContext searchContext = SearchContext.GetContext(currentSiteCollection);

    Scopes scopes = new Scopes(searchContext);

 

    // get all scope display groups

    ScopeDisplayGroupCollection scopeDisplayGroups = scopes.AllDisplayGroups;

 

    // create the scope display group and add scopes to it

    ScopeDisplayGroup scopeDisplayGroup = scopeDisplayGroups.Create("My Scope Group", "Scope Group Description",

        new Uri(currentSiteCollection.Url), true);

    scopeDisplayGroup.Add(scopes.GetSharedScope("My Shared Scope 1"));

    scopeDisplayGroup.Add(scopes.GetSharedScope("My Shared Scope 2"));

 

    // update the scope

    scopeDisplayGroup.Update();

}

When you create the new scope, use the GetSharedScope method of the Scopes object to get the shared scopes that you want to add to the display group.  The true parameter on the Create method indicates if you want to show the group in the Admin UI or not.  Once you have made the changes, be sure and call the Update method.

Comments

 

Munna said:

Nice one, one question.how do i update all mysites scopes

March 5, 2010 4:13 AM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an Applications Architect at Infusion specializing in ECM and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems