Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Customize Microsoft Graph responses with query parameters

Feedback

In this article

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 useOData 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 thev1.0 andbeta endpoints.

Tip

On thebeta endpoint, the$ prefix is optional. For example, you can usefilter instead of$filter.On thev1.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 theOData V4 query language and are supported only inGET operations.

Select the examples to try them inGraph Explorer.

NameDescriptionExample
$countReturns the total count of matching resources./me/messages?$top=2&$count=true
$expandReturns related resources./groups?$expand=members
$filterFilters results (rows)./users?$filter=startswith(givenName,'J')
$formatReturns results in the specified media format./users?$format=json
$orderbyOrders results./users?$orderby=displayName desc
$searchReturns results based on search criteria./me/messages?$search=pizza
$selectFilters properties (columns)./users?$select=givenName,surname
$skipSkips 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
$topSets 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

NameDescriptionExample
$skipTokenReturns 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.

NameDescriptionExample
$countReturns the integer total of the collection.GET /users/$count
GET /groups/{id}/members/$count

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

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

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

JSON batching

Encoding query parameters

Percent-encode query parameter values according toRFC 3986. All reserved characters in query strings must be percent-encoded. Many HTTP clients, browsers, and tools (such as theGraph 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 thev1.0 endpoint. For more information about the issue and the recommended workaround, seeKnown 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 valuelet''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 aURL 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. SeeUse the $filter query parameter to filter a collection of objects.

Note

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

For example, the following request returns both thecontact collection of the current user and the number of items in thecontact 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 fromdirectoryObject, the$count query parameter is only supported inadvanced 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 theid andname 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 thedirectReports,manager, andmemberOf relationships on a user, but you can't expand itsevents,messages, orphoto relationships. Not all resources or relationships support using$select on expanded items.

  • With Microsoft Entra resources that derive fromdirectoryObject, likeuser andgroup,$expand typically returns a maximum of 20 items for the expanded relationship and has no@odata.nextLink. For details, seequery parameter limitations.

  • $expand isn't currently supported withadvanced queries.

Filter

Use the$filter query parameter to get just a subset of a collection. For guidance on using$filter, seeUse 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

Note

The$format query parameter supports many formats (for example,atom,xml, andjson) but results might not be returned in all formats.

OrderBy

Use the$orderby query parameter to specify the sort order of the items returned from Microsoft Graph. The default order is ascending.

For example, the following request returns users in the organization ordered by their display name in ascending order:

GET https://graph.microsoft.com/v1.0/users?$orderby=displayName

Some APIs support sorting by complex type entities. The following request gets messages and sorts them by theaddress field of thefrom property, which is of the complex typeemailAddress:

GET https://graph.microsoft.com/v1.0/me/messages?$orderby=from/emailAddress/address

To sort results in ascending or descending order, append eitherasc ordesc to the field name, separated by a space; for example,?$orderby=name desc (unencoded),?$orderby=name%20desc (URL encoded). If you don't specify the sort order, ascending order is inferred.

With some APIs, you can order results on multiple properties. For example, the following request orders the messages in the user's Inbox, first by the name of the person who sent it in descending order (Z to A), and then by subject in ascending order (default).

GET https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?$orderby=from/emailAddress/name desc,subject

Note

When you specify$filter, the service infers a sort order for the results. If you use both$orderby and$filter to get messages, because the server always infers a sort order for the results of a$filter, you mustspecify properties in certain ways.

The following example shows a query filtered by thesubject andimportance properties, and then sorted by thesubject,importance, andreceivedDateTime properties in descending order.

GET https://graph.microsoft.com/v1.0/me/messages?$filter=Subject eq 'welcome' and importance eq 'normal'&$orderby=subject,importance,receivedDateTime desc

Note

Combining$orderby and$filter query parameters is supported for directory objects. SeeAdvanced query capabilities on directory objects.

Search

Use the$search query parameter to restrict request results to match a search criterion. Its syntax and behavior varies across different resources. For more information, seeUse the $search query parameter to match a search criterion.

Select

Use the$select query parameter to return a subset of properties for a resource. With$select, you can specify a subset or a superset of the default properties.

When you make a GET request without using$select to limit the property data, Microsoft Graph includes a@microsoft.graph.tips property that provides a best practice recommendation for using$select similar to the following message:

"@microsoft.graph.tips": "Use $select to choose only the properties your app needs, as this can lead to performance improvements. For example: GET groups?$select=appMetadata,assignedLabels",

For example, when getting the messages of the signed-in user, you can specify that only thefrom andsubject properties be returned:

GET https://graph.microsoft.com/v1.0/me/messages?$select=from,subject

Important

We recommend that you use$select to limit the properties returned by a query to those needed by your app. This is especially true for queries that might potentially return a large result set. Limiting the properties returned in each row reduces network load and improves your app's performance.

Inv1.0, some Microsoft Entra resources that derive fromdirectoryObject, likeuser andgroup, return a limited, default subset of properties on reads. For these resources, you must use$select to return properties outside of the default set.

Skip

Use the$skip query parameter to set the number of items to skip at the start of a collection.For example, the following request returns events for the user sorted by date created, starting with the 21st event in the collection:

GET  https://graph.microsoft.com/v1.0/me/events?$orderby=createdDateTime&$skip=20

Some Microsoft Graph APIs, like Outlook Mail and Calendars (message,event, andcalendar), use$skip to implement paging. When query results span multiple pages, these APIs return an@odata.nextLink property with a URL that contains a$skip parameter. You can use this URL to return the next page of results. To learn more, seePaging.

Directory objects such asuser,group, andapplication don't support$skip.

SkipToken

Some requests return multiple pages of data, either due to server-side paging or due to using the$top parameter to limit the page size of the response. Many Microsoft Graph APIs use theskipToken query parameter to reference subsequent pages of the result.
This parameter contains an opaque token that references the next page of results and is returned in the URL provided in the@odata.nextLink property in the response. To learn more, seePaging.

Note

If you're using OData Count (adding$count=true in the query string) for queries against directory objects, the@odata.count property is present only in the first page.

TheConsistencyLevel header required for advanced queries against directory objects isn't included by default in subsequent page requests. It must be set explicitly in subsequent pages.

Top

Use the$top query parameter to specify the number of items to be included in the result.

If more items remain in the result set, the response body contains an@odata.nextLink parameter. This parameter contains a URL that you can use to get the next page of results. To learn more, seePaging.

The minimum value of $top is 1 and the maximum depends on the corresponding API.

For example, the followinglist messages request returns the first five messages in the user's mailbox:

GET https://graph.microsoft.com/v1.0/me/messages?$top=5

Note

TheConsistencyLevel header required for advanced queries against directory objects isn't included by default in subsequent page requests. It must be set explicitly in subsequent pages.

Error handling for query parameters

Some requests return an error message if a specified query parameter isn't supported. For example, you can't use$expand on theuser/photo relationship.

https://graph.microsoft.com/v1.0/me?$expand=photo
{    "error":{        "code":"ExpandNotSupported",        "message":"Expand is not allowed for property 'Photo' according to the entity schema.",        "innerError":{            "request-id":"1653fefd-bc31-484b-bb10-8dc33cb853ec",            "date":"2017-07-31T20:55:01"        }    }}

However, sometimes query parameters specified in a request fail silently. For example, for unsupported query parameters and for unsupported combinations of query parameters. In these cases, examine the data returned by the request to determine whether the query parameters you specified had the desired effect.

Related content


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?