Microsoft Graph API - $filter email address by domain - microsoft-graph-api

As Title, I try to filter my email by domain. But got a error message "ErrorInvalidUrlQueryFilter". How can I do?
API query:
https://graph.microsoft.com/v1.0/me/messages?$filter=endswith(from/emailAddress/address,'#xxx.com')
Response:
{
"error": {
"code": "ErrorInvalidUrlQueryFilter",
"message": "The query filter contains one or more invalid nodes.",
"innerError": {
"date": "2023-02-03T08:59:37",
"request-id": "b3a28220-91d1-40b5-a34a-10cc73069951",
"client-request-id": "74043780-6e6b-dc63-5434-0ac03412b3ca"
}
}
}

endsWith operator is not supported for filtering messages by from/emailAddress/address.
As a workaround you can use contains. It will have the same affect for email address as using endsWith if you will filter by #domain.
GET https://graph.microsoft.com/v1.0/me/messages?$filter=contains(from/emailAddress/address,'#xxx.com')

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: /sites?select=siteCollection,webUrl&filter=siteCollection/root%20ne%20null returns "Cannot enumerate sites"

Got error with call: /sites?select=siteCollection,webUrl&filter=siteCollection/root%20ne%20null
"error": {
"code": "invalidRequest",
"message": "Cannot enumerate sites",
"innerError": {
"request-id": "f73vvv65-d20e-xxx-ae30-829d46a981d6",
"date": "2019-10-24T17:17:03"
}
}
I strongly recommend you to go through this link. There is a typo with the request you are trying to make. $select and $filter are query options and hence, you'll have to use the following endpoint to obtain the desired result.
GET https://graph.microsoft.com/beta/sites?$select=siteCollection,webUrl&$filter=siteCollection/root%20ne%20null
Observe carefully that there is a $ sign before the filter query option.

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.

Resources