Gravatar for jschjolberg@awareweb.com

Question by jschjolberg, Jun 17, 2014 9:01 AM

Can you use a field alias inside of an Underscore results template?

I'm outputting some raw field values in my Underscore results template while using the JavaScript Search Framework. This works fine if specify the real name of the index field - like this for example:

<%-raw.somefield33008%>

I believe these fields are appended with a unique id that is specific to the particular index you are using (33008 in this case). I'm wondering if there is a way to use field aliases instead so I don't have to change this id every time I deploy to a site which using a different index with a different id. Is there a way to do this? I've tried without much luck.

1 Reply
Gravatar for vseguin@coveo.com

Answer by Vincent Séguin, Jun 17, 2014 9:40 AM

Like Alex said, the ToCoveoFieldName is the method to use for this.

{{= raw.ToCoveoFieldName("Name") }}

This is important because it will translate the field name according to the current database you're viewing. This is also why we're using this field name convention, because a field can be different from master to web in Sitecore.

Gravatar for jschjolberg@awareweb.com

Comment by jschjolberg, Jun 17, 2014 10:40 AM

Thanks - this is good information. I'm having trouble getting this to work though. Should the format in the Underscore template be:

<%- ToCoveoFieldName("SomeField") %>

or

{{= raw.ToCoveoFieldName("SomeField") }}

I've tried it both ways with subtle variations (single quotes vs. double quotes, equal sign vs. hyphen) with no luck. It looks like I'm getting a syntax related error because the results won't load at all when I try this.

Gravatar for vseguin@coveo.com

Comment by Vincent Séguin, Jun 17, 2014 10:52 AM

The second format is the good one. Actually, it is {{= raw.<%= ToCoveoFieldName("Extension", false) %>}}.

You need the 'false' parameter because the field in the result doesn't have the @ before their name. Also, it depends if you're in a Coveo Search Page or in another page… if you're in another page that is not related to Coveo, make sure to import the Coveo.UI namespace and add SitecoreUtilities before the ToCoveoFieldName, for example :

{{= raw.<%= SitecoreUtilities.ToCoveoFieldName("Extension", false) %>}}

Gravatar for jschjolberg@awareweb.com

Comment by jschjolberg, Jun 17, 2014 1:51 PM

Still having some trouble with this. If I simply try to do this:

{{Coveo.UI.SitecoreUtilities.ToCoveoFieldName(\"MyField\", false)}}

I get an error in my browser console - "Cannot read property 'SitecoreUtilities' of undefined"

If I do this:

{{SitecoreUtilities.ToCoveoFieldName(\"MyField\", false)}}

I get "SitecoreUtilities is not defined"

If I do this:

{{ToCoveoFieldName(\"MyField\", false)}}

I get "ToCoveoFieldName is not defined"

Can you explain again how I would be able to access this method? Is there a way to include the namespace in the template?

Gravatar for vseguin@coveo.com

Comment by Vincent Séguin, Jun 17, 2014 2:00 PM

Did you import the namespace Coveo.UI? You can do it just like the regular ASP.NET way, by including this line at the top of your ascx file :

<%@ Import namespace="Coveo.UI" %>

You can also make your layout inherit from the Coveo Search, by replacing the header of your file by this :

<%@ Control Language="c#" AutoEventWireup="true" Inherits="Coveo.UI.CoveoSearch" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> <%@ Register TagPrefix="coveoui" Namespace="Coveo.UI.Controls" Assembly="Coveo.UI" %>

In that case, you won't need the SitecoreUtilities.

Gravatar for jschjolberg@awareweb.com

Comment by jschjolberg, Jun 17, 2014 2:31 PM

I'm am working in an MVC environment so my code is in a view (cshtml) - I've added a using statement to the view for Coveo.UI. The template is being loaded into the view like this:

Do you think the fact that I'm using MVC could be causing this not to work?

Gravatar for vseguin@coveo.com

Comment by Vincent Séguin, Jun 17, 2014 2:35 PM

OH! You're in MVC, this is not the same thing :)

In which build are you? April? MVC is not offcially supported in the April build, this is a new feature of Coveo for Sitecore June release.

I suggest we continue this discussion by mail, this will be more efficient.

Ask a question