DotNetNuke

Test Harness for DNN ISearchable

August 28, 2006

author:

Test Harness for DNN ISearchable

Testing an implementation of the DNN ISearchable interface implementation for a module can be time-consuming and slow if you rely on the DNN search engine indexer to run and then either check the database for results or use the Search UI. There is a simpler way.

Copy and paste the below script into DNNSearch.aspx (or grab the attachment at the end of this post), place the file in the root folder of your app and you can test ISearchable for any module instance with ease. The script requires you to provide a TabId and a ModuleId. It then queries the database for the BusinessController defined in the DesktopModules table and instantiates it exactly as the DNN search indexer does. It then calls GetSearchItems() and displays the results.

Unlike the DNN search indexer, DNNSearch does not make any changes to the database. It is useful only for testing if the ISearchable implementation is working correctly and does not provide any insights into any issues that the search index provider you are using may have.

 

DNNSearch.aspx

<%@ Import namespace="DotNetNuke.Entities.Modules" %>
<%@ Import namespace="DotNetNuke.Services.Search" %>
<%@ Import namespace="DotNetNuke.Common" %>
<%@ Page Language="c#" AutoEventWireup="false" %>
<script runat="server">
 
    void Results_Click(object sender, EventArgs e)
    {
        int moduleId = -1;
        try
        {
            moduleId = Convert.ToInt32(ModuleId.Text);
        }
        catch
        {
        }
 
        int tabId = -1;
        try
        {
            tabId = Convert.ToInt32(TabId.Text);
        }
        catch
        {
        }
 
        if ((moduleId > -1) && (tabId > -1))
            GetSearchResults(moduleId, tabId);
        else
            SearchResults.Text = "Both Module ID and Tab ID are required";
    }
 
    void GetSearchResults(int moduleId, int tabId)
    {
        ModuleController moduleController = new ModuleController();
        ModuleInfo moduleInfo = moduleController.GetModule(moduleId, tabId);
        StringBuilder sb = new StringBuilder();
 
        if (moduleInfo == null) 
        {
            SearchResults.Text = "No module found with ModuleID=" + moduleId.ToString() + " and TabID=" + tabId.ToString();
            return;
        }
 
        if (moduleInfo.BusinessControllerClass == "")
            SearchResults.Text = "The BusinessControllerClass in the database is blank.";
        else
        {
            try
            {
                object bizController = DotNetNuke.Framework.Reflection.CreateObject(moduleInfo.BusinessControllerClass, moduleInfo.BusinessControllerClass);
                if (bizController == null)
                    SearchResults.Text = "The Business Controller Class " + moduleInfo.BusinessControllerClass + " could not be instantiated.";
                else
                {    
                    SearchContentModuleInfo contentInfo = new SearchContentModuleInfo();
                    contentInfo.ModControllerType = (ISearchable) bizController;
                    contentInfo.ModInfo = moduleInfo;
                    SearchItemInfoCollection results = contentInfo.ModControllerType.GetSearchItems(contentInfo.ModInfo);
                    if (results != null)
                    {
                        int counter = 0;
                        foreach(SearchItemInfo searchItem in results)
                        {
                    if (moduleInfo.ModuleID == searchItem.ModuleId)
                    {
                        sb.Append("

Title: " + searchItem.Title);

                        sb.Append("
GUID: "
+ searchItem.GUID);
                        sb.Append("
Date: "
+ searchItem.PubDate.ToLongDateString());
                        sb.Append("
Description: "
+ searchItem.Description + "

"
);
                    }
                    counter++;
                        }
                        SearchResults.Text = counter.ToString() + " results found.
"
+ sb.ToString();
                    }
                    else
                        SearchResults.Text = "No search results.";
                }
            }
            catch(Exception e)
            {
                SearchResults.Text = "Error: " + e.Message + "

"
+ e.StackTrace;
            }
        }
    }
 
    override protected void OnInit(EventArgs e)
    {
        Results.Click += new EventHandler(Results_Click);
        base.OnInit(e);
    }
 
script>
 
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>Speerio ISearchable Testtitle>
        <style>
              body, p {font-family: Verdana; font-size: 9pt}
        style>
    HEAD>
    <body>
        <p><font size="4">DNNSearch Script - by <a href="http://www.speerio.net">Speerio, Inc.a>p>
        <p><font color="red" size="4">WARNING: Do not leave this script installed on a production system.font>p>
        <form id="Form1" method="post" runat="server">
            <p>Tab ID: <asp:TextBox ID="TabId" Runat="server">asp:TextBox>p>
            <p>Module ID: <asp:TextBox ID="ModuleId" Runat="server">asp:TextBox>p>
            <asp:Button ID="Results" Runat="server" Text="Get Search Results" />p>
            <p>To test for user-specific results, add code to GetSearchItems() to check for userid=N in querystring.p>
            <p><b>Search Results:b>p>
            <asp:Label ID="SearchResults" Runat="server" />
        form>
    body>
HTML>

DNNSearch.zip (1.48 KB)

Founder NftyDreams; founder Decentology; co-founder DNN Software; educator; Open Source proponent; Microsoft MVP; tech geek; creative thinker; husband; dad. Personal blog: http://www.kalyani.com. Twitter: @techbubble
Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.