Container.PatchItemAsync<T> Method

Definition

Patches an item in the Azure Cosmos service as an asynchronous operation.

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

Type Parameters

T

Parameters

id
String

The Cosmos item id of the item to be patched.

partitionKey
PartitionKey

PartitionKey for the item

patchOperations
IReadOnlyList<PatchOperation>

Represents a list of operations to be sequentially applied to the referred Cosmos item.

requestOptions
PatchItemRequestOptions

(Optional) The options for the item request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

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

Examples

public class ToDoActivity{
    public string id {get; set;}
    public string status {get; set;}
    public string description {get; set;}
    public int frequency {get; set;}
}

ToDoActivity toDoActivity = await this.container.ReadItemAsync<ToDoActivity>("id", new PartitionKey("partitionKey"));
/* toDoActivity = {
    "id" : "someId",
    "status" : "someStatusPK",
    "description" : "someDescription",
    "frequency" : 7
}*/

List<PatchOperation> patchOperations = new List<PatchOperation>()
{
    PatchOperation.Add("/daysOfWeek", new string[]{"Monday", "Thursday"}),
    PatchOperation.Replace("/frequency", 2),
    PatchOperation.Remove("/description")
};

ItemResponse<dynamic> item = await this.container.PatchItemAsync<dynamic>(toDoActivity.id, new PartitionKey(toDoActivity.status), patchOperations);
/* item.Resource = {
    "id" : "someId",
    "status" : "someStatusPK",
    "description" : null,
    "frequency" : 2,
    "daysOfWeek" : ["Monday", "Thursday"]
}*/

Remarks

The item's partition key value is immutable. To change an item's partition key value you must delete the original item and insert a new item. The patch operations are atomic and are executed sequentially. By default, resource body will be returned as part of the response. User can request no content by setting EnableContentResponseOnWrite flag to false. Note that, this API does not support the usage of IfMatchEtag property at the moment.

Applies to