Sample code for 30+ languages & platforms
C

Outlook List Contacts

See more Outlook Contact Examples

List Outlook Contacts

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkJsonObject jsonToken;
    HCkStringBuilder sbJson;
    int statusCode;
    HCkJsonObject jResp;
    const char *odata_etag;
    const char *id;
    const char *createdDateTime;
    const char *lastModifiedDateTime;
    const char *changeKey;
    const char *parentFolderId;
    const char *birthday;
    const char *fileAs;
    const char *displayName;
    const char *givenName;
    const char *initials;
    const char *middleName;
    const char *nickName;
    const char *surname;
    const char *title;
    const char *yomiGivenName;
    const char *yomiSurname;
    const char *yomiCompanyName;
    const char *generation;
    const char *jobTitle;
    const char *companyName;
    const char *department;
    const char *officeLocation;
    const char *profession;
    const char *businessHomePage;
    const char *assistantName;
    const char *manager;
    const char *mobilePhone;
    const char *spouseName;
    const char *personalNotes;
    int j;
    int count_j;
    const char *strVal;
    const char *name;
    const char *address;
    const char *odata_context;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttp_Create();

    // Use your previously obtained access token here: Get Outlook Contacts OAuth2 Access Token (Azure AD v2.0 Endpoint).

    jsonToken = CkJsonObject_Create();
    success = CkJsonObject_LoadFile(jsonToken,"qa_data/tokens/outlookContacts.json");
    if (success == FALSE) {
        printf("%s\n",CkJsonObject_lastErrorText(jsonToken));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        return;
    }

    CkHttp_putAuthToken(http,CkJsonObject_stringOf(jsonToken,"access_token"));

    // Send the following GET:

    // GET https://graph.microsoft.com/v1.0/me/contacts
    sbJson = CkStringBuilder_Create();
    success = CkHttp_QuickGetSb(http,"https://graph.microsoft.com/v1.0/me/contacts",sbJson);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    statusCode = CkHttp_getLastStatus(http);
    printf("Response status code = %d\n",statusCode);

    if (statusCode != 200) {
        printf("%s\n",CkStringBuilder_getAsString(sbJson));
        printf("Failed.\n");
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(jsonToken);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbJson);
    CkJsonObject_putEmitCompact(jResp,FALSE);
    printf("%s\n",CkJsonObject_emit(jResp));

    // Sample output:

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('4ee732c3-322e-4a6b-b729-2fd1eb5c6004')/contacts",
    //   "value": [
    //     {
    //       "@odata.etag": "W/\"EQAAABYAAABUcG0qWqkmQYqWLHQataQxAAD0iYfT\"",
    //       "id": "AAMkADYzZWE3YmZmLWU0YzgtNGNkZC04MGE1LWFiYTFlNTRlY2QwYQBGAAAAAAAu7cUXL5YOTrdsUIw7-v8FBwBUcG0qWqkmQYqWLHQataQxAAAAAAEOAABUcG0qWqkmQYqWLHQataQxAAD0sxexAAA=",
    //       "createdDateTime": "2021-06-29T16:32:05Z",
    //       "lastModifiedDateTime": "2021-06-29T16:32:06Z",
    //       "changeKey": "EQAAABYAAABUcG0qWqkmQYqWLHQataQxAAD0iYfT",
    //       "categories": [
    //       ],
    //       "parentFolderId": "AAMkADYzZWE3YmZmLWU0YzgtNGNkZC04MGE1LWFiYTFlNTRlY2QwYQAuAAAAAAAu7cUXL5YOTrdsUIw7-v8FAQBUcG0qWqkmQYqWLHQataQxAAAAAAEOAAA=",
    //       "birthday": null,
    //       "fileAs": "",
    //       "displayName": "Pavel Bansky",
    //       "givenName": "Pavel",
    //       "initials": null,
    //       "middleName": null,
    //       "nickName": null,
    //       "surname": "Bansky",
    //       "title": null,
    //       "yomiGivenName": null,
    //       "yomiSurname": null,
    //       "yomiCompanyName": null,
    //       "generation": null,
    //       "imAddresses": [
    //       ],
    //       "jobTitle": null,
    //       "companyName": null,
    //       "department": null,
    //       "officeLocation": null,
    //       "profession": null,
    //       "businessHomePage": null,
    //       "assistantName": null,
    //       "manager": null,
    //       "homePhones": [
    //       ],
    //       "mobilePhone": null,
    //       "businessPhones": [
    //         "+1 732 555 0102"
    //       ],
    //       "spouseName": null,
    //       "personalNotes": "",
    //       "children": [
    //       ],
    //       "emailAddresses": [
    //         {
    //           "name": "Pavel Bansky",
    //           "address": "pavelb@fabrikam.onmicrosoft.com"
    //         }
    //       ],
    //       "homeAddress": {},
    //       "businessAddress": {},
    //       "otherAddress": {}
    //     },
    // ...
    //   ]
    // }

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    odata_context = CkJsonObject_stringOf(jResp,"\"@odata.context\"");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"value");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        odata_etag = CkJsonObject_stringOf(jResp,"value[i].\"@odata.etag\"");
        id = CkJsonObject_stringOf(jResp,"value[i].id");
        createdDateTime = CkJsonObject_stringOf(jResp,"value[i].createdDateTime");
        lastModifiedDateTime = CkJsonObject_stringOf(jResp,"value[i].lastModifiedDateTime");
        changeKey = CkJsonObject_stringOf(jResp,"value[i].changeKey");
        parentFolderId = CkJsonObject_stringOf(jResp,"value[i].parentFolderId");
        birthday = CkJsonObject_stringOf(jResp,"value[i].birthday");
        fileAs = CkJsonObject_stringOf(jResp,"value[i].fileAs");
        displayName = CkJsonObject_stringOf(jResp,"value[i].displayName");
        givenName = CkJsonObject_stringOf(jResp,"value[i].givenName");
        initials = CkJsonObject_stringOf(jResp,"value[i].initials");
        middleName = CkJsonObject_stringOf(jResp,"value[i].middleName");
        nickName = CkJsonObject_stringOf(jResp,"value[i].nickName");
        surname = CkJsonObject_stringOf(jResp,"value[i].surname");
        title = CkJsonObject_stringOf(jResp,"value[i].title");
        yomiGivenName = CkJsonObject_stringOf(jResp,"value[i].yomiGivenName");
        yomiSurname = CkJsonObject_stringOf(jResp,"value[i].yomiSurname");
        yomiCompanyName = CkJsonObject_stringOf(jResp,"value[i].yomiCompanyName");
        generation = CkJsonObject_stringOf(jResp,"value[i].generation");
        jobTitle = CkJsonObject_stringOf(jResp,"value[i].jobTitle");
        companyName = CkJsonObject_stringOf(jResp,"value[i].companyName");
        department = CkJsonObject_stringOf(jResp,"value[i].department");
        officeLocation = CkJsonObject_stringOf(jResp,"value[i].officeLocation");
        profession = CkJsonObject_stringOf(jResp,"value[i].profession");
        businessHomePage = CkJsonObject_stringOf(jResp,"value[i].businessHomePage");
        assistantName = CkJsonObject_stringOf(jResp,"value[i].assistantName");
        manager = CkJsonObject_stringOf(jResp,"value[i].manager");
        mobilePhone = CkJsonObject_stringOf(jResp,"value[i].mobilePhone");
        spouseName = CkJsonObject_stringOf(jResp,"value[i].spouseName");
        personalNotes = CkJsonObject_stringOf(jResp,"value[i].personalNotes");
        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"value[i].categories");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"value[i].imAddresses");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"value[i].homePhones");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"value[i].businessPhones");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            strVal = CkJsonObject_stringOf(jResp,"value[i].businessPhones[j]");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"value[i].children");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"value[i].emailAddresses");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            name = CkJsonObject_stringOf(jResp,"value[i].emailAddresses[j].name");
            address = CkJsonObject_stringOf(jResp,"value[i].emailAddresses[j].address");
            j = j + 1;
        }

        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(jsonToken);
    CkStringBuilder_Dispose(sbJson);
    CkJsonObject_Dispose(jResp);

    }