Query

View as Markdown

Query

A class that defines a query that is used to query for Entry instance.

When Personalize creates a variant in the CMS, it assigns a "Variant Alias" to identify that specific variant. When fetching entry variants using the Delivery API, you can pass variant aliases in place of variant UIDs in thex-cs-variant-uidheader.

AddQuery

Add a custom query against specified key.

NameTypeDescription
keystring

field uid.

valuestring

field value

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().AddQuery("key", "value").Find<Entry>();

IncludeCount

Retrieve count and data of objects in result.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().IncludeCount().Find<Entry>();

IncludeBranch

Include branch for publish content.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().IncludeBranch().Find<Entry>();

IncludeFallback

Include fallback locale publish content, if specified locale content is not publish.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_Type_uid").Query().IncludeFallback().Find<Entry>();

IncludeMetadata

The includeMetadata method will add the meta information to the response.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_Type_uid").Query().IncludeMetadata().Find<Entry>();

includeEmbeddedItems

Include Embedded Objects (Entries and Assets) along with entry/entries details.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_Type_uid").Query().includeEmbeddedItems().Find<Entry>();

IncludeOnlyReference

Specifies an array of except keys that would be excluded in the response.

NameTypeDescription
keysstring[]

Array of the only reference keys to be included in response.

referenceKeystring

Key who has reference to some other class object.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_Type_uid").Query().IncludeOnlyReference(new String[]{"name", "description"},"referenceUid").Find<Entry>();

IncludeExceptReference

Specifies an array of except keys that would be excluded in the response.

NameTypeDescription
keysstring[]

Array of the except reference keys to be excluded in response.

referenceKeystring

Key who has reference to some other class object.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_Type_uid").Query().IncludeExceptReference(new String[]{"name", "description"},"referenceUid").Find<Entry>();

IncludeReference

Add a constraint that requires a particular reference key details.

NameTypeDescription
referenceFieldString

Key that to be constrained.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_Type_uid").Query().IncludeReference("reference_uid_1").Find<Entry>();

IncludeReferenceContentTypeUID

This method also includes the content type UIDs of the referenced entries returned in the response.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().IncludeReferenceContentTypeUID().Find<Entry>();

IncludeReference

Add a constraint that requires a particular reference key details.

NameTypeDescription
referenceFieldsstring[]

Array key that to be constrained.

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().IncludeReference(new String[]{"reference_uid_1", "reference_uid_2"}).Find<Entry>();

ReferenceIn

Retrieve entries based reference in query

NameTypeDescription
keystring

They key to constraint on

queryQuery

Query object

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentType contentTypeObj = stack.ContentType("contentType_id");


Query query = contentTypeObj.Query();

query.Where("email_address","[email protected]");


ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().ReferenceIn("user", query).Find<Entry>();

ReferenceNotIn

Retrieve entries based reference not in query

NameTypeDescription
keystring

The key to constraint on

queryQuery

Query object

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentType contentTypeObj = stack.ContentType("contentType_id");


Query query = contentTypeObj.Query();

query.Where("email_address","[email protected]");


ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().ReferenceNotIn("user", query).Find<Entry>();

Regex

Add a regular expression constraint for finding string values that match the provided regular expression.
NameTypeDescription
keystring

The key to be constrained.

regexstring

The regular expression pattern to match.

modifiersstring

Any of the following supported Regular expression modifiers.

  • use i for case-insensitive matching.
  • use m for making dot match newlines.
  • use x for ignoring whitespace in regex

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_type_uid").Query().Regex("name", "^browser").Find<Entry>();

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_type_uid").Query().Regex("name", "^browser", "i").Find<Entry>();

RemoveQuery

Remove provided query key from custom query if exist.

NameTypeDescription
keystring

Query name to remove.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_type_uid").Query().RemoveQuery("query_key").Find<Entry>();

SetCachePolicy

To set cache policy using Query instance.

NameTypeDescription
cachePolicyCachePolicy

CachePolicy instance

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_type_uid").Query().SetCachePolicy(CachePolicy.NetworkElseCache).Find<Entry>();

SetLocale

Sets the locale.

NameTypeDescription
Localestring

Locale to fetch entry

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_type_uid").Query().SetLocale("en-us").Find<Entry>();

SetHeader

To set headers for Contentstack rest calls.

NameTypeDescription
keystring

header name.

valuestring

header value against given header name.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_type_uid").Query().SetHeader("custom_key", "custom_value").Find<Entry>();

RemoveHeader

Remove header key.

NameTypeDescription
keystring

key to be remove from header

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> entry = await stack.ContentType("content_type_uid").Query().RemoveHeader("header_to_remove").Find<Entry>();

WhereTags

Include tags with which to search entries.

NameTypeDescription
tags (required)string[]

Array of tags you want to match in the entries

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().WhereTags(new String[] {"tag_1", tag_2}).Find<Entry>();

Skip

The number of objects to skip before returning any.

NameTypeDescription
numberstring

No of objects to skip.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Skip(20).Find<Entry>();

Limit

A limit on the number of objects to return.

NameTypeDescription
numberstring

No of objects to limit.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Limit(20).Find<Entry>();

Ascending

Sort the results in ascending order with the given key.

NameTypeDescription
fieldUidstring

