How to filter users in microsoft graph list? - microsoft-graph-api

I am using https://graph.microsoft.com/v1.0/users
endpoint from the Microsoft Graph API to get the users from the organisation. it is giving me a list of all the users but I want just users, those are created after 01-01-2023.
I have tried using above endpoint with https://graph.microsoft.com/v1.0/users?$filter=create/dateTime ge '2023-01-01T00:00' but it is giving me below error.
{
"error": {
"code": "Request_UnsupportedQuery",
"message": "Property 'dateTime' does not exist as a declared property or extension property.",
"innerError": {
"date": "2023-01-17T08:19:15",
"request-id": "3129e8a3-0f00-4e14-99fc-2741f50d12d1",
"client-request-id": "28ecaa31-31dd-3c00-7d7c-474173eb1512"
}
}
}
I don't know how to filter in the above endpoint with time. If you could help?

The correct property name is createdDateTime. Do not use quotes around date and time in this case.
https://graph.microsoft.com/v1.0/users?$filter=createdDateTime ge 2023-01-01T00:00:00Z
To filter between two dates:
https://graph.microsoft.com/v1.0/users?$filter=createdDateTime ge 2023-01-01T00:00:00Z and createdDateTime le 2023-01-07T00:00:00Z

Related

How to $filter appRoleAssignments based on appRoleId?

I have an enterprise application registered in Azure AD Tenant. It contains certain appRoles which have been assigned to Azure AD Users. Now, I would like to fetch all the users having some specific appRoles.
I have tried this:
GET /servicePrincipals/{id}/appRoleAssignedTo
taken from here:https://learn.microsoft.com/en-us/graph/api/serviceprincipal-list-approleassignedto?view=graph-rest-beta&tabs=http#optional-query-parameters
It seems like I am able to fetch all the appRoleAssignments successfully using this API, but whenever I put a filter such as: appRoleId eq {app-role-id} I am geting error like:
{
"error": {
"code": "Request_BadRequest",
"message": "Invalid filter clause appRoleId: System.Guid",
"innerError": {
"date": "2021-10-25T16:33:41",
"request-id": "{request-id}",
"client-request-id": "{client-request-id}"
}
}
}
And whenever I put single quotes, like appRoleId eq '{app-role-id}', I get this error:
{
"error": {
"code": "BadRequest",
"message": "Invalid filter clause",
"innerError": {
"date": "2021-10-25T16:34:30",
"request-id": "{request-id}",
"client-request-id": "{client-request-id}"
}
}
}
I tried with both v1 and beta endpoint. So how do I filter on appRoleId?
It seems that Microsoft Graph does not support the $filter on appRoleId yet. They already have an open issue:
https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/990
There is also a feature request here which we can upvote: https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/application-api-add-support-for-filtering-approleassignment-by/idi-p/2433822
Also in the Doc, $filter is only provided on principalDisplayName and resourceId
[1]: https://i.stack.imgur.com/ZVD8Y.png

Function-specified Filter clause doesn't work for getting Sharepoint item list in Graph API

I'm trying to get a list of SharePoint items using Graph API with a Filter clause, but I get an error when I specify a function such as StartsWith.
The following page says, "Support for the $filter operator varies depending on the Microsoft Graph API. Commonly supported are the following logical operators." Is it possible to use them?
https://learn.microsoft.com/en-us/graph/query-parameters#filter-parameter
■In the case of eq, data can be retrieved
/sites/{site-id}/lists/{list-id}/items?$expand=fields&$filter=fields/Title eq 'test'
■In the case of startsWith, an error will occur saying it is an invalid filter clause.
/sites/{site-id}/lists/{list-id}/items?$expand=fields&$filter=fields/Title startsWith 'test'
ErrorMessage :
{
"error": {
"code": "BadRequest",
"message": "Invalid filter clause",
"innerError": {
"date": "2021-07-13T02:09:30",
"request-id": "5f84935b-59f1-46cf-a160-18d64e989eb7",
"client-request-id": "e417c442-123d-ba3b-1465-8ff7f4f78645"
}
}
}
You should specify startswith in your Graph API URL as :
$filter=startswith(fields/Title,'test')

