Getting Started: Using Elastic Tables in Your Plugin Development
- Najm-us-saher Farooque
- Sep 24, 2023
- 1 min read
In our previous blog post Elastic Tables: A New Power Apps Feature, We introduced you to the latest addition in Dynamics 365, known as Elastic Tables. We have covered their creation process and highlighted their limitations. In this upcoming blog, We will guide you on utilizing elastic tables in Plugins and JavaScript for data retrieval. So, without any delay, let's dive right in.

Create Request:
As we've previously discussed, the creation of a new row in elastic tables requires a Partition ID. Let's use the example of the Coupon Table we created in the previous blog. In this case, I will demonstrate how to add a row to the Coupon Table, setting the Partition ID as "Gift Coupon" and configuring the Time to Live field to 86400 seconds, equivalent to 1 day. It's important to note that the Time to Live parameter accepts input in seconds.
To execute a single request, you can directly utilize the 'service.Create' method.
var entity = new Entity("new_coupons");
entity["new_name"] = "Coupon_003";
entity["partitionid"] = "Gift Coupon";
entity["ttlinseconds"] = 86400;
var request = new CreateRequest
{
Target = entity
};
var response = (CreateResponse)service.Execute(request);
return response.id;
Result:
Coupon_003 is created.

Here is how to execute Multiple requests. CreateMultiple will create multiple records with a single web request.
var entities = new EntityCollection();
entities.EntityName = "new_coupons";
int CouponNo = 003;
for (var i = 0; i < 100; i++)
{
CouponNo = CouponNo + 1;
var entity = new Entity("new_coupons")
{
["new_name"] = "Coupon_" + CouponNo,
["partitionid"] = "123",
["ttlinseconds"] = 120
};
entities.Entities.Add(entity);
};
service.Execute(new CreateMultipleRequest
{
Targets = entities
});
You can use `service.Create` request to create multiple records also but it will create individual requests for every row.
int CouponNo = 3;
for (var i = 0; i < 100; i++)
{
CouponNo = CouponNo + 1;
Entity coupon = new Entity("new_coupons");
coupon["new_name"] = "Coupon_" + CouponNo;
coupon["partitionid"] = "Gift Coupon";
coupon["ttlinseconds"] = 86400;
service.Create(coupon);
};
Result:
Here we have used the CreateMultiple Request and added a tracer at the end of the code. You can see Execution time of the Request is 609 seconds.

In this scenario, when using `service.Create`, individual requests are created for each row. Consequently, you can observe that the time taken for these individual requests is significantly longer compared to using the CreateMultiple Request method.

Therefore, it is evident that the CreateMultiple request is considerably faster than the service.Create request, and it can be effectively employed not only in elastic tables but also in standard tables.
Update Request:
This is the procedure for executing an update request on elastic tables. In this instance, we are modifying the Time to Live value of Coupon_6 to 2 days. Please note that the Partition ID cannot be updated. Elastic Tables automatically generate an Alternate key with the key values of Partition ID and the primary key of the table, which is the Guid of the record.
To uniquely identify the record, we utilize the KeyAttributeCollection, which includes both the primary key and Partition Id values, and then proceed to update the Time to Live field to 2 days.
var keys = new KeyAttributeCollection()
{
{ "new_couponsid", couponId },
{ "partitionid", "Gift Coupon" }
};
var entity = new Entity("new_coupons", keys)
{
Attributes = {
{ "ttlinseconds", 172800 }
}
};
var request = new UpdateRequest
{
Target = entity
};
var response = (UpdateResponse)service.Execute(request);
We attempted to update the Partition ID of the record, but despite receiving no error, the Partition ID itself was not successfully updated.
var keys = new KeyAttributeCollection()
{
{ "new_couponsid", couponId },
{ "partitionid", "Gift Coupon" }
};
var entity = new Entity("new_coupons", keys)
{
Attributes = {
{ "partitionid", "Free Shipping Coupon" }
}
};
var request = new UpdateRequest
{
Target = entity
};
var response = (UpdateResponse)service.Execute(request);

