Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.5.0+

Box: Query Documents by Customer Metadata

See more Box Examples

Run the reusable setup once per HttpCurl object, then select names and execute the operation. The setup is repeated here only to make this published example independently runnable. Query the projectDocument enterprise template using a parameterized customer ID and marker pagination. Requires the template and access to matching content; metadata queries are distinct from full-text search. The enterprise scope ID is resolved from the current user profile; enterprise membership and the pre-existing template are required.

See the Box API guide for setup and the complete workflow.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpCurlW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
    //  See Global Unlock Sample.
    //  Guide: Box API Guide.

    success = false;

    //  BEGIN ONE-TIME SETUP
    //  This block is repeated so each published example can run on its own.
    //  In an application, run the setup example once and pass the SAME curl object
    //  to operation code below. Do not recreate it for every operation.
    //  See Reusable Box HttpCurl Setup.
    CkHttpCurlW curl;
    curl.SetVar(L"access_token",L"BOX_ACCESS_TOKEN");
    curl.SetVar(L"parent_name",L"/");
    curl.SetVar(L"folder_name",L"images");
    curl.SetVar(L"file_name",L"penguins.jpg");
    curl.SetVar(L"destination_name",L"archive");
    //  Box's fixed root ID is an implementation detail; callers supply names.
    curl.SetVar(L"parent_id",L"0");
    CkStringBuilderW setupCommand;
    int setupReplacements;

    //  Register findParent; registration sends no request.
    setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
    setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
    success = curl.AddFunction(L"findParent",setupCommand.getAsString());
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    //  Register findFolder; registration sends no request.
    setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/{{parent_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
    setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
    success = curl.AddFunction(L"findFolder",setupCommand.getAsString());
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    //  Register findFile; registration sends no request.
    setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/{{folder_id}}/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
    setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
    success = curl.AddFunction(L"findFile",setupCommand.getAsString());
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    //  Register findDestination; registration sends no request.
    setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/folders/0/items?limit=1000&fields=id,type,name\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
    setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
    success = curl.AddFunction(L"findDestination",setupCommand.getAsString());
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    //  Register getFileDetails; registration sends no request.
    setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/files/{{file_id}}?fields=id,sha1,file_version\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
    setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
    success = curl.AddFunction(L"getFileDetails",setupCommand.getAsString());
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    //  Register getEnterprise; registration sends no request.
    setupCommand.SetString(L"curl -X GET \"https://api.box.com/2.0/users/me?fields=enterprise\" -H \"Authorization: Bearer ACCESS_TOKEN\"");
    setupReplacements = setupCommand.Replace(L"ACCESS_TOKEN",curl.getVar(L"access_token"));
    success = curl.AddFunction(L"getEnterprise",setupCommand.getAsString());
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    //  Initial name selections. AddOutput2 stores these WHERE values literally.
    success = curl.AddOutput2(L"findParent",L"entries",L"name",curl.getVar(L"parent_name"),true,L"id",L"parent_id");
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    success = curl.AddOutput2(L"findFolder",L"entries",L"name",curl.getVar(L"folder_name"),true,L"id",L"folder_id");
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    success = curl.AddOutput2(L"findFile",L"entries",L"name",curl.getVar(L"file_name"),true,L"id",L"file_id");
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    success = curl.AddOutput2(L"findDestination",L"entries",L"name",curl.getVar(L"destination_name"),true,L"id",L"destination_id");
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    success = curl.AddOutput(L"getFileDetails",L"file_version.id",L"file_version_id");
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    success = curl.AddOutput(L"getFileDetails",L"sha1",L"file_sha1");
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    success = curl.AddOutput(L"getEnterprise",L"enterprise.id",L"enterprise_id");
    if (success != true) {
        wprintf(L"%s\n",curl.lastErrorText());
        return;
    }

    //  END ONE-TIME SETUP
    //  The name lookups below assume each listing fits in one page (1000 items).
    //  Parent and destination names select root children; folder_name selects a child
    //  of parent_name. "/" selects the root parent. These are not arbitrary paths.

    //  BEGIN OPERATION -- keep the configured object when reusing it.
    //  Inputs are names. Retain cached IDs when the selected names have not changed.
    const wchar_t *parentName = L"/";
    const wchar_t *folderName = L"documents";
    if ((curl.getVar(L"parent_name") != parentName) || ((parentName == L"/") && (curl.VarDefined(L"parent_id") == false))) {
        curl.SetVar(L"parent_name",parentName);
        curl.ClearVar(L"parent_id");
        curl.ClearVar(L"folder_id");
        curl.ClearVar(L"file_id");
        curl.ClearVar(L"file_version_id");
        curl.ClearVar(L"file_sha1");
        if (parentName == L"/") {
            curl.SetVar(L"parent_id",L"0");
        }
        else {
            success = curl.AddOutput2(L"findParent",L"entries",L"name",curl.getVar(L"parent_name"),true,L"id",L"parent_id");
            if (success != true) {
                wprintf(L"%s\n",curl.lastErrorText());
                return;
            }

        }

    }

    if (curl.getVar(L"folder_name") != folderName) {
        curl.SetVar(L"folder_name",folderName);
        curl.ClearVar(L"folder_id");
        curl.ClearVar(L"file_id");
        curl.ClearVar(L"file_version_id");
        curl.ClearVar(L"file_sha1");
        success = curl.AddOutput2(L"findFolder",L"entries",L"name",curl.getVar(L"folder_name"),true,L"id",L"folder_id");
        if (success != true) {
            wprintf(L"%s\n",curl.lastErrorText());
            return;
        }

    }

    curl.SetVar(L"customer_id",L"CUST-1001");
    curl.SetVar(L"marker",L"");
    CkJsonObjectW result;
    const wchar_t *marker = 0;
    bool more = true;
    CkStringBuilderW authorizedCommand1;
    authorizedCommand1.SetString(L"curl -X POST \"https://api.box.com/2.0/metadata_queries/execute\" -H \"Authorization: Bearer {{access_token}}\" -H \"Content-Type: application/json\" -d '{\"from\":\"enterprise_{{enterprise_id}}.projectDocument\",\"ancestor_folder_id\":\"{{folder_id}}\",\"query\":\"customerId = :customer\",\"query_params\":{\"customer\":\"{{customer_id}}\"},\"limit\":100}'");
    int headerReplacement1 = authorizedCommand1.Replace(L"{{access_token}}",curl.getVar(L"access_token"));
    const wchar_t *pageCommand = authorizedCommand1.getAsString();
    while (more == true) {
        curl.ClearTargetOutput(L"*");
        success = curl.DoYourThing(pageCommand);
        if ((success != true) || (curl.get_StatusCode() != 200)) {
            wprintf(L"Box request failed. HTTP status: %d\n",curl.get_StatusCode());
            wprintf(L"Failure reason: %d\n",curl.get_FailReason());
            wprintf(L"%s\n",curl.responseBodyStr());
            return;
        }

        success = curl.GetResponseJson(result);
        if (success != true) {
            wprintf(L"%s\n",curl.lastErrorText());
            return;
        }

        wprintf(L"%s\n",result.emit());
        more = false;
        if (result.HasMember(L"next_marker") == true) {
            if (result.IsNullOf(L"next_marker") == false) {
                marker = result.stringOf(L"next_marker");
                if (marker != L"") {
                    if (marker == curl.getVar(L"marker")) {
                        wprintf(L"Repeated marker; stopping.\n");
                        return;
                    }

                    curl.SetVar(L"marker",marker);
                    CkStringBuilderW authorizedCommand2;
                    authorizedCommand2.SetString(L"curl -X POST \"https://api.box.com/2.0/metadata_queries/execute\" -H \"Authorization: Bearer {{access_token}}\" -H \"Content-Type: application/json\" -d '{\"from\":\"enterprise_{{enterprise_id}}.projectDocument\",\"ancestor_folder_id\":\"{{folder_id}}\",\"query\":\"customerId = :customer\",\"query_params\":{\"customer\":\"{{customer_id}}\"},\"limit\":100,\"marker\":\"{{marker}}\"}'");
                    int headerReplacement2 = authorizedCommand2.Replace(L"{{access_token}}",curl.getVar(L"access_token"));
                    pageCommand = authorizedCommand2.getAsString();
                    more = true;
                }

            }

        }

    }
    }