Searching Items in Sitecore without Solr Indexes

In Sitecore, we often need to build lists or search for a particular item to achieve this. Most of the time, we use searches with Solr, but sometimes, we need an alternative or a fallback to ensure that items are obtained correctly.

In this article, we will review how to perform a search without using Solr, considering recursive programming.

For example, to obtain a list of child items with several levels in the content tree

The ManualSearch class will help us find specific Sitecore items by searching through a hierarchy of items starting from a given root. It looks for items that match a particular template ID and collects them in a list. The search skips items named _subcontent and continues recursively through each item’s children.

The updated version ensures that it handles null values gracefully and caches the template ID for better performance. This makes the search process efficient and thorough, ensuring we get all the items that meet our criteria.

public class ManualSearch
{
    //With this global collection we will save all the items that meet the conditions in our search.
    private List<Item> ListItems { get; set; }
    //This is the general method that starts the operation.
    public List<Item> GetListItems(Item itemPath)
    {
        ListItems = new List<Item>();
        GetSitecoreItems(itemPath);
        return ListItems;
    }
    //Method that performs recursive search
    private void GetSitecoreItems(Item item)
    {
        foreach (Item child in item.Children)
        {
            //Here you can skip certain items in your search
            if (child.Name.Equals("_subcontent")) continue; 
            //You can do a deep search, if any of the children have the template you need, it will depend on the depth of the tree you have.
            if (child.Children.Any(x => x.TemplateID == new ID(YOUR_TEMPLATE_ID)))
            {
                child.Children
                    .Where(x => x.TemplateID == new ID(YOUR_TEMPLATE_ID))
                    //You can add here all the other conditions you need.
                    .ToList().ForEach(x => { ListItems.Add(x); });
            }
            else
            {
                //In this section we continue with the search
                GetSitecoreItems(child);
            }
        }
    }
}

For example, to obtain a specific item in the content tree

This example starts with the SearchItem method, which takes a root item and a field value and calls the GetSitecoreItemFromChildren method to perform the actual search.

If a match is found, it returns the matching item; otherwise, it continues the search through the children until a match is found or all children are checked.

public class ManualSearch
{
    //This is the general method that starts the operation.
    public Item SearchItem(Item rootItem, string fieldValue)
    {
        return GetSitecoreItemFromChildren(rootItem, fieldValue);
    }
    //Method that performs recursive search
    public Item GetSitecoreItemFromChildren(Item item, string fieldValue)
    {
        foreach (Item child in item.Children)
        {
            if (child.Name.Equals("_subcontent")) continue;
            var children = child.Children?.ToList() ?? new List<Item>();
            if (children.Any(x => x.TemplateID == new ID("YOUR_TEMPLATE_ID")))
            {
                var resultItem = children.FirstOrDefault(x => x.Fields["YOUR_FIELD_VALUE"].Value == fieldValue && x.TemplateID == new ID("YOUR_TEMPLATE_ID"));
                if (resultItem != null) return resultItem;
            }
            else
            {
                var result = GetSitecoreItemFromChildren(child, fieldValue);
                if (result != null) return result;
            }
        }
        return null;
    }
}

And that's it! With the previous examples, we can perform a search in Sitecore without Solr indexes to obtain a list of items or a particular item, considering conditionals and additional parameters in a search.

Thanks for reading!

If you have any questions or ideas in mind, it'll be a pleasure to communicate with you and exchange knowledge with each other.

X / LinkedIn - esdanielgomez.com


Similar Articles