@Adhikari, Sourav SBOBNG-ITPT/EM For update and delete operations, you could just bind to DocumentClient as shown in the docs to use the UpsertDocumentAsync and DeleteDocumentAsync methods, respectively.
Azure Functions Update & Delete Http Trigger in C# to update and delete Todo list items in Cosmos DB

Can anyone share the working code to update & delete http trigger azure functions to update and delete to-do list items in Cosmos DB? I could not find any code that works for update & delete. The Read & Write works fine though
3 answers
Sort by: Most helpful
-
-
Adhikari, Sourav SBOBNG-ITPT/EM 1 Reputation point
2021-07-15T05:05:56.66+00:00 @Pramod Valavala I did write the below codes for update and delete respectively using document client. But getting internal server error 500 every time and it seems to be not working. Would you mind sharing, what you have in mind or may be update the below code accordingly?
/Update/
#r "Newtonsoft.Json" #r "Microsoft.Azure.DocumentDB.Core" #r "Microsoft.Azure.WebJobs.Extensions.CosmosDB" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using Microsoft.Azure.Documents.Client; public class Todolist { public string id { get; set; } public string Title { get; set; } public string Description { get; set; } public string Priority { get; set; } public string Status { get; set; } public string Date { get; set; } } public static async Task<IActionResult> Run( HttpRequest req, [CosmosDB(ConnectionStringSetting = "**Connection string**")] DocumentClient client, ILogger log, string Id) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var updated = JsonConvert.DeserializeObject<Todolist>(requestBody); var option = new FeedOptions { EnableCrossPartitionQuery = true }; var collectionUri = UriFactory.CreateDocumentCollectionUri("my-database", "my-container"); var document = client.CreateDocumentQuery(collectionUri, option).Where(t => t.Id == Id) .AsEnumerable().FirstOrDefault(); if (document == null) { return new NotFoundResult(); } document.SetPropertyValue("id", updated.id); document.SetPropertyValue("Title", updated.Title); document.SetPropertyValue("Description", updated.Description); document.SetPropertyValue("Priority", updated.Priority); document.SetPropertyValue("Status", updated.Status); document.SetPropertyValue("Date", updated.Date); await client.ReplaceDocumentAsync(document); return new OkResult(); } }
/Delete/
#r "Newtonsoft.Json" #r "Microsoft.Azure.DocumentDB.Core" #r "Microsoft.Azure.WebJobs.Extensions.CosmosDB" using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json; using Microsoft.Azure.Documents.Client; public static async Task<IActionResult> Run( HttpRequest req, [CosmosDB(ConnectionStringSetting = "**Connection string**")] DocumentClient client, ILogger log, string Id) { var option = new FeedOptions { EnableCrossPartitionQuery = true }; var collectionUri = UriFactory.CreateDocumentCollectionUri("my-database", "my-container"); var document = client.CreateDocumentQuery(collectionUri, option).Where(t => t.id == Id) .AsEnumerable().FirstOrDefault(); if (document == null) { return new NotFoundResult(); } await client.DeleteDocumentAsync(document.SelfLink, new Requestoptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey(id) }); return new OkResult();
-
Adhikari, Sourav SBOBNG-ITPT/EM 1 Reputation point
2021-07-20T08:57:29.963+00:00 @Pramod Valavala Exactly so..its something like end2endtesting02-cosmos_DOCUMENTDB.. I am using this for Insert_data and get_data as well..Both of which works fine