Sitefinity: Index and Search Events

By in
No comments

NOTE: This post has been superceded by the Sitefinity Toolkit whcih contains a search provider for news, events, and generic content items. This post remains for reference only.

Sitefinity has a very useful search-indexing feature that works pretty much out-of-the-box to index pages, blogs and news. Unfortunately, there doesn’t seem to be any built-in provider for indexing events. Since I’ve changed the 404 page to include search results, I want to also include upcoming events in those results, just in case a date has changed or something.

This option may be available after the next release, but as of version 3.5, I couldn’t find any information in the developer manual on how to create a custom index provider… Fortunately, I found an excellent resource in the sitefinity forums that contained a CustomIndex Provider sample from Nikola on the Telerik team. In this very brief post I’ll show you how easily this can be modified to index events (or anything, really).

First, download the example from the sitefinity forum post here. Extract it to a subfolder of your App_Code folder, which I called EventsIndexProvider. You should get 4 class files: CustomIndexerInfo.cs, CustomIndexProvider.cs, SettingsControl.cs and ViewControl.cs.

The first thing I did was rename the first two to EventIndexerInfo.cs and EventIndexProvider.cs as well as renaming the classes within to match. In addition, I changed the namespace inside each file from CustomIndex to EventIndex. While this is certainly not a necessary step, it will help to keep you organized should you decide to add more index providers in the future.

From here it’s pretty straightforward: change the Name and Description properties in the EventIndexProvider class to match your index provider behavior.

/// <summary>/// Defines the name of the provider. This name is used to mange providers within Indexing Service./// </summary>public string Name
{
    get {
        return "EventsIndexProvider";
    }
}

/// <summary>/// Provides detailed description of the client/// </summary>public string Description
{
    get {
        return "Provides indexing for events.";
    }
}

/// <summary>/// Meta fields for this provider/// </summary>protected string[] MetaFields
{
    get {
        return new string[]{
            "Title" };
    }
}

Also, for some reason, the EventIndexerInfo class did not implement the IIndexerInfo interface, so be sure to include those as well.

#region IIndexerInfo Members

public string Culture
{
    get { return string.Empty; }
}

public Guid ItemID
{
    get { return Guid.Empty; }
}

#endregion

Now all that is left is to modify the GetContentToIndex method to index the event items. This is a simple matter of using the EventsManager to retrieve the events and index each one. Be sure to modify the url parameter to match the structure of your events page.

// get current eventsEventsManager mgr = new EventsManager("Events");
IMetaSearchInfo[] filters = new IMetaSearchInfo[2];
filters[0] = new MetaSearchInfo(MetaValueTypes.DateTime, "Expiration_Date", DateTime.Now, SearchCondition.GreaterOrEqual);
filters[1] = new MetaSearchInfo(MetaValueTypes.DateTime, "Publication_Date", DateTime.Now, SearchCondition.LessOrEqual);
IList events = mgr.Content.GetContent("Event_Start ASC", filters);

foreach (IContent ev in events)
{
    Hashtable metaFields = new Hashtable();            
    foreach (string key in MetaFields)
    {
        metaFields.Add(key, "");
    }

    metaFields["Title"] = ev.GetMetaData("Title");

    list.Add(
        new EventIndexerInfo(
        string.Format("/events/details{0}.aspx", ev.Url),
        metaFields,
        ev.Content.ToString())
        );
}

return list.ToArray();

Easy or what? That’s all there is to it! All we have to do now is include the index provider in web.config:

<add name="EventIndex" type="EventIndex.EventIndexProvider, App_Code" settingsControl="EventIndex.SettingsControl" viewSettingsControl="EventIndex.ViewControl" description="Provides indexing for events." />

Finally simply navigate to the Administration page of the Sitefinity admin and add include this new index in the search service and reindex! Events will now be included in search results, and show up on the 404 error page as well. For an example take a look at this invalid page on the McAllen Public Library website: http://www.mcallenlibrary.net/art_walk.aspx and notice that the artwalk event is listed in the results (at the top in fact!).

I hope that this was helpful to someone out there! I’ve included a link to a zip of the completed files available from mybloop below. As always your comments are welcome and appreciated.

Download no longer available. This project has been merged into the Sitefinity Toolkit.

On a somewhat related note, I am in the process of making a new website into which I will migrate all of my software and development content. The website selarom.com will continue to be my personal website, with all my music, videos, and long-winded rants. But now that I’m dedicating more time to developing, I think a more professional site is warranted. Stay tuned and please pardon the inevitable issues and broken links during the transition. Thanks for reading!

The following two tabs change content below.

selaromdotnet

Senior Developer at iD Tech
Josh loves all things Microsoft and Windows, and develops solutions for Web, Desktop and Mobile using the .NET Framework, Azure, UWP and everything else in the Microsoft Stack. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom.