-1

I have a model that looks like this:

[Serializable]
[SitecoreType(TemplateId = "{guid}", AutoMap =true)]
public class EventInfo : GlassBase
{

    public virtual string PageTitle { get; set; }
    public virtual DateTime StartDate { get; set; }
    public virtual DateTime EndDate { get; set; }
    [SitecoreField("SmallTileImage")]
    public virtual Image SmallTileImage { get; set; }

}

I'm using this is my controller to return the item:

string query = string.Format("fast:{0}//*[@@templateid='{1}']", 
    eventfolder, eventTemplate);
var model = GetDataSourceItem<OtherEvents>();
model.RecentEvents = context.Query<EventInfo>(query)
     .Where(x => x.StartDate >= DateTime.Today).OrderBy(x => x.StartDate);

All the fields are being returned apart from the Image field, I've checked it's in web and master db. Also if I do this:

foreach (var ev in Model.RecentEvents)
{
    var item = Sitecore.Context.Database.GetItem(ev.Id);
    Sitecore.Data.Fields.ImageField imgField = ((Sitecore.Data.Fields.ImageField)item.Fields["SmallTileImage"]);
}

on my view then it returns the value as expected.

Any ideas?

1 Answer 1

-4

You need to create a Custom Search Index Field to get the url of your image. While Querying to Solr it will not return any complex Datatype like Image instead you need to create a custom Solr Index Field to get the image field url.

You could add a computed field. Here is John West's post about it. Below is a condensed example to get just the URL of the image.

Create a class that implements Sitecore.ContentSearch.ComputedFields.IComputedIndexField.

public class ImageIndexField : IComputedIndexField
{
    public string FieldName { get; set; }
    public string ReturnType { get; set; }

    public object ComputeFieldValue(IIndexable indexable)
    {
        Assert.ArgumentNotNull(indexable, "indexable");
        var indexableItem = indexable as SitecoreIndexableItem;

        if (indexableItem == null)
        {
            Log.Warn(string.Format("{0} : unsupported IIndexable type : {1}", this, indexable.GetType()), this);
            return null;
        }

        ImageField img = indexableItem.Item.Fields["MyImageField"];

        return img == null || img.MediaItem == null ? null : MediaManager.GetMediaUrl(img.MediaItem);
    }
}

Then, add a config include with something like this:

<sitecore>
<contentSearch>
    <configuration type="Sitecore.ContentSearch.LuceneProvider.LuceneSearchConfiguration, Sitecore.ContentSearch.LuceneProvider">
        <defaultIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
            <fields hint="raw:AddComputedIndexField">
                <field fieldName="MyImageFieldUrl" storageType="YES" indexType="TOKENIZED">sc70.Search.ComputedFields.ImageUrlIndexField, sc70</field>
            </fields>
        </defaultIndexConfiguration>
    </configuration>
</contentSearch>

Note that the field name is hard coded above. I'm not sure if it is possible to pass that in as a parameter from the config. Sitecore seems to be creating separate classes for each of their computed fields and using inheritance to get reuse.

Please refer this post: https://stackoverflow.com/questions/19588900/sitecore-index-for-image-type?rq=1

Also refer this article : https://jockstothecore.com/media-indexing-approaches/

2
  • 1
    Why would you need a custom search index field? The OP isn't using an index to get any of the content. Its just a Fast Query (now that should also be fixed...), but there are no indexes involved or needed here.
    – Richard Seal
    Aug 20, 2019 at 13:09
  • yes, that's correct. What should I use instead of Fast Query?
    – gadgetgem
    Aug 20, 2019 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.