Query filters

Warning

You can use this approach to create queries in the JavaScript SDK. Be advised that this is not the recommended way of creating queries in the Web SDK; use LINQ instead.

Query object

The Querying client requires a query to be defined. The query supports the following parameters:

Parameter

Description

Filter

The type of filter to be invoked.

Skip

The number of entities to be skipped.

Take

The number of entities to be retrieved.

LoadConfiguration

The load configuration to be applied.

Sorting

The sorting options to be applied.

IdQueryFilter

Filters on the entity IDs.

The value parameter is used to query on a single ID.

RequestResponse

import { IdQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/id-query-filter";
import { ComparisonOperator } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/comparison-operator";
import { Query } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/query";

var idQueryFilter = new IdQueryFilter({
  value: entityId,
  operator: ComparisonOperator.Equals
});

var query = new Query({
  filter: idQueryFilter
});

The values parameter is used to filter on multiple IDs.

RequestResponse

var idQueryFilter = new IdQueryFilter({
  values: [entityId],
  operator: ComparisonOperator.Equals
});

The supported comparison operators are:

  • Equals

  • NotEquals

The Equals comparison operator is used by default when no operator is specified.

DefinitionQueryFilter

Provides the ability to filter on an entity definition.

The name parameter is used to filter on the name of a single entity definition.

RequestResponse

import { DefinitionQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/definition-query-filter";
import { ComparisonOperator } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/comparison-operator";
import { Query } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/query";

var definitionQueryFilter = new DefinitionQueryFilter({ name: "M.Asset", operator: ComparisonOperator.Equals });

var query = new Query({
  filter: definitionQueryFilter
});

The names parameter is used to filter on multiple entity definition names.

RequestResponse

var definitionQueryFilter = new DefinitionQueryFilter({ names: ["M.Asset", "M.Fragment"], operator: ComparisonOperator.Equals });

The supported comparison operators are:

  • Equals

  • NotEquals

The Equals comparison operator is used by default when no operator is specified.

PropertyQueryFilter

Filters on entities based on one or more property values. The specified data type will determine the type of the value and the valid comparison operators.

Note that the data-type string can be used to query on datasource properties.

The value parameter is used when filtering on a single value.

RequestResponse

import { Query } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/query";
import { PropertyQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/property-query-filter";
import { FilterDataType } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/filter-data-type";
import { ComparisonOperator } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/comparison-operator";

var propertyQueryFilter = new PropertyQueryFilter({
  operator: ComparisonOperator.Equals,
  property: "fileName",
  value: "test.jpg",
  dataType: FilterDataType.String
});

var query = new Query({
  filter: propertyQueryFilter
});

Filtering on multiple values is also possible by using the values parameter. These values will be interpreted using an OR operator. This means that any entity with the given property, matching one of the specified values will be returned.

RequestResponse

var propertyQueryFilter = new PropertyQueryFilter({
  operator: ComparisonOperator.Equals,
  property: "fileName",
  values: ["test.jpg", "test2.jpg"],
  dataType: FilterDataType.String
});

A culture can be specified to filter on using the culture parameter. This only applies for multilingual properties.

RequestResponse

var propertyQueryFilter = new PropertyQueryFilter({
  operator: ComparisonOperator.Equals,
  property: "fileName",
  value: "test.jpg",
  dataType: FilterDataType.String,
  culture: "en-US"
});

The supported comparison operators depend on the specified data type. An exception will be thrown if the comparison operator is not valid in the given context.

RelationQueryFilter

Filters on entities that have a relation to a specified entity using one or more parent IDs. The name of the relation needs to be specified in the relation parameter.

The parentId parameter is used when filtering on a single relation entity.

RequestResponse

import { Query } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/query";
import { RelationQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/relation-query-filter";

var relationQueryFilter = new RelationQueryFilter ({
  relation: "AssetTypeToAsset",
  parentId: assetTypeId
});

var query = new Query({
  filter: relationQueryFilter
});

Multiple parent IDs can be specified using the parentIds parameter. The IDs will be interpreted using an OR operator. This means that any entity with the given property, matching one of the specified values will be returned.

RequestResponse

var relationQueryFilter = new RelationQueryFilter ({
  relation: "AssetTypeToAsset",
  parentIds: [assetTypeId, assetTypeId2]
});

var query = new Query({
  filter: relationQueryFilter
});

CreatedByQueryFilter

Filters on entities created by one or more users.

Filters can be applied by specifying one or more IDs, using either the id or ids property.

RequestResponse

import { Query } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/query";
import { CreatedByQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/createdby-query-filter";
import { ComparisonOperator } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/comparison-operator";

var createdByQueryFilter = new CreatedByQueryFilter({
  id: userId,
  operator: ComparisonOperator.Equals
});

var query = new Query({
  filter: createdByQueryFilter
});

When specifying multiple IDs using the ids parameter, all entities that were created by one of the given user IDs will be returned.

RequestResponse

var createdByQueryFilter = new CreatedByQueryFilter({
  ids: [userId, userId2],
  operator: ComparisonOperator.Equals
});

It is also possible to specify one or more usernames using the username or usernames property.

RequestResponse

var createdByQueryFilter = new CreatedByQueryFilter({
  username: "Administrator",
  operator: ComparisonOperator.Equals
});

When specifying multiple usernames, any entity created by one of the given users will be returned.

RequestResponse

var createdByQueryFilter = new CreatedByQueryFilter({
  usernames: ["Administrator", "M.Demo.User"],
  operator: ComparisonOperator.Equals
});

The supported comparison operators are:

  • Equals

  • NotEquals

The Equals comparison operator is used by default when no operator is specified.

CompositeQueryFilter

Allows combination of different query filters using AND and OR operators. The query filters can be specified using the children parameter of the composite query filter. The combine method operator can be specified using the combineMethod parameter.

RequestResponse

import { Query } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/query";
import { PropertyQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/property-query-filter";
import { FilterDataType } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/filter-data-type";
import { ComparisonOperator } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/comparison-operator";
import { DefinitionQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/definition-query-filter";
import { CompositeQueryFilter } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/composite-query-filter";
import { CompositeFilterOperator } from "@sitecore/sc-contenthub-webclient-sdk/dist/contracts/querying/filters/composite-filter-operator";

var definitionQueryFilter = new DefinitionQueryFilter({
  names: ["M.Asset", "M.Fragment"],
  operator: ComparisonOperator.Equals
});

var propertyQueryFilter = new PropertyQueryFilter({
  operator: ComparisonOperator.Equals,
  property: "fileName",
  value: "test.jpg",
  dataType: FilterDataType.String
});

var compositeQueryFilter = new CompositeQueryFilter({
  children: [
    definitionQueryFilter,
    propertyQueryFilter
  ],
  combineMethod: CompositeFilterOperator.Or
});

var query = new Query({
  filter: compositeQueryFilter
});

Query load configuration

Load configurations can be set on the LoadConfiguration of the Query object.

Note

EntityLoadOptions from the older SDK has been deprecated; use LoadConfiguration instead.

Do you have some feedback for us?

If you have suggestions for improving this article,