4

Is there a built in way to get all items in an index that inherit from a base template? I've seen a custom solution doing something similar to what is described here and was surprised there wasn't a way to do this out of the box. Is this the case in the newest version of Sitecore (8.2 Update-5)? Are there any updates that can/should be made to a solution like this or can I consider this the gold standard?

As far as I know using the GetItem method on a SearchResultItem hits the database for item data so I can't use that to use any extension methods I have available on a normal item.

5

You are definitely right, GetItem on every result would completely defy the purpose of having an index in place and should be avoided.

Essentially nothing has changed. The information you link to is correct, although I prefer the original post on the subject which is focused entirely on a vanilla Sitecore. And as far as I can tell, everything in that article still holds true for Sitecore 8.

I don't have a Sitecore 8.2u5 on hand right now, but in 8.2u2 the code for the calculated _templates field looks like this. And as such, still only selects 1 layer of BaseTemplates for any item it indexes.

IEnumerable<string> collection = ((IEnumerable<TemplateItem>)item.Template.BaseTemplates)
    .Select<TemplateItem, string>((Func<TemplateItem, string>) 
        (item2 => IdHelper.NormalizeGuid(item2.ID)));

Code taken with dotPeek tends to sometimes be a bit hard on the eyes, but in essence this code would need to be recursive, to grab all the base templates in play. And it isn't.

Essentially, you need to create your own computed field that recursively works through BaseTemplates on the item - taking care that you don't end up in an endless recursive loop by circling over the same template more than once. Then use your computed field instead of the OOTB one supplied by Sitecore.

3
  • Is there any particular reason inheritance checking seems to be avoided? Even the Habitat project has its own extension method for template inheritance. In this case I'm referring to inheritance for items in general, not in an index. – Teeknow Aug 19 '17 at 0:18
  • 1
    Just one thing to note. Don't use recursion to do this. Use a Stack, it will perform much better, which is pretty important when indexing. – Richard Seal Aug 19 '17 at 2:12
  • 1
    I don't see a good reason, no. Oversight would be my guess. – Mark Cassidy Aug 19 '17 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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