Mike Robbins

Sitecore Developer Blog

Follow me on GitHub

Sitecore ContentSearch exclude standard values

Using the SearchContext class within Sitecore the result collection brings back the standard values item along with any items that are matched.

To solve this problem you can add a computed field to the Lucene index to return a bool property to Linq to say whether the item is a standard value of not.

This means you can search and exclude standard values like this.

context.GetQueryable<ClassName>().Where(i => i.TemplateId == DataAccess.GlobalItems.CarTemplate.ID && !i.IsStandardValue).ToList();

Add the following tag to the AddComputedIndexField section of your index.

<fields hint="raw:AddComputedIndexField">
<field fieldName="isstandardvalue" storageType="no" indexType="untokenized">ToolBox.SitecoreCMS.ContentSearch.ComputedFields.IsStandardValue, ToolBox.SitecoreCMS</field></fields>

Here is the class that adds the computed field to the index

using System;
using System.Text;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data;
using Sitecore.Data.Items;
namespace ToolBox.SitecoreCMS.ContentSearch.ComputedFields
{
    public class IsStandardValue : IComputedIndexField
    {
        public object ComputeFieldValue(IIndexable indexable)
        {
            Item item = indexable as SitecoreIndexableItem;
            return StandardValuesManager.IsStandardValuesHolder(item);
        }
        public string FieldName { get; set; }
        public string ReturnType { get; set; }
    }
}