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

Azure Table: Conditionally Replace an Entity

See more Azure Table Service Examples

Disabled by default: review the target and set applyChange=true. Reads the current entity and retains its exact ETag, then sends If-Match. A concurrent change yields 412; reread and reconcile instead of using a wildcard.

PUT replaces the entity: omitted custom properties are removed. The sample intentionally retains only Address and IsActive alongside the keys. The expected write status is 204. No automatic replay follows a lost response.

Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.

HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.

Microsoft reference.

Chilkat C++ Downloads

C++
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
#include <CkRest.h>
#include <CkAuthAzureStorage.h>
#include <CkSocket.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  StringBuilder compares text by value in every generated language.
    CkStringBuilder textCompare;
    bool applyChange = false;
    if (applyChange != true) {
        std::cout << "This example changes entity data. Review the target and deliberately enable applyChange." << "\r\n";
        return;
    }

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false;
    CkJsonObject config;
    config.put_Strict(true);
    success = config.LoadFile("qa_data/azure/storage.json");
    if (success != true) {
        std::cout << "Could not load private Azure configuration." << "\r\n";
        return;
    }

    textCompare.Clear();
    textCompare.SetString(config.stringOf("account"));
    bool sameText1 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
        std::cout << "Supply the storage account name." << "\r\n";
        return;
    }

    //  access_key is the base64 account key, not a connection string or SAS.
    textCompare.Clear();
    textCompare.SetString(config.stringOf("access_key"));
    bool sameText2 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("access_key") != 1) || (sameText2 == true)) {
        std::cout << "Supply the base64 account access key." << "\r\n";
        return;
    }

    CkStringBuilder accountText;
    success = accountText.Append(config.stringOf("account"));
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
        std::cout << "Storage account names need 3-24 lowercase letters or digits." << "\r\n";
        return;
    }

    CkStringBuilder allowedAccountChars;
    success = allowedAccountChars.Append("abcdefghijklmnopqrstuvwxyz0123456789");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    int charIndex = 0;
    while ((charIndex < accountText.get_Length())) {
        if (allowedAccountChars.Contains(accountText.getRange(charIndex,1,false),true) != true) {
            std::cout << "Storage account names need lowercase letters or digits." << "\r\n";
            return;
        }

        charIndex = charIndex + 1;
    }

    CkStringBuilder endpoint;
    success = endpoint.Append(config.stringOf("account"));
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = endpoint.Append(".table.core.windows.net");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    //  Table names contain only letters and digits and are case-insensitive.
    //  Entity PartitionKey and RowKey values are case-sensitive.
    const char *resourcePath = "/ExampleCustomers()";
    CkRest rest;
    rest.put_ConnectTimeoutMs(20000);
    rest.put_IdleTimeoutMs(60000);
    CkAuthAzureStorage azAuth;
    azAuth.put_Account(config.stringOf("account"));
    azAuth.put_AccessKey(config.stringOf("access_key"));
    azAuth.put_Service("Table");
    azAuth.put_Scheme("SharedKey");
    azAuth.put_XMsVersion("2026-04-06");
    success = rest.SetAuthAzureStorage(azAuth);
    if (success != true) {
        std::cout << "Could not configure Shared Key signing." << "\r\n";
        return;
    }

    CkSocket connection;
    connection.put_RequireSslCertVerify(true);
    connection.put_MaxReadIdleMs(60000);
    connection.put_MaxSendIdleMs(60000);
    success = connection.Connect(endpoint.getAsString(),443,true,20000);
    if (success != true) {
        std::cout << "TLS connection failed; inspect diagnostics privately." << "\r\n";
        return;
    }

    //  Do not replay a write automatically after losing its response.
    success = rest.UseConnection(connection,false);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    const char *responseText = 0;
    CkJsonObject result;
    result.put_Strict(true);
    success = rest.AddHeader("DataServiceVersion","3.0;NetFx");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = rest.AddHeader("MaxDataServiceVersion","3.0;NetFx");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = rest.AddHeader("Content-Type","application/json");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = rest.AddHeader("Accept","application/json;odata=minimalmetadata");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    const char *partitionKey = "customers-west";
    const char *rowKey = "customer-001";
    CkStringBuilder encodedPk;
    success = encodedPk.Append(partitionKey);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    int escapedencodedPk = encodedPk.Replace("'","''");
    success = encodedPk.Encode("url_rfc3986","utf-8");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    CkStringBuilder encodedRk;
    success = encodedRk.Append(rowKey);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    int escapedencodedRk = encodedRk.Replace("'","''");
    success = encodedRk.Encode("url_rfc3986","utf-8");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    //  Escape apostrophes for OData first, then percent-encode each key once.
    CkStringBuilder entityPath;
    success = entityPath.Append("/ExampleCustomers(PartitionKey='");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = entityPath.Append(encodedPk.getAsString());
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = entityPath.Append("',RowKey='");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = entityPath.Append(encodedRk.getAsString());
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = entityPath.Append("')");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    resourcePath = entityPath.getAsString();
    responseText = rest.fullRequestNoBody("GET",resourcePath);
    if (rest.get_LastMethodSuccess() != true) {
        std::cout << "Request did not complete; inspect the target before retrying a write." << "\r\n";
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        std::cout << "HTTP status: " << rest.get_ResponseStatusCode() << "\r\n";
        std::cout << "Request ID: " << rest.responseHdrByName("x-ms-request-id") << "\r\n";
        //  404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
        //  Keep response bodies private. Reconcile uncertain writes before retrying.
        return;
    }

    success = result.Load(responseText);
    if (success != true) {
        std::cout << "Expected well-formed JSON." << "\r\n";
        return;
    }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("PartitionKey"));
    bool sameText3 = textCompare.ContentsEqual("",true);
    if ((result.JsonTypeOf("PartitionKey") != 1) || (sameText3 == true)) {
        std::cout << "Missing string PartitionKey." << "\r\n";
        return;
    }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("RowKey"));
    bool sameText4 = textCompare.ContentsEqual("",true);
    if ((result.JsonTypeOf("RowKey") != 1) || (sameText4 == true)) {
        std::cout << "Missing string RowKey." << "\r\n";
        return;
    }

    textCompare.Clear();
    textCompare.SetString(result.stringOf("PartitionKey"));
    bool sameText5 = textCompare.ContentsEqual(partitionKey,true);
    textCompare.Clear();
    textCompare.SetString(result.stringOf("RowKey"));
    bool sameText6 = textCompare.ContentsEqual(rowKey,true);
    if ((sameText5 != true) || (sameText6 != true)) {
        std::cout << "Returned entity keys do not match." << "\r\n";
        return;
    }

    const char *entityEtag = rest.responseHdrByName("ETag");
    textCompare.Clear();
    textCompare.SetString(entityEtag);
    bool sameText7 = textCompare.ContentsEqual("",true);
    textCompare.Clear();
    textCompare.SetString(entityEtag);
    bool sameText8 = textCompare.ContentsEqual("*",true);
    if ((sameText7 == true) || (sameText8 == true)) {
        std::cout << "Missing exact entity ETag." << "\r\n";
        return;
    }

    rest.Disconnect(10);
    success = connection.Connect(endpoint.getAsString(),443,true,20000);
    if (success != true) {
        std::cout << "TLS reconnection failed." << "\r\n";
        return;
    }

    success = rest.UseConnection(connection,false);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = rest.AddHeader("If-Match",entityEtag);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    CkJsonObject body;
    success = body.UpdateString("PartitionKey",partitionKey);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = body.UpdateString("RowKey",rowKey);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = body.UpdateString("Address","Updated address");
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    success = body.UpdateBool("IsActive",true);
    if (success != true) {
        std::cout << "Could not prepare the request." << "\r\n";
        return;
    }

    responseText = rest.fullRequestString("PUT",resourcePath,body.emit());
    if (rest.get_LastMethodSuccess() != true) {
        std::cout << "Request did not complete; inspect the target before retrying a write." << "\r\n";
        return;
    }

    if (rest.get_ResponseStatusCode() != 204) {
        std::cout << "HTTP status: " << rest.get_ResponseStatusCode() << "\r\n";
        std::cout << "Request ID: " << rest.responseHdrByName("x-ms-request-id") << "\r\n";
        //  404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
        //  Keep response bodies private. Reconcile uncertain writes before retrying.
        return;
    }

    const char *updatedEtag = rest.responseHdrByName("ETag");
    textCompare.Clear();
    textCompare.SetString(updatedEtag);
    bool sameText9 = textCompare.ContentsEqual("",true);
    if (sameText9 == true) {
        std::cout << "Write succeeded but ETag is missing. Retrieve it before another conditional write." << "\r\n";
        return;
    }

    std::cout << "Updated ETag: " << updatedEtag << "\r\n";
    std::cout << "Example completed." << "\r\n";
    }