Blog / Development

Getting to Know the Kentico Cloud .NET SDK Delivery API Filters


by Bryan Soltis

Jun 14, 2017

Once you get started developing with Kentico Cloud, you’ll quickly realize the power of the platform is its accessibility. By leveraging industry-standard services to retrieve information, companies can architect robust solutions capable of delivering content to any device. In this article, I’ll be introducing you to the various filters that are available within the Delivery API to help you understand how to get your content quickly. 

Incorporating a cloud-based CMS into your projects means there will be a lot of calls to the system to get data. With Kentico Cloud, this means a whole mess of Delivery API calls to get the information. Luckily, the platform has several built-in capabilities to simplify data retrieval and help you get your content.

Because Kentico Cloud content is designed to be multi-channel, data of all types can be stored in the system. This means in Kentico Cloud, your website information lives right alongside your mobile app details. To help you keep all this data organized, Kentico Cloud has content types, taxonomies, locations, and other resources you can leverage to keep things sorted.

 Because there are so many ways to categorize your content, you will need to know how to filter items when retrieving them for your applications. Say hello to the Delivery API Filters. These powerful functions can help you retrieve the exact information you need, in a variety of ways. Here’s a brief overview of each filter currently available in the API.

AllFilter

This filter is used to match a field or property to all the items in a collection of values.

products = await client.GetItemsAsync<Cafe>(
                new EqualsFilter("system.type", "product"),
                new AllFilter("elements.features", "sla,free_consulting_hours")
            );

Examples
- Filtering for articles that contain all the specified keywords
- Returning products that match a set of criteria or multiple values

AnyFilter

This filter can be used to return content items that match any of the values in a collection. The AnyFilter is typically used for multi-valued elements such as taxonomy or multiple choice.

var products = await client.GetItemsAsync<Cafe>(
                new EqualsFilter("system.type", "product"),
                new AnyFilter("elements.color", "red,blue")
);

Examples
- Returning items that match any value in a specific collection

ContainsFilter

This filter is very useful when you want to check if a field or element contains a value. This may apply when you are looking for text in a specific element, or only know part of the phrase.

new ContainsFilter("system.codename", "abc")

Examples
- Trying to find blog articles with a specific keyword
- Searching in text elements for a phrase

GreaterThanFilter / GreaterThanOrEqualFilter / LessThanFilter/LessThanOrEqualFilter

These filters are used to determine if a value is greater or less than the specified value. This can be very useful when working with dates and numeric elements.

new GreaterThanFilter("system.name", "123")
new GreaterThanOrEqualFilter("system.name", "123")
new LessThanFilter("system.name", "123")
new LessThanOrEqualFilter("system.name", "123")
new LessThanFilter("elements.date", DateTime.Now.ToString("yyyy-MM-dd"))

Examples
- Filtering articles by dates
- Limiting results by setting an element value threshold

InFilter

This filter is useful when you are determining if a field or element contains a value in the specified collection. The InFilter is typically used for single-valued elements such as price or type.

new InFilter("system.codename", "abc,efg,hij")

Examples
- Filtering results by a collection of values
- Pulling back multiple types of tags for articles

RangeFilter

This filter is helpful when you want to limit results based on a pre-defined range. The filter will compare the specified value to see if it falls between the range limits. 

new RangeFilter("system.codename", "0", "100")
new Range("elements.date", DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"))

Examples
- Retrieving content pages with a rating within a specific range
- Returning articles published between two dates

Moving forward

As this blog shows, there are a lot of different ways to filter data retrieved from Kentico Cloud. By learning how and when to use each type of filter, you can easily return the content you need, and reduce the amount of programming and calls.

You can learn loads more about the Kentico Cloud Delivery API here.

You can find out more about the Kentico .NET SDK here.

Good luck!