How can i search MS Graph API for toRecipients

i am trying to to get email via graph APi based on the toRecipients . Is that posible or is it still only avail via search and not filter ?
I tried
https://graph.microsoft.com/beta/me/mailFolders('SentItems')/messages?$select=sender,subject,toRecipients&filter=(toRecipients/emailAddress/address) eq 'test#demo.com'
which generates the blow error.
{
"error": {
"code": "BadRequest",
"message": "Invalid filter clause",
"innerError": {
"request-id": "25583e87-66da-477b-a1be-0a0fd0371349",
"date": "2020-04-21T20:01:39"
}
}
}
i also tried To instead of toRecipients but that doesnt work eithet.
Can you try the following? Note the $filter query parameter.
https://graph.microsoft.com/beta/me/mailFolders('SentItems')/messages?$select=sender,subject,toRecipients&$filter=(toRecipients/emailAddress/address) eq 'test#demo.com'

Microsoft Graph API - Unable to filter with endswith

To filter Microsoft Graph API responses, the docs suggest to use the filter query option.
Here I've used startswith and endswith string functions on the '/me/messages' endpoint.
query = {
'$filter': "
startswith(from/emailAddress/address, 'abcd')
or endswith(from/emailAddress/address, 'gmail.com')"
}
Only using startswith returns the expected response. On adding the endswith function to the filter query, the response received is an error.
{
"error": {
"code": "ErrorInvalidUrlQueryFilter",
"message": "The query filter contains one or more invalid nodes.",
"innerError": {
"request-id": "0d12e1f6-6105-4826-9656-8613f8c167ed",
"date": "2019-03-14T11:05:56"
}
}
}
To use $filter with endsWith you need to:
Add $count=true to the query parameters
Add ConsistencyLevel: eventual request header
Example : 'https://graph.microsoft.com/v1.0/users?$count=true&ConsistencyLevel=eventual&$filter=endswith(mail,'#hotmail.com')'
**It works for mail and userPrincipalName but not for displayName
Copied this exact same query (with the ConsistencyLevel either in the query params or headers params and wasn't able to make it work. The app that I used to request authorisation token have Users.ReadWrite.All permission AND the User.Read.All.
In Postman :
GET https://graph.microsoft.com/v1.0/users?$count=true&ConsistencyLevel=eventual&$filter=endswith(mail,'#hotmail.com')
{
"error": {
"code": "Request_UnsupportedQuery",
"message": "Unsupported property filter clause operator 'SuffixMatch'.",
"innerError": {
"date": "2022-07-27T13:22:38",
"request-id": "3f509514-4e77-4eb1-8557-0fe102d502f5",
"client-request-id": "3f509514-4e77-4eb1-8557-0fe102d502f5"
}
}
}
----- EDIT ------
Just found my answer here : https://learn.microsoft.com/en-us/graph/query-parameters
Use of $count is not supported in Azure AD B2C tenants.

Filter files by date range in OneDrive

I would like to obtain the OneDrive content using Microsoft Graph filtered by date so that only files created between particular dates are obtained.
I have tried:
/v1.0/me/drive/items/{id}/children?filter=createdDateTime ge 2016-02-26T14:41:09Z
but get following response
"error": {
"code": "invalidRequest",
"message": "The request is malformed or incorrect.",
"innerError": {
"request-id": "c636022f-2fa9-4e41-b8fc-63be5fc5e681",
"date": "2017-04-07T10:05:46"
}
}
I have also tried:
/v1.0/me/drive/root/children?filter=name eq 'folderapr05'
and it works.
Does OneDrive support filter parameter for dates?
Filtering by dateTime isn't supported.
If you're attempting to do this for the purposes of syncing with OneDrive, you may want to take a look at delta. This method allows your app to track changes to a drive and its children over time.

Resources