Forum Discussion
mmcossu
Mar 14, 2024Copper Contributor
Azure Cosmos Db Triggers not working
I have a cosmos db for NoSql instance I'm working on. I have a collection, let's coll it "my-coll". I want to add a property in any document being added to the collection. The https://learn.micros...
Kidd_Ip
Mar 02, 2025MVP
Seems the problem is that Cosmos DB triggers are not automatically invoked when using the Azure Portal or REST API, please considering this:
1. Specify the Trigger in Request Options: When you create or update a document, you need to include the trigger in the request options. Here's an example using the .NET SDK:
dynamic newItem = new { id = "1", name = "Sample Item" };
Uri collectionUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "my-coll");
RequestOptions requestOptions = new RequestOptions { PreTriggerInclude = new List<string> { "createPreTrigger" } };
await client.CreateDocumentAsync(collectionUri, newItem, requestOptions);
2. Using Postman: If you're using Postman to test, you need to include the trigger in the request headers. Add a header named x-ms-documentdb-pre-trigger-include with the value set to your trigger ID (createPreTrigger).
3. JavaScript Example: If you're using JavaScript, you can also specify like this:
const newItem = { id: "1", name: "Sample Item" };
const requestOptions = { preTriggerInclude: ["createPreTrigger"] };
await client.createDocument(collectionUri, newItem, requestOptions);