The key to order by.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Ascending("name").Find<Entry>();

Descending

Sort the results in descending order with the given key.

NameTypeDescription
fieldUidstring

The key to order by.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Descending("name").Find<Entry>();

And

Combines all the queries together using AND operator

NameTypeDescription
queryObjectsList<Query>

List of Query instances on which AND query executes.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentType contentTypeObj = stack.ContentType("contentType_id");


Query query1 = contentTypeObj.Query();

query1.Where("username","content");


Query query2 = contentTypeObj.Query();

query2.Where("email_address","[email protected]");


List<Query> queryList = new List<Query>();

queryList.Add(query1);

queryList.Add(query2);


ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().And(queryList).Find<Entry>();

Or

Add a constraint to fetch all entries which satisfy any queries.

NameTypeDescription
queryObjectsList<Query>

List of Query instances on which OR query executes.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentType contentTypeObj = stack.ContentType("contentType_id");


Query query1 = contentTypeObj.Query();

query1.Where("username","content");


Query query2 = contentTypeObj.Query();

query2.Where("email_address","[email protected]");


List<Query> queryList = new List<Query>();

queryList.Add(query1);

queryList.Add(query2);


ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Or(queryList).Find<Entry>();

Except

Specifies list of field uids that would be excluded from the response.

NameTypeDescription
fieldUidstring[]

field uid which get excluded from the response.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Except(new String[] {"name", description }).Find<Entry>();

Only

Specifies an array of 'only' keys in BASE object that would be 'included' in the response.

NameTypeDescription
fieldUidstring[]

Array of the 'only' keys to be included in response.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Only(new String[] {"name", description }).Find<Entry>();

Exists

Add a constraint that requires, a specified key exists in response.

NameTypeDescription
keystring

The key to be constrained.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Exist("title").Find<Entry>();

NotExists

Add a constraint that requires, a specified key does not exists in response.

NameTypeDescription
keystring

The key to be constrained.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().NotExist("title").Find<Entry>();

NotEqualTo

Add a constraint to the query that requires a particular key's entry to be not equal to the provided value.

NameTypeDescription
keystring

the key to be constrained.

valueObject

The object that must not be equaled.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().NotEqualTo("title", "Demo").Find<Entry>();

Where

Add a constraint to the query that requires a particular key entry's value not be contained in the provided array.

NameTypeDescription
keystring

the key to be constrained.

valueObject

Field value which get included from the response.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Where("title", "Demo").Find<Entry>();

NotContainedIn

Add a constraint to the query that requires a particular key entry's value not be contained in the provided array.

NameTypeDescription
keystring

the key to be constrained.

valueObject[]

The list of values the key object should not be.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().NotContainedIn("title", new Object[]{ "Demo", "Welcome"}).Find<Entry>();

ContainedIn

Add a constraint to the query that requires a particular key's entry to be contained in the provided array.

NameTypeDescription
keystring

the key to be constrained.

valueObject[]

The possible values for the key's object.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().ContainedIn("title", new Object[]{ "Demo", "Welcome"}).Find<Entry>();

LessThanOrEqualTo

Add a constraint to the query that requires a particular key entry to be less than or equal to the provided value.

NameTypeDescription
keystring

the key to be constrained.

valueObject

The value that provides an upper bound or equal.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().LessThanOrEqualTo("age", 20).Find<Entry>();

LessThan

Add a constraint to the query that requires a particular key entry to be less than the provided value.

NameTypeDescription
keystring

the key to be constrained.

valueObject

The value that provides an upper bound.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().LessThan("age", 20).Find<Entry>();

GreaterThanOrEqualTo

Add a constraint to the query that requires a particular key entry to be greater than or equal to the provided value.

NameTypeDescription
keystring

the key to be constrained.

valueObject

The value that provides a lower bound or equal.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().GreaterThanOrEqualTo("age", 20).Find<Entry>();

GreaterThan

Add a constraint to the query that requires a particular key entry to be greater than the provided value.

NameTypeDescription
keystring

the key to be constrained.

valueObject

The value that provides an lower bound.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().GreaterThan("age", 20).Find<Entry>();

Find

Execute a Query and Caches its result (Optional)

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Find<Entry>();

FindOne

Execute a Query and Caches its result (Optional)

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().FindOne<Entry>();

Count

Retrieve only count of entries in result.

using Contentstack.Core; 

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");

ContentstackCollection<Entry> collection = await stack.ContentType("content_Type_uid").Query().Count();

Variants

The variants method retrieves details of a specific entry variant or an array of entry variants based on the applied query.

When Personalize creates a variant in the CMS, it assigns a "Variant Alias" to identify that specific variant. When fetching entry variants using the Delivery API, you can pass variant aliases in place of variant UIDs in the x-cs-variant-uid header.

NameTypeDescription
variant_headers (required)string | string[]

List of variants

Example:

using Contentstack.Core;

using Contentstack.Core.Models;


ContentstackClient stack = new ContentstackClient("api_key", "delivery_token", "environment");


ContentstackCollection<Entry> collection = await stack

						.ContentType("content_Type_uid")

						.Query()

						.Variant(new String[]{"variant_uid1/variant_alias1", "variant_uid2/variant_alias2"})

						.Find<Entry>();