In this article, I will continue showing how to execute the calls to Microsoft Graph API using some optional OData query parameters.
The part-1 of this article series can be found here. Let’s now see the remaining query parameters one by one.
$select
Explanation
When you fire an API call to MS Graph, it will return the JSON result with all the properties. You may not need all the properties of a result set always. When you want MS Graph API to return only certain properties, then use $select parameter to specify which properties you want in result set.
Please note that the no. of records in the result will not be affected with or without $select parameter, only what’s returned for each record in result will change.
Example
Open MS Graph Explorer here and fire the following call to get a list of files from OneDrive:
- https://graph.microsoft.com/v1.0/me/drive/root/children or click here, then press “Run Query”
See the data returned in JSON response.
Following is the result JSON snippet containing some of the values.
- {
- "createdBy": {
- "user": {
- "displayName": "Megan Bowen"
- }
- },
- "createdDateTime": "2017-07-31T18:56:29Z",
- "id": "01BYE5RZ6QN3ZWBTUFOFD3GSPGOHDJD36K",
- "lastModifiedDateTime": "2017-07-31T18:56:29Z",
- "name": "Attachments",
- "folder": {
- "childCount": 0
- },
- "size": 0
- },
- {
- "createdBy": {
- "user": {
- "displayName": "Megan Bowen"
- }
- },
- "createdDateTime": "2017-08-07T16:18:24Z",
- "id": "01BYE5RZ4CPC5XBOTZCFD2CT7SZFNICEYC",
- "lastModifiedDateTime": "2017-08-07T16:18:24Z",
- "name": "Contoso Clothing",
- "folder": {
- "childCount": 14
- },
- "size": 5912877
- }
Out of all these properties, what may be most important for you if you wanted to show the user his/her files from OneDrive? Of course, the name of the file/folder.
Fire following call in MS Graph Explorer to get only file name in result.
- https://graph.microsoft.com/v1.0/me/drive/root/children?$select=name or click here, then press “Run Query”
You will see the following result.
Following is the result JSON snippet containing some of the values.
- {
- "value": [
- {
- "@odata.etag": "\"{60F36ED0-85CE-4771-B349-E671C691EFCA},1\"",
- "name": "Attachments"
- },
- {
- "@odata.etag": "\"{A9D9C2AC-FF32-42EE-87C3-283CFCE061E1},1\"",
- "name": "Business Data"
- },
- {
- "@odata.etag": "\"{73E9E609-FA05-42D8-82BD-1239D821CDD8},1\"",
- "name": "Class Documents"
- }
- ]
- }
Now, MS Graph API returns only file/folder name, instead of all the details. (Of course, “etag” is included but ignore it)
You can also use more than one property separated by comma, e.g.
- https://graph.microsoft.com/v1.0/me/drive/root/children?$select=name,size
And, you will get the following result.
Following is the result JSON snippet containing some of the values.
- {
- "value": [
- {
- "@odata.etag": "\"{60F36ED0-85CE-4771-B349-E671C691EFCA},1\"",
- "name": "Attachments",
- "size": 0
- },
- {
- "@odata.etag": "\"{A9D9C2AC-FF32-42EE-87C3-283CFCE061E1},1\"",
- "name": "Business Data",
- "size": 39566226
- }
- ]
- }
If you type in the property that does not exist in the result set, then it will be ignored.
e.g. - fire the following call and see what happens:
- https://graph.microsoft.com/v1.0/me?$select=salary
Usage
In your programming, you can use $select query parameter to,
- Get only specific property in result set (to make efficient use of bandwidth)
- Specify those properties not returned in default result set
$search
Explanation
Using $search query parameter, you can specify free text search criteria for items being returned in result set. Please note that this parameter is currently supported for only “messages” and “person” entities. Microsoft Graph team will cover more entities later.
Some programmers are confused between $search and $filter parameters because both do similar operations. I will explain the difference briefly in the next section for the $filter.
When you specify $search parameter, it tells MS Graph API to include only those entities in the result which match the specified expression. Here what to match is left for the implementer, OData specification does not mandate anything. Hence when you specify search criteria with $search query parameter, the result will be dependent on what MS Graph API team has decided to implement. Mostly it works as free text search.
Example
Let’s say you want to find out emails which have “brainstorming” mentioned somewhere.
Open MS Graph Explorer here and fire the following call.
- https://graph.microsoft.com/v1.0/me/messages?$search=brainstorming or click here, then press “Run Query”
See the data returned in JSON response.
Following is the result JSON snippet containing some of the values.
- {
- "value": [
- {
- "createdDateTime": "2017-09-08T05:57:44Z",
- "lastModifiedDateTime": "2017-09-08T05:57:45Z",
- "hasAttachments": false,
- "subject": "DG2000+ Brainstorming Session",
- "bodyPreview": "Hello Product Launch Team...",
- "importance": "normal",
- "sender": {
- "emailAddress": {
- "name": "Miriam Graham",
- "address": "[email protected]"
- }
- },
- "ccRecipients": [],
- "bccRecipients": [],
- "replyTo": []
- }
- ]
- }
Graph API will match the word brainstorming in an email’s from, subject and body fields, and will show the matching messages in the result.
Let’s see another example of using $search with “people” entity.
Open MS Graph Explorer here and fire following call:
- https://graph.microsoft.com/v1.0/me/people?$search=langer or click here, then press “Run Query”
See the data returned in JSON response.
Following is the result JSON snippet containing some of the values.
- {
- "value": [
- {
- "id": "e3d0513b-449e-4198-ba6f-bd97ae7cae85",
- "displayName": "Isaiah Langer",
- "givenName": "Isaiah",
- "surname": "Langer",
- "jobTitle": "Web Marketing Manager",
- "department": "Sales & Marketing",
- "phones": [
- {
- "type": "business",
- "number": "+1 918 555 0101"
- }
- ]
- }
- ]
- }
MS Graph API will search for the people related to the sample account user, will search for “langer” in display name or email, and will return matching records in result.
There are many more possible calls using the $search parameter, but I will restrict to these two calls only for brevity. I encourage you to explore more by yourself.
Usage
In your programming, you can use $search parameter to:
- perform a free text search on messages or people related to user
- perform fuzzy searches on people
$filter
Explanation
With $filter parameter, you can specify the filter criteria on a result set using logical operators or string functions. There are fixed logical and string operations which can be performed on a result set using $filter function.
The following extract is taken from the OData specification website here
The $filter system query option allows clients to filter a collection of resources that are addressed by a request URL. The expression specified with $filter is evaluated for each resource in the collection, and only items where the expression evaluates to true are included in the response.
Let’s understand the difference between $search and $filter parameters,
- $search starts with an empty result set and adds items to it based on criteria that match, while $filter takes full result set, and removes items based on criteria that don't match.
- $search operates like a free text search, while $filter works on predefined logical and string functions.
Which logical and string functions are supported with $filter?
As per current MS Graph API documentation, following functions are supported, but the support varies as per which entity you are dealing with, so always refer to latest documentation on Microsoft site:
- equals (eq)
- not equals (ne)
- greater than (gt)
- greater than or equals (ge)
- less than (lt), less than or equals (le)
- and (and)
- or (or)
- not (not)
- startswith
- any
Example
Let’s see some examples of how you can use $filter parameter.
Open MS Graph Explorer here and fire the following call:
- https://graph.microsoft.com/v1.0/me/people?$filter=jobTitle eq null or click here, then press “Run Query”
See the data returned in JSON response, it returns all “users” associated with the sample account whose job title is not set,
Following is the result JSON snippet containing some of the values.
- {
- "value": [
- {
- "displayName": "Marketing",
- "jobTitle": null
- },
- {
- "displayName": "Product Launch Event",
- "jobTitle": null
- },
- {
- "displayName": "Retail",
- "jobTitle": null
- },
- {
- "displayName": "Tailspin Toys",
- "jobTitle": null
- },
- {
- "displayName": "Activewear",
- "jobTitle": null
- }
- ]
- }
Similarly, if you want to see only those records where job title is set, then simply change the operator “eq” (for equal to) to “ne” (for not equal to)
- https://graph.microsoft.com/v1.0/me/people?$filter=jobTitle ne null&$select=jobTitle,displayName or click here, then press “Run Query”
You will see the following response which will list all users who have job title assigned,
Following is the result JSON snippet containing some of the values.
- {
- "value": [
- {
- "id": "e3d0513b-449e-4198-ba6f-bd97ae7cae85",
- "displayName": "Isaiah Langer",
- "jobTitle": "Web Marketing Manager"
- },
- {
- "id": "48d31887-5fad-4d73-a9f5-3c356e68a038",
- "displayName": "Megan Bowen",
- "jobTitle": "Auditor"
- },
- {
- "id": "f5289423-7233-4d60-831a-fe107a8551cc",
- "displayName": "Ben Walters",
- "jobTitle": "VP Sales"
- }
- ]
- }
Please note here that you see only 2 properties in the response because I also used the $select parameter in the API call. You can use “&” in an API call when you want to use more than one optional query parameter,
https://graph.microsoft.com/v1.0/me/people?$filter=jobTitle ne null&$select=jobTitle,displayName
There are much more uses of $filter parameter possible, but I will restrict to only two examples for brevity. I suggest you read more on Microsoft Graph API documentation page here.
Usage
Use the $filter parameter in your programming to,
- make API calls with logical operations for checking equality, greater than, less than, etc. operations
$skipToken
Explanation
When a response from MS Graph API does not contain all the available records (due to the user explicitly specifying to select only some top records or due to server-side paging implemented in Graph API to limit response size), then you will see the response contains a server generated value with the $skipToken parameter. This value is to be specified in the next call to MS Graph API so that it will return the next set of records. The value of $skipToken is generated by the server, it can not be specified by you as a consumer.
Example
Fire the following call to get only top 10 files stored on user’s OneDrive,
- https://graph.microsoft.com/v1.0/me/drive/root/children?$top=10 or click here, then press “Run Query”
See the result, note the value in @odata.nextLink property,
Following is the result JSON snippet containing some of the values,
- {
- "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('48d31887-5fad-4d73-a9f5-3c356e68a038')/drive/root/children",
-
- "@odata.nextLink": "https:
- $skiptoken=Paged%3dTRUE%26p_SortBehavior%3d0%26p_FileLeafRef%3dAll%2520Japan%2520Revenues%2520By%2520City%252exlsx%26p_ID%3d33%26p_FileDirRef%3dpersonal%252fmeganb%255fm365x214355%255fonmicrosoft%255fcom%252fDocuments%26RootFolder%3d%252fpersonal%252fmeganb%255fm365x214355%255fonmicrosoft%255fcom%252fDocuments",
- "value": [
- {
-
- }
- ]
- }
The @odata.nextLink property contains the call which should be fired to get the next set of result. For identifying what should be returned, it has a skip token generated from server-side with some logic to skip those records in current response.
Simply fire the query mentioned in the @odata.nextLink property, and you will get next set of values.
Usage
- In your programming, you can use $skipToken to get paginated data (implement paging).
Limitations/Errors
Not all query parameters are supported for all entities. Always refer to latest Microsoft documentation for what’s supported. I also suggest firing the call in MS Graph Explorer first and see if you are getting the expected result or not, before starting any programming and later having any surprise.
If you include some query parameter for a non-supported entity, then MS Graph API will throw an appropriate error.
Let’s understand with an example.
Fire the following call in MS Graph Explorer:
- https://graph.microsoft.com/v1.0/users?$filter=123 or click here, then press “Run Query”
You will get error 400,
Following is the result JSON snippet containing some of the values,
- {
- "error": {
- "code": "BadRequest",
- "message": "Invalid filter clause",
- "innerError": {
- "request-id": "c19b6fbb-e3f4-4007-a012-eac483100272",
- "date": "2017-11-13T18:41:25"
- }
- }
- }
Using query parameter without ‘$’ prefix in beta version of MS Graph API
With beta version of MS Graph API, using “$” prefix before a query parameter is optional.
Fire following call in MS Graph Explorer to get top 2 recently accessed files in result,
- https://graph.microsoft.com/beta/me/drive/recent?top=2 or click here, then press “Run Query”
You will see the following result,
Following is the result JSON snippet containing some of the values,
- {
- "value": [
- {
- "@odata.type": "#microsoft.graph.driveItem",
- "id": "01BYE5RZ2KXWOTNNU3K5B3AZ4YMANXEMAE"
- },
- {
- "@odata.type": "#microsoft.graph.driveItem",
- "id": "01BYE5RZZ5OJSCSRM6BZDY7ZEFZ3NJ2QAY"
- }
- ]
- }
Note that the “top” query parameter is used without any “$” prefix here and still you get only 2 records in result.
This concludes my 2-part article series on MS Graph API query parameters. I hope you enjoyed learning about how to use query parameters in the MS Graph API call.
I suggest you keep reading more about MS Graph API until my next article (part-3).
You can read some of my articles on MS Graph API and Office 365 development here.