1

I am constructing a listing page by querying Solr Index where I am getting

[Title], [Summary], [Published Date], [Body] etc.,

The Solr index is returning all the indexed fields and so far so good. The problem for me is if [Summary] field is not populated by the content author, I have to get some dynamic text (like a couple of lines) from [Body] field, which is an RTE. Any thoughts/ ideas on how to achieve this?

2 Answers 2

3

You can create computed index field and write logic according to that if Summary field is empty in that case it will go to Body field. And logic will be like that into your computed index field class. It is not exact code it's only condition should be like below.

public object ComputeFieldValue(IIndexable indexable)
   {
      Item item = indexable as SitecoreIndexableItem;

      if (item == null)
           return null;

     if(!string.IsNullOrEmpty(item.Fields["Summary"].Value)) {
        return item.Fields["Summary"].Value
       }
    else {
    return item.Fields["Body"].Value
    }
}

Then use this computed index field into your query and rebuild your indexes, I think it will start working.

Hope it helps you.

1

To populate some dynamic text when the summary field is not available so you can use the computed field for the summary field and in that computed field you can check if any value is coming or not and on that basis, you can add some dynamic text.

You can check more about the computed field here https://doc.sitecore.com/xp/en/developers/90/platform-administration-and-architecture/create-a-computed-index-field.html

2
  • Thanks for writing. The dynamic text I required should be some random text like first two lines from [Body] field of that article. Each article has its own description and they are not same. Also, I can't define some static text to the computed field.
    – Yeshwanth
    Commented Aug 24, 2022 at 13:50
  • You do not need to write static text, you can write your own logic to get text Commented Aug 24, 2022 at 14:44

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.