ISearchable: Easily make your DotNetNuke® module searchable (in VB and C#)

The ISearchable interface is used to allow the users of your module to search for content using the search mechanism provided by the DotNetNuke framework.

What ISearchable Will Do For You

Most users are familiar with the search provided by websites such as Google.com

If you type in "create a DotNetNuke module" in Google you will see a list of websites that describe how to make DotNetNuke modules. If you type in "buy a DotNetNuke module" you will see sites that sell DotNetNuke modules. Google is able to understand (to the best of it's ability) what you are ultimately trying to find by searching through the entire contents of all the pages it has stored in it's database.

The search provided by the DotNetNuke framework doesn't work like that. It works more like an index in the back of a book. The only items in the index are items that the author (in this case the module developer) has decided to put there. The search merely allows DotNetNuke portal users to quickly find items placed in this index.

The DotNetNuke Search is Very Useful

At first glance you might prefer the Google approach and not see the usefulness of the search provided by the DotNetNuke framework. However, like a carefully constructed index in a book, the DotNetNuke search allows the module developer to deliver helpful relevant results to search queries by only placing items in that index that a user would want to search on. In this case what you leave out is as important as what you put in.

For the Survey module it was decided that only the questions not the answers would be placed in the search index. This omitted such potentially repetitive and unhelpful words such as yes and no from cluttering up the search results.

Implementing Search for the Survey Module

To implement search for the Survey module we performed three steps:

Implement ISearchable in the Controller Class

When you look at the module definition for the Survey module, you can see that the controller class defined is DotNetNuke.Modules.Survey.SurveyController.

We opened up this class in the Visual Studio code editor and added this line at the top of the class and hit the Enter key.

VB:

Implements Entities.Modules.ISearchable

C#:

: Entities.Modules.ISearchable

Hitting the Enter key after we insert the line causes Visual Studio to insert a stub for the method.

(the green wavy line under End Function is there because currently the method does not return a value. This will be fixed in the next step.)

Next, we add the following code to the method and save the page.

VB:

Public Function GetSearchItems(ByVal ModInfo As Entities.Modules.ModuleInfo) _
As Services.Search.SearchItemInfoCollection Implements Entities.Modules.ISearchable.GetSearchItems
' Get the Surveys for this Module instance
Dim colSurveys As List(Of SurveyInfo) = GetSurveys(ModInfo.ModuleID)
Dim SearchItemCollection As New SearchItemInfoCollection
Dim SurveyInfo As SurveyInfo
For Each SurveyInfo In colSurveys
Dim SearchItem As SearchItemInfo
SearchItem =
New SearchItemInfo _
(ModInfo.ModuleTitle &
" - " & SurveyInfo.Question, _
SurveyInfo.Question, _
SurveyInfo.CreatedByUser, _
SurveyInfo.CreatedDate, ModInfo.ModuleID, _
SurveyInfo.SurveyId, _
SurveyInfo.Question)
SearchItemCollection.Add(SearchItem)
Next
Return SearchItemCollection
End Function

C#:

public SearchItemInfoCollection GetSearchItems(DotNetNuke.Entities.Modules.ModuleInfo ModInfo)
{
// Get the Surveys for this Module instance
List<SurveyInfo> colSurveys = GetSurveys(ModInfo.ModuleID);
SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();
foreach (SurveyInfo SurveyInfo in colSurveys)
{
SearchItemInfo SearchItem;
SearchItem =
new SearchItemInfo
(ModInfo.ModuleTitle + " - " + SurveyInfo.Question,
SurveyInfo.Question,
SurveyInfo.CreatedByUser,
SurveyInfo.CreatedDate, ModInfo.ModuleID,
Convert.ToString(SurveyInfo.SurveyId),
SurveyInfo.Question);
SearchItemCollection.Add(SearchItem);
}
return SearchItemCollection;
}

We return to the module definition (for the Survey module) and click the Update link.

Searchable will now be checked under Supported Features.

The content will now be searchable.

What Did We Just Do?

To implement the search we performed three steps:

If you look in the object browser at the definition for the SearchItemInfo object you can see that it has a number of overloads.

The constructor we used has this signature:

Public Sub New(
ByVal Title As
String,
ByVal Description As String,
ByVal Author As Integer,
ByVal PubDate As
Date,
ByVal ModuleID As Integer,
ByVal SearchKey As
String,
ByVal Content As
String
)
Member of:
DotNetNuke.Services.Search.SearchItemInfo

The important thing to remember is that the SearchKey parameter must be a unique value. In this case we passed the contents of the SurveyId field (from the Surveys table) to the SearchKey parameter.

The Content is the content that the portal users will be searching on. We passed the contents of the Question field to the Content parameter. As you can see in the table schema below, the Question field has a direct one-to-one relationship with the SurveyID field.

This line adds the SearchItemInfo object to the SearchItemInfoCollection collection:

SearchItemCollection.Add(SearchItem)

This line returns the SearchItemInfoCollection collection as the output of the method:

Return SearchItemCollection

DotNetNuke is a Framework To Make Your Job Easier

You could easily create your own search function. However, it would certainly require more code. In addition the Search functionality provided by the DotNetNuke framework searches across all modules (that implement the interface) at the same time.

This demonstrates yet another reason why you should consider leveraging the DotNetNuke framework for your next project to save time and money.

[Back to: The ADefWebserver DotNetNuke HELP WebSite]


DotNetNuke® is a registered trademark of Perpetual Motion Interactive Systems Inc.