Magnus Baneryd
Oct 20, 2009
  6868
(3 votes)

Implementing a Search provider for SiteCenter in 5 minutes.

Ever wondered how to add your own search result to the new search box in SiteCenter? If not I’ll give you a quick introduction anyway.

This is how you do it, first read the “Introduction to Gadgets” and follow the project setup instructions.

When that is done open the QuickChat project inside Visual Studio and add a new class called UsersSearchProvider.

Open the newly created file and implement the EPiServer.Shell.Search.Contracts.ISearchProvider on the class.

public class UsersSearchProvider : ISearchProvider
{
    #region ISearchProvider Members
 
    /// <summary>
    /// Area that the provider mapps to, used for spotlight searching
    /// </summary>
    public string Area
    {
        get { throw new NotImplementedException(); }
    }
 
    /// <summary>
    /// The category that the provider returns hits in
    /// </summary>
    public string Category
    {
        get { throw new NotImplementedException(); }
    }
 
    /// <summary>
    /// Executes a Search on the provider
    /// </summary>
    /// <param name="query">The query to execute</param>
    /// <returns>A list of search results</returns>
    public IEnumerable<SearchResult> Search(Query query)
    {
        throw new NotImplementedException();
    }
 
    #endregion
}

Now to the fun part, the actual implementation.

if you look at the code above you will see two properties, Area and Category.

The Area property is used for “spot-light” searching, this property maps directly to the different Module Areas that has been configured. E.g if you would like your search result to be prioritized when you are in Edit mode the Area should be CMS which is the module area name for the EPiServer CMS.

public string Area
{
    get { return "CMS"; }
}

The Category property is used when the search results are displayed. In our case we want the the results to be categorized as Users because we are going to search for users.

public string Category
{
    get { return "Users"; }
}

To keep this sample as simple as possible I will just use the regular Membership.FindUsersByName and FindUsersByEmail to find all users inside EPiServer and then redirect the link result to Admin/EditUser.aspx, won’t be that nice but it does the trick.

public IEnumerable<SearchResult> Search(Query query)
{
    List<SearchResult> results = new List<SearchResult>();
 
    //Get the wildcardsymbol for the providers
    string wildcardSymbol = "%";
    ProviderCapabilitySettings settings = null;
    if (Membership.Provider != null && ProviderCapabilities.Providers.TryGetValue(Membership.Provider.GetType(), out settings))
    {
        wildcardSymbol = settings.WildcardSymbol;
    }
 
    string searchQuery = query.SearchQuery + wildcardSymbol;
 
    //Search for users using userName
    int totalRecords = 0;
    MembershipUserCollection memershipHits = Membership.FindUsersByName(searchQuery, 0, 10, out totalRecords);
 
    foreach (MembershipUser user in memershipHits)
    {
        //Do not exceed maximum number of results
        if (results.Count < query.MaxResults)
        {
            AddUser(user, results);
        }
    }
 
    if (results.Count < query.MaxResults)
    {
        //Search bo emailadress
        memershipHits = Membership.FindUsersByEmail(searchQuery, 0, 10, out totalRecords);
        foreach (MembershipUser user in memershipHits)
        {
            //Do not exceed maximum number of results
            if (results.Count < query.MaxResults)
            {
                AddUser(user, results);
            }
        }
    }
 
    return results;
}
 
private static void AddUser(MembershipUser user, IList<SearchResult> results)
{
    string url = EPiServer.UriSupport.ResolveUrlFromUIBySettings("Admin/EditUser.aspx?membershipUsername=" + user.UserName + "&Provider=" + user.ProviderName);
 
    bool exist = results.FirstOrDefault(r => r.Url == url) != null;
 
    if (!exist)
    {
        results.Add(new SearchResult(url, user.UserName));
    }
 
}

Thats nice, but how do I get into the provider inside SiteCenter?

This is how you do it:

[Export(typeof(ISearchProvider))]
public class UsersSearchProvider : ISearchProvider
{...}

You will need to add a reference to the MEF assembly (System.ComponentModel.Composition), It’s located in the PublicTemplates bin directory.

Look easy right? That’s why we use MEF to export dependencies from the modules inside SiteCenter.

This is what happens behind the hood:

Our search controller imports all classes that have been exported with the ISearchProvider contract and then filters these depending on the ISearchProvider.Area when performing a search.

UsersSearchProvider.cs

Oct 20, 2009

Comments

Please login to comment.
Latest blogs
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024