How to: Create a Scope Display Group using the API

Posted Friday, February 29, 2008 9:59 AM by CoreyRoth

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



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.


# re: How to: Create a Scope Display Group using the API

Friday, March 5, 2010 4:13 AM by Munna

Nice one, one do i update all mysites scopes

Leave a Comment