Function-specified Filter clause doesn't work for getting Sharepoint item list in Graph API - microsoft-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')

Related

How to filter users in microsoft graph list?

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

Filtering users by business phone number with MS Graph API fails

According to the MS Graph API documentation, businessPhones are supported to be used in a $filter query with eq comparison, just like the imAddresses property.
When inspecting Microsoft's use query parameters documentation, there's an example where the imAddresses property is used in a collection filter and it works just fine.
GET https://graph.microsoft.com/v1.0/users?$filter=imAddresses/any(s:s eq 'admin#contoso.com')
My goal ist to list all users that have a specific phone number in their businessPhones collection property.
However, when I try to use the businessPhones property in a similar query, the query does not work as expected.
GET https://graph.microsoft.com/v1.0/users?$filter=businessPhones/any(s:s eq '1234')
Status code: 400
{
"error": {
"code": "Request_UnsupportedQuery",
"message": "Unsupported or invalid query filter clause specified for property 'businessPhones' of resource 'User'.",
"innerError": {
"date": "2021-07-30T08:07:24",
"request-id": "ac3923be-de11-448f-b2b5-245edc82d20e",
"client-request-id": "ac3923be-de11-448f-b2b5-245edc82d20e"
}
}
}
Any ideas on what I am missing?
You need to use advanced query capabilities, which means you need to add a $count=true query string parameter and a ConsistencyLevel=Eventual header to your request.

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.

$filter "not groupTypes/any() " on Groups in Microsoft Graph

The following works in Graph Explorer:
https://graph.microsoft.com/v1.0/groups?$filter=groupTypes/any(c:c eq 'Unified')
However, this does not:
https://graph.microsoft.com/v1.0/groups?$filter=not groupTypes/any()
This returns
{
"error": {
"code": "BadRequest",
"message": "Filter not supported.",
"innerError": {
"request-id": "d0c9fac3-9ee4-4b92-8841-6dbefca2d150",
"date": "2017-07-20T13:37:18"
}
}
}
Am I constructing the OData $filter the wrong way or is it really not supported? (use case: give me all groups except unified ones)
The verb not isn't recognized by Graph. I expect what you're looking for here would be ne such as:
https://graph.microsoft.com/v1.0/groups?$filter=groupTypes/any(c:c ne 'Unified')
You can read about the $filter parameter in the Graph Documentation.
https://graph.microsoft.com/v1.0/me/memberOf/$/microsoft.graph.group?$filter=groupTypes/any(a:a%20eq%20'unified')
You can read about it in Documentation - Reference https://learn.microsoft.com/en-us/graph/overview

Resources