Share via


Container.ReadItemAsync<T> Method

Definition

Reads a item from the Azure Cosmos service as an asynchronous operation.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ItemResponse<T>> ReadItemAsync<T> (string id, Microsoft.Azure.Cosmos.PartitionKey partitionKey, Microsoft.Azure.Cosmos.ItemRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadItemAsync : string * Microsoft.Azure.Cosmos.PartitionKey * Microsoft.Azure.Cosmos.ItemRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ItemResponse<'T>>
Public MustOverride Function ReadItemAsync(Of T) (id As String, partitionKey As PartitionKey, Optional requestOptions As ItemRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ItemResponse(Of T))

Type Parameters

T

Parameters

id
String

The Cosmos item id

partitionKey
PartitionKey

The partition key for the item.

requestOptions
ItemRequestOptions

(Optional) The options for the item request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

A Task containing a ItemResponse<T> which wraps the read resource record.

Examples

public class ToDoActivity{
    public string id {get; set;}
    public string status {get; set;}
}

Example 1: Reading Item with Full Response

This example demonstrates how to read an item from the container and retrieve the full
response, including metadata such as request units (RU) consumed, along with the
`ToDoActivity` object.

ItemResponse<ToDoActivity> toDoActivity = await this.container.ReadItemAsync<ToDoActivity>("id", new PartitionKey("partitionKey"));

Example 2: Reading Item with Implicit Casting

This example shows how to read an item from the container and implicitly cast the
response directly to a `ToDoActivity` object, omitting the metadata in the `ItemResponse`.

ToDoActivity toDoActivity = await this.container.ReadItemAsync<ToDoActivity>("id", new PartitionKey("partitionKey"));

Remarks

Items contain meta data that can be obtained by mapping these meta data attributes to properties in T. * "_ts": Gets the last modified time stamp associated with the item from the Azure Cosmos DB service. * "_etag": Gets the entity tag associated with the item from the Azure Cosmos DB service. * "ttl": Gets the time to live in seconds of the item in the Azure Cosmos DB service. Note that, this API does not support the usage of IfMatchEtag property at the moment.

Applies to