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

Chukwuma Obi 41 Reputation points


I have an "account A" let say, 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         = ""  
    Client_Id     = $clientId  
    Client_Secret = $clientSecret  
$tokenResponse = Invoke-RestMethod -Uri "$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody  
$headers = @{  
    "Authorization" = "Bearer $($tokenResponse.access_token)"  
    "Content-type"  = "application/json"  
#$URLsend = "$select=sender,subject$filter=from/emailAddress/address eq ''"  
#$URLsend = "$filter=from/emailAddress/address+eq+''"  
$URLsend = "$select=id,receivedDateTime,subject,from&$filter=singleValueExtendedProperties/any(ep:ep/id eq 'String 0x5D01' and ep/value eq '')&$orderby=receivedDateTime DESC"  
try {  
   $request =  Invoke-RestMethod -Method GET -Uri $URLsend -Headers $headers   
} catch [System.SystemException]  {  
  Write-Host $_  

I have tried various $URLsend parameters commented above

Microsoft Graph Mail API
Microsoft Graph Mail API
A Microsoft API that supports accessing data in users' primary mailboxes and in shared mailboxes.
739 questions
No comments
{count} votes

Accepted answer
  1. Glen Scales 3,981 Reputation points MVP

    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 = "`$select=id,receivedDateTime,subject,from&`$filter=singleValueExtendedProperties/any(ep:ep/id eq 'String 0x5D01' and ep/value eq '')&`$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 - MSFT 6,346 Reputation points

    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.

    Content-type: application/json  
        "requests": [  
                "entityTypes": [  
                "query": {  
                    "queryString": ""  

    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.