Global LINQ Filters
Global LINQ Filters
Coveo for Sitecore 4.1 (November 2018)
The Global LINQ Filters allow the creation of filters to be applied to every query that’s executed through the LINQ layer. This can be used to globally limit search results based on security or templates.
Global LINQ Filters are only available starting from Sitecore 7.2 rev. 140228 (initial release) or later.
To define a Global LINQ Filter, follow these steps:
-
Create a new class that inherits from the
Sitecore.ContentSearch.Pipelines.QueryGlobalFilters.ApplyGlobalLinqFilters
class. Override theGetQuery
virtual method and apply your custom logic, as shown in the following code sample:using Sitecore.ContentSearch.Pipelines.QueryGlobalFilters; namespace Coveo.Custom { public class CustomGlobalFilter : ApplyGlobalLinqFilters { protected override object GetQuery(QueryGlobalFiltersArgs args) { // Filter search results by keeping only those that have been created in the last 3 days. IQueryable<SearchResultItem> query = (IQueryable<SearchResultItem>)args.Query; query = query.Where(item => item.CreatedDate > DateTime.Now.AddDays(-3)); return query; } } }
-
Open your
Coveo.SearchProvider.Custom.config
configuration file and add the following lines in the<pipelines>
XML element:<contentSearch.getGlobalLinqFilters> <processor type="Coveo.Custom.GlobalQueryFilter, Coveo.Custom" /> </contentSearch.getGlobalLinqFilters>
-
Open LinqScratchPad (typically located under
http://SitecoreInstanceName/sitecore/admin/LinqScratchPad.aspx
) in your browser and execute a query to validate that the Global LINQ filter is working as expected.