Gravatar for dnull@rightpoint.com

Question by null, May 30, 2016 1:37 PM

SitecoreUtilities.ToCoveoFieldName() in Coveo For Sitecore 4?

SitecoreUtilities.ToCoveoFieldName appears to have been removed in Coveo for Sitecore 4.

How do I get, from code--say, a controller--, the Coveo field name for an index field?

1 Reply
Gravatar for sbelzile@coveo.com

Answer by Sébastien Belzile, May 30, 2016 2:19 PM

It as indeed been removed, this removal was necessary to support changing the indexing root of an index.

How do I get, from code--say, a controller--, the Coveo field name for an index field?

Answer:

You need to fetch the FieldNameTranslator defined on the Coveo index that matches your context. In a controller, the SitecoreHelper class used by Coveo models implements the methods that you are interested in:

  • ISearchIndex GetFirstIndexForDatabase(string p_DatabaseName)
  • IFieldNameTranslator GetFieldNameTranslator(ISearchIndex p_Index)
Gravatar for dnull@rightpoint.com

Comment by null, May 30, 2016 4:01 PM

Awesome thanks sbelzile.I was able to get the field name with the following:

var sitecoreHelper = new SitecoreHelper();
var itemAsIndexable = (IIndexable)(SitecoreIndexableItem)Sitecore.Context.Item;
var searchIndex = sitecoreHelper.GetSearchIndex(itemAsIndexable);
var fieldNameTranslator = sitecoreHelper.GetFieldNameTranslator(searchIndex);
string coveoFieldName = fieldNameTranslator.TranslateToCoveoFormat("My Field Name", true);
Gravatar for sbelzile@coveo.com

Comment by Sébastien Belzile, May 30, 2016 4:18 PM

Add some null check or call the GetSearchIndex() method with the name of the context content database.

The code sample you provided will explode if the context item is not located under the root of your index. In other words sitecoreHelper.GetSearchIndex(itemAsIndexable); may return null.

Gravatar for dnull@rightpoint.com

Comment by null, May 31, 2016 7:32 AM

Good call, thanks again!

Ask a question