Generic extension methods for Sitecore ContentSearch

I would first like to mention that the code shown in this post is not originally mine. I’ve been using it quite often lately and made some tweaks to it which is why I think it deserves a quick blog post.

Some of the code originate from my colleague Uli Weltersbach, he wrote a blog post on how to index base templates in Sitecore.

The rest of the code I got from my colleague Bo Breiting. He wrote this blog post about his extension methods for Sitecore ContentSearch.

Bo’s extension methods are great but they did not have a type declaration set explicitly. You would typically want to use them on a SearchResultItem which makes them look like this:

Here we have set SearchResultItem as type on the IQueryable parameter so the compiler can infer what implementation of Where to use (the one from IEnumerable or from IQueryable).

But this has an issue when you are using your own derived SearchResultItem class.

When you call the extension methods the return type of the IQueryable will be denominated to a SearchResultItem thus it will not build.

An example

We have a derived SearchResultItem class as the simple one shown below.

Then we get our IQueryable<MySearchResultItem>  from our SearchContext and try to call one of the extension methods.

This code will not build since there is no conversion between a MySearchResultItem and a SearchResultItem.

The fix

To fix this we simply change the extension methods to take a generic type instead and constrain the generic type to be a SearchResultItem as shown below.

And now without changing the code in our repository class the code builds and the repository works.

When would you use this?

This code comes in handy if you do not want to call GetItem() on the returned Documents to get the field values directly from the item.

The consideration to take regarding GetItem() is of course performance. If you have a large set of results then you can potentially save many calls to the database.

Quite often you anyway would like to call GetItem further down your execution path for example to get the item url through the LinkManager.

Working with the SearchResultItem without getting the item have the disadvantage that you only have the field values that is set to stored.yes in your index.

How do you store values in your index then? You need to explicitly define what fields you like to store in your index by patching in the field names in the fieldMapping element in configuration and set the StorageType attribute to yes.

This will make your indexes grow in size so choose the right implementation approach for your solution.

If you want to store all field values in your index then check out Uli’s blog post about creating a custom FieldMapper that index all Sitecore fields.

If you want to use these extension methods then also remember to patch in a computed index field for the base templates as shown below.

That was it. If you have any nice extension methods for ContentSearch please share them in the comments below.

Anders Laub

Anders Laub Christoffersen

Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders has been nominated a Sitecore Technical MVP three years in a row for 2014, 2015 and 2016. Anders is now working as a Sr. Solutions Architect at Sitecore in Copenhagen.

One thought on “Generic extension methods for Sitecore ContentSearch

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website