Edit

Customize Microsoft Graph responses with query parameters

Query parameters help you optimize Microsoft Graph API responses by controlling exactly what data is returned. Instead of retrieving all available properties and data, you can use query parameters to:

  • Filter results to get only the records you need
  • Select specific properties to reduce response size and improve performance
  • Sort and paginate data for better user experiences
  • Expand related resources to get connected data in a single request

This article explains how to use OData system query options and other Microsoft Graph query parameters effectively. You learn the syntax, see practical examples, and discover best practices for building efficient queries that enhance your application's performance.

Support for specific query parameters varies between API operations and can differ between the v1.0 and beta endpoints.

Tip

On the beta endpoint, the $ prefix is optional. For example, you can use filter instead of $filter. On the v1.0 endpoint, the $ prefix is optional for only a subset of APIs. For simplicity, always include $ across all versions.

OData system query options

A Microsoft Graph API operation might support one or more of the following OData system query options. These query options are compatible with the OData V4 query language and are supported only in GET operations.

Select the examples to try them in Graph Explorer.

Name Description Example
$count Returns the total count of matching resources. /me/messages?$top=2&$count=true
$expand Returns related resources. /groups?$expand=members
$filter Filters results (rows). /users?$filter=startswith(givenName,'J')
$format Returns results in the specified media format. /users?$format=json
$orderby Orders results. /users?$orderby=displayName desc
$search Returns results based on search criteria. /me/messages?$search=pizza
$select Filters properties (columns). /users?$select=givenName,surname
$skip Skips items in a result set. Also used by some APIs to implement paging and can be used with $top to manually page results. /me/messages?$skip=11
$top Sets the page size of results. /users?$top=2

To find the OData system query options that an API and its properties support, see the "Properties" table in the resource page and the "Optional query parameters" section of the LIST and GET operations for the API.

Other query parameters

Name Description Example
$skipToken Returns the next page of results from result sets that span multiple pages. (Some APIs use $skip instead.) /users?$skiptoken=X%274453707402000100000017...

Other OData URL capabilities

The following OData 4.0 capabilities are URL segments, not query parameters.

Name Description Example
$count Returns the integer total of the collection. GET /users/$count
GET /groups/{id}/members/$count

Get a count of users
$ref Updates entities membership to a collection. POST /groups/{id}/members/$ref

Add a member to a group
$value Returns or updates the binary value of an item. GET /me/photo/$value

Get the photo for a user, group, or team
$batch Combines multiple HTTP requests into a batch request. POST /$batch

JSON batching

Encoding query parameters

Percent-encode query parameter values according to RFC 3986. All reserved characters in query strings must be percent-encoded. Many HTTP clients, browsers, and tools (such as the Graph Explorer) handle this encoding automatically. If a query fails, a possible cause is failure to encode the query parameter values appropriately. Sometimes, you need to double-encode values.

Note

There's a known issue with encoding ampersand (&) symbols in $search expressions on the v1.0 endpoint. For more information about the issue and the recommended workaround, see Known issue: $search for directory objects fails for encoded ampersand (&) character.

For example, an unencoded URL looks like this:

GET https://graph.microsoft.com/v1.0/users?$filter=startswith(givenName, 'J')

The properly percent-encoded URL looks like this:

GET https://graph.microsoft.com/v1.0/users?$filter=startswith(givenName%2C+'J')

The double-encoded URL looks like this:

GET https://graph.microsoft.com/v1.0/users?$filter=startswith%28givenName%2C%20%27J%27%29

Escaping single quotes

For requests that use single quotes, if any parameter values also contain single quotes, they should be double escaped; otherwise, the request fails because of invalid syntax. In the example, the string value let''s meet for lunch? has the single quote escaped.

GET https://graph.microsoft.com/v1.0/me/messages?$filter=subject eq 'let''s meet for lunch?'

Count

Use the $count query parameter to get the count of the total number of items in a collection or matching an expression. You can use $count in the following ways:

  1. As a query string parameter with the syntax $count=true to include a count of the total number of items in a collection alongside the page of data values returned from Microsoft Graph. For example, users?$count=true.
  2. As a URL segment to get only the integer total of the collection. For example, users/$count.
  3. In a $filter expression with equality operators to get a collection of data where the filtered property is an empty collection. See Use the $filter query parameter to filter a collection of objects.

Note

  1. On resources that derive from directoryObject, $count is only supported in an advanced query. See Advanced query capabilities on directory objects.
  2. Use of $count isn't supported in Azure AD B2C tenants.

For example, the following request returns both the contact collection of the current user and the number of items in the contact collection in an @odata.count property.

GET  https://graph.microsoft.com/v1.0/me/contacts?$count=true

For directory objects, that is, resources that derive from directoryObject, the $count query parameter is only supported in advanced queries.

Expand

Many Microsoft Graph resources expose both declared properties of the resource and its relationships with other resources. These relationships are also called reference properties or navigation properties, and they can reference either a single resource or a collection of resources. For example, the mail folders, manager, and direct reports of a user are all exposed as relationships.

You can use the $expand query string parameter to include the expanded resource or collection referenced by a single relationship (navigation property) in your results. For some APIs, only one relationship can be expanded in a single request.

The following example gets root drive information along with the top-level child items in a drive:

GET https://graph.microsoft.com/v1.0/me/drive/root?$expand=children

With some resource collections, you can also specify the properties to be returned in the expanded resources by adding a $select parameter. The following example performs the same query as the previous example but uses a $select statement to limit the properties returned for the expanded child items to the id and name properties.

GET https://graph.microsoft.com/v1.0/me/drive/root?$expand=children($select=id,name)

Note

  • Not all relationships and resources support the $expand query parameter. For example, you can expand the directReports, manager, and memberOf relationships on a user, but you can't expand its events, messages, or photo relationships. Not all resources or relationships support using $select on expanded items.

  • With Microsoft Entra resources that derive from directoryObject, like user and group, $expand typically returns a maximum of 20 items for the expanded relationship and has no @odata.nextLink. For details, see query parameter limitations.

  • $expand isn't currently supported with advanced queries.

Filter

Use the $filter query parameter to get just a subset of a collection. For guidance on using $filter, see Use the $filter query parameter to filter a collection of objects.

Format

Use the $format query parameter to specify the media format of the items returned from Microsoft Graph.

For example, the following request returns the users in the organization in JSON format:

GET https://graph.microsoft.com/v1.0/users?$format=json