Conditional Transformation in search result

Nishant Khandelwal asked on February 15, 2017 12:35


I used smart search box for site search. I build two index one for custom table other for pages. Now I want to write conditional transformation for results. If result came from custom table data then it should go on specific page as per its ID and if result came from pages index then it should go on concern page.

How can I achieve? Please guide..

Recent Answers

Dawid Jachnik answered on February 15, 2017 14:10

Hello, If you want to redirect users to specific page basing on keyword I think it's not achievable in easy way, with some drop dwon which type is he searching could be easier.

If you have one page that shows results using Smart Search Results web part you can achive it by using condition in transformation. You need to check whether is custom table item or page so you can use

<%# if (ValidationHelper.GetInteger(GetSearchValue("ItemID"),-1)>0) { %>
//custom table
<% } else { %>
// page
<% } %>
1 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on February 15, 2017 15:24

Dawid's will work, but there is also another property you can grab to get more information, it's:

<%# ValidationHelper.GetString(GetSearchValue("ClassName"), "") %>

This gets the class name of the search object. I know for pages it returns stuff like cms.menuitem or cms.blogpost, i bet for a custom table it will have it's class name too.

2 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on February 15, 2017 20:14 (last edited on February 16, 2017 05:04)

Hi Nishant,
I'm not sure that Dawid's example will work, because as I know if statement does't work with binding syntax <%# %>. So, if it doesn't work, you can try to use placeholders instead to show some HTML layout by conditions:

<!-- if -->
<asp:PlaceHolder runat="server" 
Visible='<%# ValidationHelper.GetInteger(GetSearchValue("ItemID"),-1)>0 %>'>
<!-- else -->
<asp:PlaceHolder runat="server" 
Visible='<%# ValidationHelper.GetInteger(GetSearchValue("ItemID"),-1)<=0 %>'>
<!-- end -->
2 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.