Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Outlook -- Search Messages in a Particular FolderDemonstrates 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:
This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
#include <C_CkHttpW.h> #include <C_CkStringBuilderW.h> #include <C_CkHashtableW.h> #include <C_CkJsonObjectW.h> #include <C_CkDateTimeW.h> #include <C_CkDtObjW.h> void ChilkatSample(void) { HCkHttpW http; HCkStringBuilderW sbResponse; HCkHashtableW htFolderMap; HCkStringBuilderW sbMap; const wchar_t *folderId; BOOL success; HCkJsonObjectW json; HCkStringBuilderW sbExpression; HCkDateTimeW dt; HCkDtObjW dtObj; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // 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). CkHttpW_putAuthToken(http,L"MICROSOFT_GRAPH_ACCESS_TOKEN"); sbResponse = CkStringBuilderW_Create(); // 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 = CkHashtableW_Create(); sbMap = CkStringBuilderW_Create(); CkStringBuilderW_LoadFile(sbMap,L"qa_data/outlook/folderMap.xml",L"utf-8"); CkHashtableW_AddFromXmlSb(htFolderMap,sbMap); // Get the ID for the "/Inbox" folder: folderId = CkHashtableW_lookupStr(htFolderMap,L"/Inbox"); if (CkHashtableW_getLastMethodSuccess(htFolderMap) != TRUE) { wprintf(L"Folder ID not found\n"); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); return; } success = TRUE; json = CkJsonObjectW_Create(); CkJsonObjectW_putEmitCompact(json,FALSE); CkHttpW_SetUrlVar(http,L"folder_id",folderId); CkHttpW_SetUrlVar(http,L"select",L"subject,from"); // ----------------------------------------------------------------------------------------------------- // Only return emails from "chilkat.support@gmail.com" CkHttpW_SetUrlVar(http,L"filter",L"from/emailAddress/address eq 'chilkat.support@gmail.com'"); // To return the full content of each email, omit the "&select=..." part of the URL. success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // ----------------------------------------------------------------------------------------------------- // Only return emails where the subject contains "Amazon" CkHttpW_SetUrlVar(http,L"filter",L"contains(subject,'Amazon')"); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(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) CkHttpW_SetUrlVar(http,L"filter",L"startswith(subject,'this email') and (from/emailAddress/address eq 'support@chilkatsoft.com')"); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // ----------------------------------------------------------------------------------------------------- // Only return emails received within the last 24 hours sbExpression = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbExpression,L"receivedDateTime ge "); dt = CkDateTimeW_Create(); CkDateTimeW_SetFromCurrentSystemTime(dt); CkDateTimeW_AddDays(dt,-1); CkStringBuilderW_Append(sbExpression,CkDateTimeW_getAsTimestamp(dt,FALSE)); CkHttpW_SetUrlVar(http,L"filter",CkStringBuilderW_getAsString(sbExpression)); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbExpression); CkDateTimeW_Dispose(dt); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // ----------------------------------------------------------------------------------------------------- // Only return emails received in October 2016 dtObj = CkDtObjW_Create(); CkDtObjW_putYear(dtObj,2016); CkDtObjW_putMonth(dtObj,10); CkDtObjW_putDay(dtObj,1); CkDtObjW_putUtc(dtObj,TRUE); CkDateTimeW_SetFromDtObj(dt,dtObj); CkStringBuilderW_Clear(sbExpression); CkStringBuilderW_Append(sbExpression,L"(receivedDateTime ge "); CkStringBuilderW_Append(sbExpression,CkDateTimeW_getAsTimestamp(dt,FALSE)); CkStringBuilderW_Append(sbExpression,L") and (receivedDateTime lt "); CkDtObjW_putMonth(dtObj,11); CkDateTimeW_SetFromDtObj(dt,dtObj); CkStringBuilderW_Append(sbExpression,CkDateTimeW_getAsTimestamp(dt,FALSE)); CkStringBuilderW_Append(sbExpression,L")"); // This is the expression we just built: (receivedDateTime ge 2016-10-01T00:00:00Z) and (receivedDateTime lt 2016-11-01T00:00:00Z) wprintf(L"%s\n",CkStringBuilderW_getAsString(sbExpression)); CkHttpW_SetUrlVar(http,L"filter",CkStringBuilderW_getAsString(sbExpression)); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbExpression); CkDateTimeW_Dispose(dt); CkDtObjW_Dispose(dtObj); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // ----------------------------------------------------------------------------------------------------- // Return unread emails CkHttpW_SetUrlVar(http,L"filter",L"isRead eq false"); // success = http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbExpression); CkDateTimeW_Dispose(dt); CkDtObjW_Dispose(dtObj); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // ----------------------------------------------------------------------------------------------------- // Return emails with a plain-text or HTML body containing the phrase "Outlook 365" CkHttpW_SetUrlVar(http,L"filter",L"contains(body/content,'Outlook 365')"); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbExpression); CkDateTimeW_Dispose(dt); CkDtObjW_Dispose(dtObj); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(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 CkHttpW_SetUrlVar(http,L"search",L"Amazon"); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbExpression); CkDateTimeW_Dispose(dt); CkDtObjW_Dispose(dtObj); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // ----------------------------------------------------------------------------------------------------- // Search for chilkatsoft.com // Some chars, such as the "." make it necessary to enclose the search expression in double-quotes. CkHttpW_SetUrlVar(http,L"search",L"\"chilkatsoft.com\""); success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",sbResponse); if ((success != TRUE) && (CkHttpW_getLastStatus(http) == 0)) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbExpression); CkDateTimeW_Dispose(dt); CkDtObjW_Dispose(dtObj); return; } CkJsonObjectW_LoadSb(json,sbResponse); wprintf(L"%s\n",CkJsonObjectW_emit(json)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbResponse); CkHashtableW_Dispose(htFolderMap); CkStringBuilderW_Dispose(sbMap); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbExpression); CkDateTimeW_Dispose(dt); CkDtObjW_Dispose(dtObj); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.