An Azure service that provides an event-driven serverless compute platform.
@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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
An Azure service that provides an event-driven serverless compute platform.
An Azure NoSQL database service for app development.
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
@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
@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 For update and delete operations, you could just bind to DocumentClient as shown in the docs to use the UpsertDocumentAsync and DeleteDocumentAsync methods, respectively.