Azure Table: Insert an Entity with Typed Properties
See more Azure Table Service Examples
Insert into an existing table using Prefer: return-no-content. Requires HTTP 204, avoids parsing its empty body, and captures the ETag for later conditional writes. Insert is create-only; an existing PartitionKey/RowKey pair produces a conflict.
Includes explicit Edm.Guid, Edm.DateTime, and Edm.Int64 annotations. The Int64 is serialized as a string beyond JavaScript's exact integer range, preserving its digits in every generated language. The timestamp is server-managed. AmountDue demonstrates Edm.Double, which is not exact decimal currency. See Table JSON types.
Uses Chilkat.Rest and AuthAzureStorage with Table-specific Shared Key signing. Load a private qa_data/azure/storage.json containing string fields account and access_key (the base64 account key, not a connection string or SAS). Keep this file outside public web content and source control. Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table.
Pins x-ms-version: 2026-04-06, Microsoft's documented fully deployed Storage version at review time, and sends OData 3.0 headers. Older supported versions are not automatically obsolete. See Storage versioning and OData headers.
Microsoft recommends Microsoft Entra ID authorization, preferably managed identity where available. This example retains its existing Shared Key authentication pattern; use it only when account policy permits. TLS certificates are verified, timeouts are bounded, and diagnostic dumps and entity payloads are not printed. A lost response can leave a write's outcome uncertain: inspect the target before retrying.
Microsoft insert entity reference.
Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkAuthAzureStorageW.h>
#include <CkSocketW.h>
void ChilkatSample(void)
{
bool success = false;
// StringBuilder compares text by value in every generated language.
CkStringBuilderW textCompare;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkJsonObjectW config;
config.put_Strict(true);
success = config.LoadFile(L"qa_data/azure/storage.json");
if (success != true) {
wprintf(L"Could not load private Azure configuration.\n");
return;
}
textCompare.Clear();
textCompare.SetString(config.stringOf(L"account"));
bool sameText1 = textCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"account") != 1) || (sameText1 == true)) {
wprintf(L"Supply the storage account name.\n");
return;
}
// access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear();
textCompare.SetString(config.stringOf(L"access_key"));
bool sameText2 = textCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"access_key") != 1) || (sameText2 == true)) {
wprintf(L"Supply the base64 account access key.\n");
return;
}
CkStringBuilderW accountText;
success = accountText.Append(config.stringOf(L"account"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
wprintf(L"Storage account names need 3-24 lowercase letters or digits.\n");
return;
}
CkStringBuilderW allowedAccountChars;
success = allowedAccountChars.Append(L"abcdefghijklmnopqrstuvwxyz0123456789");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
int charIndex = 0;
while ((charIndex < accountText.get_Length())) {
if (allowedAccountChars.Contains(accountText.getRange(charIndex,1,false),true) != true) {
wprintf(L"Storage account names need lowercase letters or digits.\n");
return;
}
charIndex = charIndex + 1;
}
CkStringBuilderW endpoint;
success = endpoint.Append(config.stringOf(L"account"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = endpoint.Append(L".table.core.windows.net");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
const wchar_t *resourcePath = L"/ExampleCustomers";
CkRestW rest;
rest.put_ConnectTimeoutMs(20000);
rest.put_IdleTimeoutMs(60000);
CkAuthAzureStorageW azAuth;
azAuth.put_Account(config.stringOf(L"account"));
azAuth.put_AccessKey(config.stringOf(L"access_key"));
azAuth.put_Service(L"Table");
azAuth.put_Scheme(L"SharedKey");
azAuth.put_XMsVersion(L"2026-04-06");
success = rest.SetAuthAzureStorage(azAuth);
if (success != true) {
wprintf(L"Could not configure Shared Key signing.\n");
return;
}
CkSocketW connection;
connection.put_RequireSslCertVerify(true);
connection.put_MaxReadIdleMs(60000);
connection.put_MaxSendIdleMs(60000);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
return;
}
// Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
CkStringBuilderW responseBody;
success = rest.AddHeader(L"DataServiceVersion",L"3.0;NetFx");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"MaxDataServiceVersion",L"3.0;NetFx");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"Content-Type",L"application/json");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"Accept",L"application/json;odata=minimalmetadata");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"Prefer",L"return-no-content");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// The table must already exist. Change resourcePath above to your table's name.
// PartitionKey + RowKey uniquely identify the entity. Inserting an existing pair returns 409.
// Adapt these fixed sample values; keys cannot contain /, backslash, #, ?, or control characters.
// Timestamp is server-managed; do not put it in the request.
CkJsonObjectW json;
success = json.UpdateString(L"PartitionKey",L"customers-west");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateString(L"RowKey",L"customer-001");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateString(L"Address",L"Mountain View");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateInt(L"Age",23);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateNumber(L"AmountDue",L"200.23");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// AmountDue is Edm.Double, not exact decimal currency. Use an integer minor-unit design for exact money.
// Quote the whole JSON path so @odata.type is a literal property name.
success = json.UpdateString(L"\"CustomerCode@odata.type\"",L"Edm.Guid");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateString(L"CustomerCode",L"c9da6455-213d-42c9-9a79-3e9149a57833");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// Quote the whole JSON path so @odata.type is a literal property name.
success = json.UpdateString(L"\"CustomerSince@odata.type\"",L"Edm.DateTime");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateString(L"CustomerSince",L"2008-07-10T00:00:00Z");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// Quote the whole JSON path so @odata.type is a literal property name.
success = json.UpdateString(L"\"NumberOfOrders@odata.type\"",L"Edm.Int64");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateString(L"NumberOfOrders",L"9007199254740993");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = json.UpdateBool(L"IsActive",true);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
CkStringBuilderW requestBody;
success = json.EmitSb(requestBody);
if (success != true) {
wprintf(L"Could not serialize the JSON request.\n");
return;
}
success = rest.FullRequestSb(L"POST",resourcePath,requestBody,responseBody);
if (success != true) {
wprintf(L"Request did not complete; check the target before retrying.\n");
return;
}
if (rest.get_ResponseStatusCode() != 204) {
wprintf(L"HTTP status: %d\n",rest.get_ResponseStatusCode());
wprintf(L"Request ID: %s\n",rest.responseHdrByName(L"x-ms-request-id"));
// A conflict is not success. Do not replay or convert this request into an upsert.
if (rest.get_ResponseStatusCode() == 409) {
wprintf(L"The table/entity may already exist, or a table may still be deleting. Inspect its state.\n");
}
// Table errors may be in an OData JSON body instead of x-ms-error-code.
// Keep the body and LastErrorText private; they can contain entity data.
return;
}
// HTTP 204 has no JSON body. Preserve this ETag exactly, including W/ and quotes,
// for a later conditional update or delete with If-Match.
const wchar_t *entityEtag = rest.responseHdrByName(L"ETag");
textCompare.Clear();
textCompare.SetString(entityEtag);
bool sameText3 = textCompare.ContentsEqual(L"",true);
if (sameText3 == true) {
wprintf(L"Entity inserted, but ETag is missing. Retrieve the entity before a conditional write; do not repeat the insert.\n");
return;
}
wprintf(L"Entity inserted.\n");
wprintf(L"ETag: %s\n",entityEtag);
}