Sample code for 30+ languages & platforms
PowerBuilder

Outlook -- Search Messages in a Particular Folder

See more Outlook Examples

Demonstrates search the messages in a particular Outlook mailbox folder.

This uses the OData $filter and $search system query options. See OData System Query Options for general information.

Also see OData URL Conventions for information about $filter, $search and other query options.

This example demonstrates the following searches:

  • Find emails from a particular email address.
  • Find emails where the subject contains a particular word or phrase.
  • Using an "AND" expression.
  • Find emails received in the last 24 hours.
  • Find emails received in October 2016
  • Find unread emails
  • Find emails containing a particular phrase in the body.
  • Free-text search of a keyword or phrase.
  • Free-text search of an email address.
Note: This example requires Chilkat v9.5.0.68 or greater.

This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_SbResponse
oleobject loo_HtFolderMap
oleobject loo_SbMap
string ls_FolderId
oleobject loo_Json
oleobject loo_SbExpression
oleobject loo_Dt
oleobject loo_DtObj

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Use your previously obtained access token here:
// See the following examples for getting an access token:
//    Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
//    Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
//    Refresh Access Token (Azure AD v2.0 Endpoint).
//    Refresh Access Token (Azure AD Endpoint).

loo_Http.AuthToken = "MICROSOFT_GRAPH_ACCESS_TOKEN"

loo_SbResponse = create oleobject
li_rc = loo_SbResponse.ConnectToNewObject("Chilkat.StringBuilder")

// In this example, we'd like to get the messages in the folder "/Inbox",
// but we must specify the corresponding folder_id.  The best way to do this is to create
// a local map of folderPaths-to-folderIds.
// This example does it:  Create Outlook Folder Map)
loo_HtFolderMap = create oleobject
li_rc = loo_HtFolderMap.ConnectToNewObject("Chilkat.Hashtable")

loo_SbMap = create oleobject
li_rc = loo_SbMap.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbMap.LoadFile("qa_data/outlook/folderMap.xml","utf-8")
loo_HtFolderMap.AddFromXmlSb(loo_SbMap)

// Get the ID for the "/Inbox" folder:
ls_FolderId = loo_HtFolderMap.LookupStr("/Inbox")
if loo_HtFolderMap.LastMethodSuccess <> 1 then
    Write-Debug "Folder ID not found"
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    return
end if

li_Success = 1
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.EmitCompact = 0

loo_Http.SetUrlVar("folder_id",ls_FolderId)
loo_Http.SetUrlVar("select","subject,from")

// -----------------------------------------------------------------------------------------------------
// Only return emails from "chilkat.support@gmail.com"
loo_Http.SetUrlVar("filter","from/emailAddress/address eq 'chilkat.support@gmail.com'")

// To return the full content of each email, omit the "&select=..." part of the URL.
li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Only return emails where the subject contains "Amazon"
loo_Http.SetUrlVar("filter","contains(subject,'Amazon')")

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Only return emails where the subject starts with "this email" and the from address is support@chilkatsoft.com
// (the startswith function is case insensitive)
loo_Http.SetUrlVar("filter","startswith(subject,'this email') and (from/emailAddress/address eq 'support@chilkatsoft.com')")

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Only return emails received within the last 24 hours
loo_SbExpression = create oleobject
li_rc = loo_SbExpression.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbExpression.Append("receivedDateTime ge ")
loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.CkDateTime")

loo_Dt.SetFromCurrentSystemTime()
loo_Dt.AddDays(-1)
loo_SbExpression.Append(loo_Dt.GetAsTimestamp(0))

loo_Http.SetUrlVar("filter",loo_SbExpression.GetAsString())

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbExpression
    destroy loo_Dt
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Only return emails received in October 2016
loo_DtObj = create oleobject
li_rc = loo_DtObj.ConnectToNewObject("Chilkat.DtObj")

loo_DtObj.Year = 2016
loo_DtObj.Month = 10
loo_DtObj.Day = 1
loo_DtObj.Utc = 1
loo_Dt.SetFromDtObj(loo_DtObj)

loo_SbExpression.Clear()
loo_SbExpression.Append("(receivedDateTime ge ")
loo_SbExpression.Append(loo_Dt.GetAsTimestamp(0))
loo_SbExpression.Append(") and (receivedDateTime lt ")
loo_DtObj.Month = 11
loo_Dt.SetFromDtObj(loo_DtObj)
loo_SbExpression.Append(loo_Dt.GetAsTimestamp(0))
loo_SbExpression.Append(")")

// This is the expression we just built:  (receivedDateTime ge 2016-10-01T00:00:00Z) and (receivedDateTime lt 2016-11-01T00:00:00Z)
Write-Debug loo_SbExpression.GetAsString()

loo_Http.SetUrlVar("filter",loo_SbExpression.GetAsString())

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbExpression
    destroy loo_Dt
    destroy loo_DtObj
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Return unread emails
loo_Http.SetUrlVar("filter","isRead eq false")

// success = http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse);
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbExpression
    destroy loo_Dt
    destroy loo_DtObj
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Return emails with a plain-text or HTML body containing the phrase "Outlook 365"
loo_Http.SetUrlVar("filter","contains(body/content,'Outlook 365')")

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbExpression
    destroy loo_Dt
    destroy loo_DtObj
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Use the $search query option instead of $filter.
// $search is a free-text search over whatever fields the server deems appropriate, such as in the subject,
// body, address fields, etc.

// Search for the word Amazon
loo_Http.SetUrlVar("search","Amazon")

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbExpression
    destroy loo_Dt
    destroy loo_DtObj
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()

// -----------------------------------------------------------------------------------------------------
// Search for chilkatsoft.com
// Some chars, such as the "." make it necessary to enclose the search expression in double-quotes.
loo_Http.SetUrlVar("search","~"chilkatsoft.com~"")

li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",loo_SbResponse)
if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponse
    destroy loo_HtFolderMap
    destroy loo_SbMap
    destroy loo_Json
    destroy loo_SbExpression
    destroy loo_Dt
    destroy loo_DtObj
    return
end if

loo_Json.LoadSb(loo_SbResponse)
Write-Debug loo_Json.Emit()


destroy loo_Http
destroy loo_SbResponse
destroy loo_HtFolderMap
destroy loo_SbMap
destroy loo_Json
destroy loo_SbExpression
destroy loo_Dt
destroy loo_DtObj