Using ItemTemplate in CMSSearchResults

Alexander Pinin asked on May 25, 2017 11:36

Hi! I'm trying to define my own ItemTemplate in the CMSSearchResults control. But it looks like nothing happens. Results are displayed as with default template. What may goes wrong? I have the code like this:

  <cms:CMSSearchResults ID="CMSSearchResults1" runat="server" CMSSearchDialogID="CMSSearchDialog1">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
      <div>
        <a class="Trrrr" href="href"><%# Eval("Title") %></a><br>
      </div>
    </ItemTemplate>
    <FooterTemplate></FooterTemplate> 
  </cms:CMSSearchResults>

Correct Answer

Anton Grekhovodov answered on May 25, 2017 17:16

I've never used custom template for this control and I tried to do it and got the same error. It looks like the control isn't defined properly. But there is a hack which can help you, example:

<cms:CMSSearchResults ID="srchResults" runat="server" IngoneTransformations="true">
    <ItemTemplate>
       <a href="<%# DataBinder.Eval((Container as RepeaterItem).DataItem, "NodeID") %>">NodeID</a>
    </ItemTemplate>
</cms:CMSSearchResults>

But the markup is too complicated and I think it will be better if you create a custom transformation and assign its name to TransformationName property (IgnoreTransformation property should be disabled)

2 votesVote for this answer Unmark Correct answer

Recent Answers


Prashant Verma answered on May 25, 2017 12:45 (last edited on May 25, 2017 12:46)

Hi Alexander,

You cannot modify it directly by placing item template between control tag's its a user control which inheriting from CMSModules_SmartSearch_Controls_SearchResults , if you want to modify template layout then simply clone this user control SearchResults and modify.

Thanks

3 votesVote for this answer Mark as a Correct answer

Alexander Pinin answered on May 25, 2017 13:01

Hi Prashant! Thank you. So there is no way to change appearance of CMSSearchResults without cloning?

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 25, 2017 13:21

Yes Alexander cloning is better way to customize.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 25, 2017 13:59 (last edited on May 25, 2017 13:59)

Hi Alexander!
There is a transformation called cms.root.searchresults, you can modify it or if you set property IgnoreTransformations to true, your template will work. Read more about CMSSearchResults control in documentation

2 votesVote for this answer Mark as a Correct answer

Alexander Pinin answered on May 25, 2017 15:23

Hi Anton!
I tried to set IgnoreTransformations to true, but got this error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 25, 2017 17:47

The error, I'm guessing, is because you're using Eval() when you should be using GetSearchValue() instead.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 25, 2017 18:13

Unfortunately, GetSearchValue won't return any results, because it uses current SearchContext which is not populated inside CMSSearchResult control.

4 votesVote for this answer Mark as a Correct answer

Alexander Pinin answered on May 26, 2017 07:07

Anton thank you! Your hack example works fine!
Brenden thank you! GetSearchValue() takes an id and a column name. Could you tell what kind of id it should be?

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 26, 2017 11:00

This is ID of the item which is fetched from Database or Smart Search Index. So, to pass it into the method, you should get it from query, but it doesn't make sense here, because to get the value of it, you should use Eval method:)

0 votesVote for this answer Mark as a Correct answer

Alexander Pinin answered on May 26, 2017 11:15 (last edited on May 26, 2017 11:15)

Ok, so I think the hack is only way to customize it without copying.

0 votesVote for this answer Mark as a Correct answer

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