« Back to All Posts

January 23

Sitefinity: Index and Search Events

Posted Under:

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 EventsIndexProvider files

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!

  • Facebook
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Furl it!

Comments  5

  • SelArom 23 Jan

    sorry about the code formatting. hopefully this will be resolved when I migrate to the new site. stay tuned!
  • Georgi Chokov's Blog 10 Feb

    Pingback from: http://blogs.sitefinity.com/GeorgiChokov/Posts/09-02-10/eventsindexprovider_-_include_your_events_in_the_search_results.aspx
  • Software Blog 22 Apr

    Pingback from: http://www.selarom.net/blog/2009-04-22/changes_to_custom_indexproviders_implementation_in_sitefinity_3_6_sp1.aspx
  • DD 12 May

    Thanks for the helpful post!!
  • Stacy Draper 08 Jun

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0535: 'EventIndex.EventIndexerInfo' does not implement interface member 'Telerik.Framework.Search.IIndexerInfo.ResolveIndexPath()'

    Source Error:

    Line 9:      /// Summary description for EventIndexerInfo
    Line 10:     /// </summary>
    Line 11: public class EventIndexerInfo : IIndexerInfoLine 12:     {
    Line 13:         private string _url;

    Source File: c:\....\App_Code\EventIndexerInfo.cs    Line: 11

    This is happens because of a minor change in 3.6 - there is a ping back to the post that describes how to fix it:
    http://www.selarom.net/blog/2009-04-22/changes_to_custom_indexproviders_implementation_in_sitefinity_3_6_sp1.aspx
    it didn't jump out at me so I thought I'd post it here again, so someone else doesn't have to go to google to find it like I just did.
Post a comment!
  1. Formatting options