Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Search Result Transformation Question View modes: 
User avatar
Member
Member
vcarter - 1/3/2014 1:23:23 PM
   
Search Result Transformation Question
I have a couple of custom document types that use custom queries to display the detail information. They are grouped under parent documents that use a URL redirect to remove the parent from the NodeAliasPath

Example:

/Media/NewsRoom/News/This-Is-My-Article.aspx

becomes:

/Media/NewsRoom/This-Is-My-Article.aspx

When these document types are retreived in the search results they obviously have the full alias path. I am working on the transformation for my results page and would like to add some text replacement so the links follow the correct format. What I am looking for is a best practice way of doing so. My current intent is this:

<script runat="server"> 
protected override void OnDataBinding(EventArgs e)
{
/* Conditional Replacement Code Here */
}
</script>

<div style="margin-bottom: 30px;">
<div style="float: left; border: solid 1px #eeeeee; width: 90px; height:90px; margin-right: 5px;">
<img src="<%# GetSearchImageUrl("/CMSModules/CMS_SmartSearch/no_image.gif",90) %>" alt="" />
</div>
<div style="margin-left: 108px;">
<div>
<a style="font-weight: bold" href='<%# SearchResultUrl(true) %>'>
<%# SearchHighlight(CMS.GlobalHelper.HTMLHelper.HTMLEncode(CMS.ExtendedControls.ControlsHelper.RemoveDynamicControls(DataHelper.GetNotEmpty(Eval("Title"), "/"))), "<span style='font-weight:bold;'>", "</span>") %>
</a>
</div>
<div style="margin-top: 5px; min-height:40px">
<%# SearchHighlight(CMS.GlobalHelper.HTMLHelper.HTMLEncode(TextHelper.LimitLength(HttpUtility.HtmlDecode(CMS.GlobalHelper.HTMLHelper.StripTags(CMS.ExtendedControls.ControlsHelper.RemoveDynamicControls(GetSearchedContent(DataHelper.GetNotEmpty(Eval("Content"), ""))), false, " ")), 280, "...")), "<span style='background-color: #FEFF8F'>", "</span>") %><br />
</div>
<div style="margin-top: 5px;">
<div title="Relevance: <%# Convert.ToInt32(ValidationHelper.GetDouble(Eval("Score"),0.0)*100) %>%"
style="width: 50px; border: solid 1px #aaaaaa; margin-top: 7px; margin-right: 6px;
float: left; color: #0000ff; font-size: 2pt; line-height: 4px; height: 4px;">
<div style="<%# "background-color:#a7d3a7;width:"+ Convert.ToString(Convert.ToInt32((ValidationHelper.GetDouble(Eval("Score"),0.0)/2)*100)) + "px;height:4px;line-height: 4px;" %>">
</div>
</div>
<span style="color: #008000">
<%# TextHelper.BreakLine(SearchHighlight(SearchResultUrl(true),"<strong>","</strong>"),100,"<br />") %>
</span>
<span style="padding-left:5px;;color: #888888; font-size: 9pt">
(<%# GetDateTimeString(ValidationHelper.GetDateTime(Eval("Created"), DateTimeHelper.ZERO_TIME), true) %>)
</span>
</div>
</div>
<div style="clear: both">
</div>
</div>


In the script segment I am thinking I will use a case statement to set the URL value for a hyperlink control which would replace the normal link and <%# SearchResultUrl(true) %>. Does this seem like a good approach?

Is OnDataBinding the correct method to use? Any help would be appreciated.

User avatar
Kentico Legend
Kentico Legend
Accepted solutionAccepted solution
Brenden Kehren - 1/3/2014 2:52:53 PM
   
RE:Search Result Transformation Question
You're on the right path. I've done this with a transformation before for search results. Simply create a function in which you want to perform the work in and return the URL. Since you are in the Transformation code, it will be executed every time an item is databound.

Here is an example of a few simple methods I have in a transformation:
<script runat="server">
protected string GetStoreNumber(string Title)
{
try
{
return ValidationHelper.GetString(Title.Substring(Title.IndexOf("#") + 1, 3), "");
}
catch
{
return "";
}
}

protected string GetSearchType(object id)
{
string ReturnValue = "generic";
if(id.ToString().Contains("custom.nutritionoldway"))
{
ReturnValue = "nutrition";
}
if(id.ToString().Contains("custom.location"))
{
ReturnValue = "location";
}
return ReturnValue;
}
</script>
And now utilizing them in the same transformation:
<asp:Hyperlink runat="server" id="lnkGeneral"   visible='<%# GetSearchType(Convert.ToString(Eval("id"))) == "generic" && Convert.ToString(Eval("Title")).ToUpper() != "CTA" %>'   NavigateUrl='<%# SearchResultUrl(true) %>' >
<%#SearchHighlight(CMS.GlobalHelper.HTMLHelper.HTMLEncode(CMS.ExtendedControls.ControlsHelper.RemoveDynamicControls(DataHelper.GetNotEmpty(Eval("Title"), "/"))), "<span style='font-weight:bold;'>", "</span>")%>
</asp:Hyperlink>
<asp:Hyperlink runat="server" id="lnkLocation" visible='<%# GetSearchType(Convert.ToString(Eval("id"))) == "location" %>' NavigateUrl='<%# "/Locations/Details/" + GetStoreNumber(Convert.ToString(Eval("Title"))) %>' >
Location Result: <%#SearchHighlight(CMS.GlobalHelper.HTMLHelper.HTMLEncode(CMS.ExtendedControls.ControlsHelper.RemoveDynamicControls(DataHelper.GetNotEmpty(Eval("Title"), "/"))), "<span style='font-weight:bold;'>", "</span>")%>
</asp:Hyperlink>
<asp:Hyperlink runat="server" id="lnkNutrition" visible='<%# GetSearchType(Convert.ToString(Eval("id"))) == "nutrition" %>' NavigateUrl='<%# "/Food/Nutrition-and-Ingredients-Products/" + Convert.ToString(Eval("Image")).Replace(" ", "-") + "/" + Convert.ToString(Eval("Title")) %>' >
Nutrition and Ingredients Result: <%# CMS.GlobalHelper.HTMLHelper.HTMLEncode(Convert.ToString(Eval("Content"))) %>
</asp:Hyperlink>

User avatar
Member
Member
vcarter - 1/3/2014 3:13:31 PM
   
RE:Search Result Transformation Question
Thanks Froggeye as always you are a big help.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 1/4/2014 6:16:24 PM
   
RE:Search Result Transformation Question
Glad to help vcarter! I know it wasn't your first solution but it is pretty easy to implement. Good luck

Brenden