Try our conversational search powered by Generative AI!

Magnus Baneryd
Oct 20, 2009
  1577
(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 and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024