Search code examples
c#asp.net-core-webapidocument-database

return id of newly created document in DocumentDB within .NET Core


I am using the following to create a DocumentDB document, how can I ament it to return the guid id of the document that gets created?

            _logger.LogInformation("creating document");
            Uri uri = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
            await client.CreateDocumentAsync(uri, doc);

Solution

  • Simply read id value from the document after saving it:

     await client.CreateDocumentAsync(uri, doc);
     _logger.LogInformation($"created document {doc.Id}");