MS Graph filter or search always returns default result based on any query Parameters

Chukwuma Obi 41 Reputation points
2022-09-05T10:21:57.433+00:00

Hi,

Issue
I have an "account A" let say peter@ourdomain.com, I want to return all emails that contains 'paul@jaswant .com' in inbox or in sent items.

I have tried multiple queries on PowerShell but it returns the default emails all the time. Is there a particular requirement in terms of license for this feature.

PowerShell Test Script

$clientID = "xxx"  
$Clientsecret = "xxx"  
$tenantID = "xxx"  
  
#Connect to GRAPH API  
$tokenBody = @{  
    Grant_Type    = "client_credentials"  
    Scope         = "https://graph.microsoft.com/.default"  
    Client_Id     = $clientId  
    Client_Secret = $clientSecret  
}  
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody  
$headers = @{  
    "Authorization" = "Bearer $($tokenResponse.access_token)"  
    "Content-type"  = "application/json"  
}  
  
#$URLsend = "https://graph.microsoft.com/v1.0/users/someemail@somemail.ltd/messages?$select=sender,subject$filter=from/emailAddress/address eq 'someaccount@outlook.com'"  
  
#$URLsend = "https://graph.microsoft.com/v1.0/users/someemail@somemail.ltd/messages?$filter=from/emailAddress/address+eq+'someaccount@outlook.com'"  
  
  
$URLsend = " https://graph.microsoft.com/v1.0/users/someemail@somemail.ltd/mailFolders/inbox/messages?$select=id,receivedDateTime,subject,from&$filter=singleValueExtendedProperties/any(ep:ep/id eq 'String 0x5D01' and ep/value eq 'someaccount@outlook.com')&$orderby=receivedDateTime DESC"  
  
  
try {  
  
   $request =  Invoke-RestMethod -Method GET -Uri $URLsend -Headers $headers   
   $request.value  
} catch [System.SystemException]  {  
  Write-Host $_  
  
}  

I have tried various $URLsend parameters commented above

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,644 questions
0 comments No comments
{count} votes

Accepted answer
  1. Glen Scales 4,431 Reputation points
    2022-09-06T00:04:57.273+00:00

    When you use $Search in PowerShell it will take that as a variable and do Variable substitution so you need to escape it with the string using "`$Search" character eg

    $URLsend = "https://graph.microsoft.com/v1.0/users/user@domain/mailFolders/inbox/messages?`$select=id,receivedDateTime,subject,from&`$filter=singleValueExtendedProperties/any(ep:ep/id eq 'String 0x5D01' and ep/value eq 'someaccount@outlook.com')&`$orderby=receivedDateTime DESC"  
    

    you can test your query in the Graph Explorer as well but that works okay for me in PowerShell when its escaped.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Srinivasa Rao Darna 6,686 Reputation points Microsoft Vendor
    2022-09-05T17:35:14.28+00:00

    Hi @Chukwuma Obi ,

    You may have to write a complex $filter using from toRecipients instead I recommend to use search API.
    Below API should get all emails with queryString paul@jaswant .com.

    POST https://graph.microsoft.com/v1.0/search/query  
    Content-type: application/json  
    {  
        "requests": [  
            {  
                "entityTypes": [  
                    "message"  
                ],  
                "query": {  
                    "queryString": "paul@abc.com"  
                }  
            }  
        ]  
    }  
    

    Refer to search-concept-messages for more information.

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.