I am trying to delete an item from a container of Cosmos DB database from azure function. But I am getting this error PartitionKey value must be supplied for this operation. I also investigated this but didn't get any luck from there.
Below is my code -
public class Item{
public Item(){}
public string id {get; set;}
}
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
DocumentClient client = new DocumentClient(new Uri("https://***.documents.azure.com:443/"), Environment.GetEnvironmentVariable("key"));
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var updated = JsonConvert.DeserializeObject<Item>(requestBody);
var option = new FeedOptions { EnableCrossPartitionQuery = true };
var collectionUri = UriFactory.CreateDocumentCollectionUri("Test_DB", "Test_Table");
var document = client.CreateDocumentQuery(collectionUri, option).Where(t => t.Id == updated.id)
.AsEnumerable().FirstOrDefault();
log.LogInformation("Total Incentive : " + document.GetPropertyValue<string>("id"));
if (document == null)
{
return new NotFoundResult();
}
log.LogInformation("Total Incentive : " + document.SelfLink);
log.LogInformation("Total Incentive : " + document.Id);
await client.DeleteDocumentAsync(document.SelfLink);
//await client.ReplaceDocumentAsync(document);
return new OkObjectResult("Deleted");
}
After googling I realized that I need to provide a partition key value as the second parameter in DeleteDocumentAsync() like below. But I am in a fix how to provide that partition key value to that function.
await client.DeleteDocumentAsync(document.SelfLink,
new Requestoptions
{
PartitionKey = new Microsoft.Azure.Documents.PartitionKey("33333")
}
)
DB Name: Test_DB
Table Name: Test_Table
Partition Key: /testCategory
An example element of my Test_Table which I am trying to delete is:
{
"id": "3f614t4e-q85f-4357-8393-a0b3542db4e1",
"Email": "testuser@test.com",
"Name": "Test User",
"_rid": "XNMBANxVc8oIAAAAAAAAAA==",
"_self": "dbs/XNMOXA==/colls/TNMNANyVc9o=/docs/XNMBANxVc8oIZZAAAAAAPP==/",
"_etag": "\"450031d4-0000-0300-0000-60cb36470000\"",
"_attachments": "attachments/",
"_ts": 1623930439
}
Can anyone help me how to put that partition key in the azure function code? Or how to adjust the table of the database?
Thanks In Advance.