Magnus Baneryd
Oct 20, 2009
  1437
(0 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
Performance optimization – the hardcore series – part 2

Earlier we started a new series about performance optimization, here Performance optimization – the hardcore series – part 1 – Quan Mai’s blog...

Quan Mai | Oct 4, 2023 | Syndicated blog

Our first steps into local AI

After a consumer of tools like ChatGPT and CoPilot, we as developers like to dive deeper into it. How does it work? Where to start? Can I create my...

Chuhukon | Oct 4, 2023 | Syndicated blog

Update on .NET 8 support

With .NET 8 now in release candidate stage I want to share an update about our current thinking about .NET 8 support in Optimizely CMS and Customiz...

Magnus Rahl | Oct 3, 2023

Adding Block Specific JavaScript and CSS to the body and head of the page

A common requirement for CMS implementations includes the ability to embed third party content into a website as if it formed part of the website...

Mark Stott | Oct 3, 2023

Performance optimization – the hardcore series – part 1

Hi again every body. New day – new thing to write about. today we will talk about memory allocation, and effect it has on your website performance....

Quan Mai | Oct 3, 2023 | Syndicated blog

Next level content delivery with Optimizely Graph

Optimizely introduced a new product called Optimizely Graph earlier this year. We were one of the first partners to adopt this new service in a...

Ynze | Oct 2, 2023 | Syndicated blog