Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > How to customizing search results based on document type View modes: 
User avatar
Member
Member
jhoppe - 4/7/2011 9:52:24 AM
   
How to customizing search results based on document type
Hi,

I'm trying to customize the search results images. I don't have an image field in the database for my document types. Instead, I want to detect the search result document type in the transformation and display an image based on the document type. In example, all CMS.News will have one image. Can I access the document type from the search transformation? Or Can I get the nodeID and then lookup the nodeID in the database?

Also, does anyone know what fields are returned as part of the search?

Thanks!
Joe Hoppe

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 4/11/2011 10:47:38 AM
   
RE:How to customizing search results based on document type
Hi,

You will need to modify the behavior of the search results control (and be aware of it in case of future upgrade) - \CMSModules\SmartSearch\Controls\SearchResults.ascx.cs

In this file, in method "Search()", the results retrieved by search are assigned to the "results" data set. You will need to access the dataset and assign for given documents appropriate image according to their class name (type). Then you will pass the dataset to the transformation where you will handle the image display (e.g. using GetSearchValue).

Best regards,
Juraj Ondrus

User avatar
Member
Member
jhoppe - 4/18/2011 11:13:22 AM
   
RE:How to customizing search results based on document type
Worked great. Thanks!

User avatar
Member
Member
phil-fourleaf.co - 5/11/2011 11:42:42 AM
   
RE:How to customizing search results based on document type
Hey guys,

This is exactly what I am looking for, any chance of posting a little source for how this was implemented.

Many thanks.

User avatar
Member
Member
joeh42 - 5/11/2011 11:59:09 AM
   
RE:How to customizing search results based on document type
Sure here is some sample code... I add two new columns to the search result dataset, and then use eval to display them in the transformation.

                // Search
DataSet results = SearchHelper.Search(searchText, SearchHelper.GetSort(srt), path, DocumentTypes, culture, defaultCulture, CombineWithDefaultCulture, CheckPermissions, SearchInAttachments, Indexes, displayResults, startPosition, numberOfProceeded, (UserInfo)CMSContext.CurrentUser, out numberOfResults, AttachmentsWhere, AttachmentsOrderBy);

if (results != null)
{
string documentType = "documentType";
results.Tables[0].Columns.Add(documentType);

foreach (DataRow dr in results.Tables[0].Rows)
{
if (dr["type"].ToString() == "cms.document")
{
string idField = dr["id"].ToString().Split(';')[1];
idField = idField.Split('_')[0];
int id = Convert.ToInt32(idField);

var tree = new CMS.TreeEngine.TreeProvider();
var node = tree.SelectSingleNode(id);

switch (node.NodeClassName)
{
case "custom.Article":
dr["image"] = "~/icon_articles.png";
dr[documentType] = "Article";
break;
case "custom.News":
dr["image"] = "~/icon_news.png";
dr[documentType] = "News/event";
break;
case "custom.Resource":
dr["image"] = "~/icon_resources.png";
dr[documentType] = "Resource";
break;
}
}
else if (dr["type"].ToString() == "forums.forum"){
dr[documentType] = "Discussion";
}
else if (dr["type"].ToString() == "cms.user")
{
dr[documentType] = "Member profile";
}
}
}

User avatar
Member
Member
phil-fourleaf.co - 5/12/2011 4:07:05 AM
   
RE:How to customizing search results based on document type
Brilliant! Thanks Joe.