Magnus Baneryd
Oct 20, 2009
  6913
(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
Copy Optimizely SaaS CMS Settings to ENV Format Via Bookmarklet

Do you work with multiple Optimizely SaaS CMS instances? Use a bookmarklet to automatically copy them to your clipboard, ready to paste into your e...

Daniel Isaacs | Dec 22, 2024 | Syndicated blog

Increase timeout for long running SQL queries using SQL addon

Learn how to increase the timeout for long running SQL queries using the SQL addon.

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog

Overriding the help text for the Name property in Optimizely CMS

I recently received a question about how to override the Help text for the built-in Name property in Optimizely CMS, so I decided to document my...

Tomas Hensrud Gulla | Dec 20, 2024 | Syndicated blog

Resize Images on the Fly with Optimizely DXP's New CDN Feature

With the latest release, you can now resize images on demand using the Content Delivery Network (CDN). This means no more storing multiple versions...

Satata Satez | Dec 19, 2024