November Happy Hour will be moved to Thursday December 5th.

Magnus Baneryd
Oct 20, 2009
  1791
(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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog