Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHashtable.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkDtObj.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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).

    CkHttp::setCkAuthToken(http, "MICROSOFT_GRAPH_ACCESS_TOKEN")

    sbResponse.i = CkStringBuilder::ckCreate()
    If sbResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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)
    htFolderMap.i = CkHashtable::ckCreate()
    If htFolderMap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sbMap.i = CkStringBuilder::ckCreate()
    If sbMap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckLoadFile(sbMap,"qa_data/outlook/folderMap.xml","utf-8")
    CkHashtable::ckAddFromXmlSb(htFolderMap,sbMap)

    ; Get the ID for the "/Inbox" folder:
    folderId.s = CkHashtable::ckLookupStr(htFolderMap,"/Inbox")
    If CkHashtable::ckLastMethodSuccess(htFolderMap) <> 1
        Debug "Folder ID not found"
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        ProcedureReturn
    EndIf

    success = 1
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)

    CkHttp::ckSetUrlVar(http,"folder_id",folderId)
    CkHttp::ckSetUrlVar(http,"select","subject,from")

    ; -----------------------------------------------------------------------------------------------------
    ; Only return emails from "chilkat.support@gmail.com"
    CkHttp::ckSetUrlVar(http,"filter","from/emailAddress/address eq 'chilkat.support@gmail.com'")

    ; To return the full content of each email, omit the "&select=..." part of the URL.
    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

    ; -----------------------------------------------------------------------------------------------------
    ; Only return emails where the subject contains "Amazon"
    CkHttp::ckSetUrlVar(http,"filter","contains(subject,'Amazon')")

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

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

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

    ; -----------------------------------------------------------------------------------------------------
    ; Only return emails received within the last 24 hours
    sbExpression.i = CkStringBuilder::ckCreate()
    If sbExpression.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbExpression,"receivedDateTime ge ")
    dt.i = CkDateTime::ckCreate()
    If dt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDateTime::ckSetFromCurrentSystemTime(dt)
    CkDateTime::ckAddDays(dt,-1)
    CkStringBuilder::ckAppend(sbExpression,CkDateTime::ckGetAsTimestamp(dt,0))

    CkHttp::ckSetUrlVar(http,"filter",CkStringBuilder::ckGetAsString(sbExpression))

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbExpression)
        CkDateTime::ckDispose(dt)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

    ; -----------------------------------------------------------------------------------------------------
    ; Only return emails received in October 2016
    dtObj.i = CkDtObj::ckCreate()
    If dtObj.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDtObj::setCkYear(dtObj, 2016)
    CkDtObj::setCkMonth(dtObj, 10)
    CkDtObj::setCkDay(dtObj, 1)
    CkDtObj::setCkUtc(dtObj, 1)
    CkDateTime::ckSetFromDtObj(dt,dtObj)

    CkStringBuilder::ckClear(sbExpression)
    CkStringBuilder::ckAppend(sbExpression,"(receivedDateTime ge ")
    CkStringBuilder::ckAppend(sbExpression,CkDateTime::ckGetAsTimestamp(dt,0))
    CkStringBuilder::ckAppend(sbExpression,") and (receivedDateTime lt ")
    CkDtObj::setCkMonth(dtObj, 11)
    CkDateTime::ckSetFromDtObj(dt,dtObj)
    CkStringBuilder::ckAppend(sbExpression,CkDateTime::ckGetAsTimestamp(dt,0))
    CkStringBuilder::ckAppend(sbExpression,")")

    ; This is the expression we just built:  (receivedDateTime ge 2016-10-01T00:00:00Z) and (receivedDateTime lt 2016-11-01T00:00:00Z)
    Debug CkStringBuilder::ckGetAsString(sbExpression)

    CkHttp::ckSetUrlVar(http,"filter",CkStringBuilder::ckGetAsString(sbExpression))

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbExpression)
        CkDateTime::ckDispose(dt)
        CkDtObj::ckDispose(dtObj)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

    ; -----------------------------------------------------------------------------------------------------
    ; Return unread emails
    CkHttp::ckSetUrlVar(http,"filter","isRead eq false")

    ; success = http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse);
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbExpression)
        CkDateTime::ckDispose(dt)
        CkDtObj::ckDispose(dtObj)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

    ; -----------------------------------------------------------------------------------------------------
    ; Return emails with a plain-text or HTML body containing the phrase "Outlook 365"
    CkHttp::ckSetUrlVar(http,"filter","contains(body/content,'Outlook 365')")

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbExpression)
        CkDateTime::ckDispose(dt)
        CkDtObj::ckDispose(dtObj)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

    ; -----------------------------------------------------------------------------------------------------
    ; 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
    CkHttp::ckSetUrlVar(http,"search","Amazon")

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbExpression)
        CkDateTime::ckDispose(dt)
        CkDtObj::ckDispose(dtObj)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)

    ; -----------------------------------------------------------------------------------------------------
    ; Search for chilkatsoft.com
    ; Some chars, such as the "." make it necessary to enclose the search expression in double-quotes.
    CkHttp::ckSetUrlVar(http,"search",Chr(34) + "chilkatsoft.com" + Chr(34))

    success = CkHttp::ckQuickGetSb(http,"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",sbResponse)
    If (success <> 1) AND (CkHttp::ckLastStatus(http) = 0)
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponse)
        CkHashtable::ckDispose(htFolderMap)
        CkStringBuilder::ckDispose(sbMap)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbExpression)
        CkDateTime::ckDispose(dt)
        CkDtObj::ckDispose(dtObj)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(json,sbResponse)
    Debug CkJsonObject::ckEmit(json)


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbResponse)
    CkHashtable::ckDispose(htFolderMap)
    CkStringBuilder::ckDispose(sbMap)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbExpression)
    CkDateTime::ckDispose(dt)
    CkDtObj::ckDispose(dtObj)


    ProcedureReturn
EndProcedure