Container.CreateItemAsync<T>(T, Nullable<PartitionKey>, ItemRequestOptions, CancellationToken) Method

Definition

Creates a item as an asynchronous operation in the Azure Cosmos service.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ItemResponse<T>> CreateItemAsync<T> (T item, Nullable<Microsoft.Azure.Cosmos.PartitionKey> partitionKey = null, Microsoft.Azure.Cosmos.ItemRequestOptions requestOptions = null, System.Threading.CancellationToken cancellationToken = null);
abstract member CreateItemAsync : 'T * Nullable<Microsoft.Azure.Cosmos.PartitionKey> * Microsoft.Azure.Cosmos.ItemRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ItemResponse<'T>>

Type Parameters

T

Parameters

item
T

A JSON serializable object that must contain an id property. CosmosSerializer to implement a custom serializer

partitionKey
Nullable<PartitionKey>

Partition key for the item. If not specified will be populated by extracting from {T}

requestOptions
ItemRequestOptions

(Optional) The options for the item request ItemRequestOptions

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

The ItemResponse<T> that was created contained within a Task object representing the service response for the asynchronous operation.

Exceptions

Represents a consolidation of failures that occurred 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.
403Forbidden - This likely means the collection in to which you were trying to create the document is full.
409Conflict - This means a item with an id matching the id field of item already existed
413RequestEntityTooLarge - This means the item 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.

Examples


   public class ToDoActivity{
       public string id {get; set;}
       public string status {get; set;}
   }
   
   ToDoActivity test = new ToDoActivity()
   {
      id = Guid.NewGuid().ToString(),
      status = "InProgress"
   };
  
   ItemResponse item = this.container.CreateItemAsync<ToDoActivity>(tests, new PartitionKey(test.status));

   public class ToDoActivity{
       public string id {get; set;}
       public string status {get; set;}
   }
   
   ToDoActivity test = new ToDoActivity()
   {
      id = Guid.NewGuid().ToString(),
      status = "InProgress"
   };
  
   ItemResponse item = await this.container.CreateItemAsync<ToDoActivity>(tests, new PartitionKey(test.status));

Applies to