List of all posts in the #30DaysMSGraph series
In day 4 we discussed the syntax for Microsoft Graph requests. Today we’ll begin looking at query parameters available for requests against Microsoft Graph. For a full listing of the query parameters available and additional information / examples please see the full documentation: Use query parameters to customize responses.
Filter
Filter is one of the more common query parameters that you may use or encounter. Filter retrieves a subset of rows from the total results that match the specified conditions.
Syntax
<baseGraphQuery>?$filter=<conditions>
Ex. – Get users whose display name starts with “A”
https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,’A’)
Select
Select allows for retrieving a subset or superset of properties rather than the default properties. This can be especially helpful if you are returning many records that have a high number of default properties returned. Reduce memory usage and network bandwidth usage by limiting the number of properties returned to only the ones that your app needs.
Syntax
<baseGraphQuery>?$select=<property1>,<property2>,…,<propertyN>
Ex. – Get name, size, and webUrl of logged in user’s OneDrive site files
https://graph.microsoft.com/v1.0/me/drive/root/children?$select=name,size,webUrl
OrderBy
OrderBy will sort the results returned from a query using the specified property. Certain APIs do support multiple properties to sort on.
Syntax
<baseGraphQuery>?$orderby=<property1>,<property2>,…,<propertyN>
Ex. – Get logged in user’s contacts sorted by birthday (default ascending)
https://graph.microsoft.com/v1.0/me/contacts?$orderby=birthday
Additionally, you can specify ascending or descending order by appending a space plus “asc” (default in most cases) or “desc” to the property name.
Ex. – Get logged in user’s contacts sorted by birthday descending.
https://graph.microsoft.com/v1.0/me/contacts?$orderby=birthday desc
Format
Specific endpoints from Microsoft Graph will offer the ability to return results in different formats. This might include CSV (comma separated value) downloadable files, JSON (JavaScript object notation), or other popular formats. Endpoints that support the Format query parameter will default to one format but allow alternate supported formats to be requested.
Syntax
<baseGraphQuery>?$format=<formatValue>
Ex. – Get report of Office 365 Group activity in JSON format
https://graph.microsoft.com/beta/reports/getOffice365GroupsActivityGroupCounts(period=’D7′)?$format=application/json
Ex. – Get report of Office 365 Group activity in CSV format
https://graph.microsoft.com/beta/reports/getOffice365GroupsActivityGroupCounts(period=’D7′)?$format=text/csv
Note: To properly test out this call you will need to log into the Graph Explorer as an Office 365 Global Administrator or an Azure AD Administrator who can grant OAuth permissions to an account. For the time being simply note the syntax.
Try It Out
Use the query parameters covered today to filter, sort, and specify columns. Navigate to the Graph Explorer. Execute the following commands.
- Get logged in user’s high priority emails.
- https://graph.microsoft.com/v1.0/me/messages?$filter=importance eq ‘High’
- Get name, size, and webUrl of logged in user’s OneDrive site files.
- https://graph.microsoft.com/v1.0/me/drive/root/children?$select=name,size,webUrl
- Get logged in user’s contacts sorted by birthday.
- https://graph.microsoft.com/v1.0/me/contacts?$orderby=birthday
Join us tomorrow as we continue with query parameters available for Microsoft Graph requests in Day 6.