Azure Functions Update & Delete Http Trigger in C# to update and delete Todo list items in Cosmos DB

Adhikari, Sourav SBOBNG-ITPT/EM 1 Reputation point
2021-07-14T10:38:36.927+00:00

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

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,908 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,901 questions
Developer technologies C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Pramod Valavala 20,656 Reputation points Microsoft Employee Moderator
    2021-07-15T04:55:26.95+00:00

    @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.

    0 comments No comments

  2. 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


  3. 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();  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.