Gravatar for gaur.arun777@gmail.com

Question by Arun Sharma, Jul 24, 2017 5:33 AM

Is there any way to store coveo index field value in a variable?

Hi Team,

I need to hold any Sitecore coveo indexed filed value in a variable and after some modification want to show to end user:

like:

{{=image(raw.@(Model.ToCoveoFieldName("BioimageUrl", false)), {alt : "Anything"})   }}

That Get the Bioimage URL that is computed by:

<fieldfieldName="BioimageUrl"sourceField="ProfileImage">Coveo.SearchProvider.ComputedFields.ImageUrlComputedField, Coveo.SearchProviderBase</field>

So now want i want, I wanna use secure media url and want as below:

@Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(url);

So how can I get above BioimageUrl into variable and assign to 'url' and after that how can I show image with protected url.

Thanks in advance.

regards,

Arun Sharma

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Jul 24, 2017 1:46 PM

As François mentioned, you will need a custom computed field for secure media. The reason is that the `@(Model.ToCoveoFieldName("BioimageUrl",false))` code runs on the server-side to output the translated field name only. Then, on the client side, the code `{{= image(raw.fbioimageurlXXXXX, { alt: "Anything" }) }}` is running to create an `<img />` element with the URL from the result field and the provided alternative text. You cannot execute server code with the client-side URL at that point.

1 Reply
Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Jul 24, 2017 12:39 PM

`Coveo.SearchProvider.ComputedFields.ImageUrlComputedField` is an OOTB computed field that provides basic image URL support. If you want to have some custom code, you need to code your own computed field.

You could do something similar to:

public override object ComputeFieldValue(IIndexable p_Indexable) {
    IItem currentItem = p_Indexable as IItem;
    SitecoreItemDataField field = currentItem.GetField("YOUR_FIELD_NAME") as SitecoreItemDataField;
    ImageField imageField = new Sitecore.Data.Fields.ImageField(field);
    string mediaUrl = MediaManager.GetMediaUrl(imageField.MediaItem);
    return Sitecore.Resources.Media.HashingUtils.ProtectAssetUrl(mediaUrl);
}

This code has not been tested, this is just to put you on the right track :)

If you need more information on custom computed fields, refer to: Implementing and Configuring a Computed Field in Your Index

Ask a question