Query filters

Warning

This is not the recommended way of creating queries. 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.

Parameter 'Value' should be used to query on a single id.

RequestResponse
var query = new Query
  {
    Filter = new IdQueryFilter { Value = entityId }
  };

The parameter 'Values' should be used to filter on multiple ids.

RequestResponse
var query = new Query
  {
    Filter = new IdQueryFilter {
      Values = new List<long>() { assetId, assetTypeId }
    }
  };

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 should be used to filter on the name of a single entity definition.

RequestResponse
var query = new Query
  {
    Filter = new DefinitionQueryFilter { Name = "M.Asset" }
  };

The 'Names' parameter should be used to filter on multiple entity definition names.

RequestResponse
var query = new Query
  {
    Filter = new DefinitionQueryFilter {
      Names = new string[] { "M.Asset", "M.Fragment" }
    }
  };

It is also possible to filter on the entity definition by id, using the 'Id' parameter.

RequestResponse
var query = new Query
{
  Filter = new DefinitionQueryFilter {
    Id = assetDefinitionId
  }
};

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 datatype string can be used to query on datasource properties.

The 'Value' parameter should be used when filtering on a single value.

RequestResponse
var query = new Query
  {
    Filter = new PropertyQueryFilter {
      Property = "Title",
      Value = "Logo",
      DataType = FilterDataType.String
    }
  };

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

RequestResponse
var query = new Query
  {
    Filter = new PropertyQueryFilter {
      Property = "Title",
      Values = new string[] { "Logo", "Sitecore Content Hub" },
      DataType = FilterDataType.String
    }
  };

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

RequestResponse
var query = new Query
  {
    Filter = new PropertyQueryFilter {
      Property = "Description",
      Value = "Sitecore Content Hub Logo",
      DataType = FilterDataType.String,
      Culture = new CultureInfo("en-US")
    }
  };

The supported comparison operators depend on the specified data type. An exception will be thrown when 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 should be specified when filtering on a single relation entity.

RequestResponse
var query = new Query
  {
    Filter = new RelationQueryFilter {
      Relation = "AssetTypeToAsset",
      ParentId = assetTypeId
    }
  };

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

RequestResponse
var query = new Query
  {
    Filter = new RelationQueryFilter {
      Relation = "AssetTypeToAsset",
      ParentIds = new long[] { logoAssetTypeId, mediaAssetTypeId }
    }
  };

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
var query = new Query
  {
    Filter = new CreatedByQueryFilter()
    {
      Id = userId
    }
  };

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

RequestResponse
var query = new Query
  {
    Filter = new CreatedByQueryFilter()
    {
      Ids = new long[] { demoUserId, trainingUserId }
    }
  };

It is also possible to specify one or more usernames using the 'Username' or 'Usernames' property.

RequestResponse
var query = new Query
  {
    Filter = new CreatedByQueryFilter()
    {
      Username = "M.Demo.User"
    }
  };

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

RequestResponse
var query = new Query
  {
    Filter = new CreatedByQueryFilter()
    {
      Usernames = new string[] { "M.Demo.User", "M.Training.User" }
    }
  };

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 the 'AND', 'OR' combine 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
var query = new Query
  {
    Filter = new CompositeQueryFilter()
      {
        Children = new QueryFilter[] {
          new RelationQueryFilter
          {
            Relation = "AssetTypeToAsset",
            ParentId = logoAssetTypeId
          },
          new PropertyQueryFilter {
            Property = "Title",
            Values = new string[] { "Logo", "Sitecore Content Hub" },
            DataType = FilterDataType.String
          }
        },
        CombineMethod = CompositeFilterOperator.Or
      }
  };

Query load configuration

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

See Load configurations for more information.

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,