You can use UpdateMultiple Request also.
string tableLogicalName = "new_coupons";
List<Entity> entityList = new List<Entity>
{
new Entity(tableLogicalName, couponID1)
{
Attributes =
{
{ "ttlinseconds", 259200 },
{ "partitionid", "Gift Coupon" }
}
}
};
EntityCollection entities = new EntityCollection(entityList)
{
EntityName = tableLogicalName
};
UpdateMultipleRequest updateMultipleRequest = new UpdateMultipleRequest()
{
Targets = entities,
};
service.Execute(updateMultipleRequest);
Upsert Record:
When working with elastic tables, it's important to note that the Upsert operation doesn't trigger the Create or Update messages based on whether the record already exists or not. Instead, Upsert directly applies the changes to the entity. Here's how you can execute an Upsert request.
var entity = new Entity("new_coupons", couponID)
{
Attributes = {
{ "new_name", "Coupon_007" },
{ "ttlinseconds", 172800 }
}
};
var request = new UpsertRequest
{
Target = entity
};
var response = (UpsertResponse)service.Execute(request);
return response.RecordCreated;
In this particular case, it appears that an incorrect Guid was provided for the Coupon, resulting in the Upsert request creating a new record with the identifier "Coupon_007" and a Time to Live of 2 days.

Retrieve Request:
There are two primary methods for retrieving data from Elastic Tables:
Using the Alternate Key: You can retrieve data by specifying the alternate key, which includes both the Partition ID and the primary key of the record.
Simple Retrieve Request: Alternatively, you can perform a basic retrieve request by specifying the Partition ID as an optional parameter. This method allows you to fetch data based on the Partition ID alone.
Using the Alternate Key:
var keys = new KeyAttributeCollection() {
{ "new_couponsid", couponId },
{ "partitionid", partitionId }
};
var entityReference = new EntityReference("new_coupons", keys);
var request = new RetrieveRequest
{
ColumnSet = new ColumnSet("new_name"),
Target = entityReference
};
var response = (RetrieveResponse)service.Execute(request);
tracingService.Trace($"Coupon: {response.Entity.GetAttributeValue<string>("new_name")}");
Simple Retrieve Request:
var entityReference = new EntityReference("new_coupons", couponId);
var request = new RetrieveRequest
{
ColumnSet = new ColumnSet("new_name"),
Target = entityReference,
["partitionId"] = partitionId
};
var response = (RetrieveResponse)service.Execute(request);
Partition ID is important for retrieving requests. If you don't use one then you will get into this error.
Exception Message: The HTTP status code of the response was not expected (404).
Status: 404
Response:
{"error":{"message":"Could not find item '80853426-565a-ee11-be6e-000d3a4aed4a'.","details":[{"message":"\r\nErrors : [\r\n \"Resource Not Found. Learn more: https://aka.ms/cosmosdb-tsg-not-found\"\r\n]\r\n"}]}}
If Partition Id is null then provide Partition Id as null in the cod
Delete Request:
You have two options for deleting rows in Elastic Tables:
Single Row Deletion: You can delete a single row by utilizing the Service.Delete method.
Bulk Deletion: For bulk deletion operations, you can use the Delete Multiple Request method. It's worth noting that Delete Multiple is exclusively available for Elastic Tables and is not applicable to standard tables.
string tableLogicalName = "new_coupons";
List<EntityReference> entityReferences = new List<EntityReference> {
{
new EntityReference(logicalName: tableLogicalName,
keyAttributeCollection: new KeyAttributeCollection
{
{ "new_couponsid", couponID1 },
{ "partitionid", "Gift Coupon" }
})
},
{ new EntityReference(logicalName: tableLogicalName,
keyAttributeCollection: new KeyAttributeCollection
{
{ "new_couponsid", couponID2 },
{ "partitionid", "Gift Coupon" }
})
}
};
OrganizationRequest request = new OrganizationRequest(requestName: "DeleteMultiple")
{
Parameters = {
{"Targets", new EntityReferenceCollection(entityReferences)}
}
};
service.Execute(request);
This wraps up our blog on Elastic Tables. Since it is in preview, there may be additional functionality updates in the future from Microsoft.
Stay tuned for our next blog!
Comments