DocumentClient.CreateDocumentAsync Method

Definition

Overloads

CreateDocumentAsync(String, Object, RequestOptions, Boolean, CancellationToken)

Creates a Document as an asychronous operation in the Azure Cosmos DB service.

CreateDocumentAsync(Uri, Object, RequestOptions, Boolean, CancellationToken)

Creates a document as an asychronous operation in the Azure Cosmos DB service.

CreateDocumentAsync(String, Object, RequestOptions, Boolean, CancellationToken)

Creates a Document as an asychronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>> CreateDocumentAsync (string documentsFeedOrDatabaseLink, object document, Microsoft.Azure.Documents.Client.RequestOptions options = default, bool disableAutomaticIdGeneration = false, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateDocumentAsync : string * obj * Microsoft.Azure.Documents.Client.RequestOptions * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
override this.CreateDocumentAsync : string * obj * Microsoft.Azure.Documents.Client.RequestOptions * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
Public Function CreateDocumentAsync (documentsFeedOrDatabaseLink As String, document As Object, Optional options As RequestOptions = Nothing, Optional disableAutomaticIdGeneration As Boolean = false, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResourceResponse(Of Document))

Parameters

documentsFeedOrDatabaseLink
String

The link of the DocumentCollection to create the document in. E.g. dbs/db_rid/colls/coll_rid/

document
Object

The document object to create.

options
RequestOptions

(Optional) Any request options you wish to set. E.g. Specifying a Trigger to execute when creating the document. RequestOptions

disableAutomaticIdGeneration
Boolean

(Optional) Disables the automatic id generation, If this is True the system will throw an exception if the id property is missing from the Document.

cancellationToken
CancellationToken

(Optional) A CancellationToken that can be used by other objects or threads to receive notice of cancellation.

Returns

The Document that was created contained within a Task object representing the service response for the asynchronous operation.

Implements

Exceptions

If either documentsFeedOrDatabaseLink or document is not set.

Represents a consolidation of failures that occured during async processing. Look within InnerExceptions to find the actual exception(s)

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the document supplied. It is likely that disableAutomaticIdGeneration was true and an id was not supplied
403Forbidden - This likely means the collection in to which you were trying to create the document is full.
409Conflict - This means a Document with an id matching the id field of document already existed
413RequestEntityTooLarge - This means the Document exceeds the current max entity size. Consult documentation for limits and quotas.
429TooManyRequests - This means you have exceeded the number of request units per second. Consult the DocumentClientException.RetryAfter value to see how long you should wait before retrying this operation.

Examples

Azure Cosmos DB supports a number of different ways to work with documents. A document can extend Resource

public class MyObject : Resource
{
    public string MyProperty {get; set;}
}

using (IDocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
{
    Document doc = await client.CreateDocumentAsync("dbs/db_rid/colls/coll_rid/", new MyObject { MyProperty = "A Value" });
}

A document can be any POCO object that can be serialized to JSON, even if it doesn't extend from Resource

public class MyPOCO
{
    public string MyProperty {get; set;}
}

using (IDocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
{
    Document doc = await client.CreateDocumentAsync("dbs/db_rid/colls/coll_rid/", new MyPOCO { MyProperty = "A Value" });
}

Finally, a Document can also be a dynamic object

using (IDocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
{
    Document doc = await client.CreateDocumentAsync("dbs/db_rid/colls/coll_rid/", new { SomeProperty = "A Value" } );
}

Create a Document and execute a Pre and Post Trigger

using (IDocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key"))
{
    Document doc = await client.CreateDocumentAsync(
        "dbs/db_rid/colls/coll_rid/",
        new { id = "DOC123213443" },
        new RequestOptions
        {
            PreTriggerInclude = new List<string> { "MyPreTrigger" },
            PostTriggerInclude = new List<string> { "MyPostTrigger" }
        });
}

See also

Applies to

CreateDocumentAsync(Uri, Object, RequestOptions, Boolean, CancellationToken)

Creates a document as an asychronous operation in the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>> CreateDocumentAsync (Uri documentCollectionUri, object document, Microsoft.Azure.Documents.Client.RequestOptions options = default, bool disableAutomaticIdGeneration = false, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateDocumentAsync : Uri * obj * Microsoft.Azure.Documents.Client.RequestOptions * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
override this.CreateDocumentAsync : Uri * obj * Microsoft.Azure.Documents.Client.RequestOptions * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
Public Function CreateDocumentAsync (documentCollectionUri As Uri, document As Object, Optional options As RequestOptions = Nothing, Optional disableAutomaticIdGeneration As Boolean = false, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResourceResponse(Of Document))

Parameters

documentCollectionUri
Uri

the URI of the document collection to create the document in.

document
Object

the document object.

options
RequestOptions

The request options for the request.

disableAutomaticIdGeneration
Boolean

Disables the automatic id generation, will throw an exception if id is missing.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

The task object representing the service response for the asynchronous operation.

Implements

Applies to