Share via


DocumentClient.ReplaceDocumentAsync Method

Definition

Overloads

ReplaceDocumentAsync(Document, RequestOptions, CancellationToken)

Replaces a Document in the Azure Cosmos DB service as an asynchronous operation.

ReplaceDocumentAsync(String, Object, RequestOptions, CancellationToken)

Replaces a Document in the Azure Cosmos DB service as an asynchronous operation.

ReplaceDocumentAsync(Uri, Object, RequestOptions, CancellationToken)

Replaces a document as an asynchronous operation in the Azure Cosmos DB service.

ReplaceDocumentAsync(Document, RequestOptions, CancellationToken)

Replaces a Document in the Azure Cosmos DB service as an asynchronous operation.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>> ReplaceDocumentAsync (Microsoft.Azure.Documents.Document document, Microsoft.Azure.Documents.Client.RequestOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReplaceDocumentAsync : Microsoft.Azure.Documents.Document * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
override this.ReplaceDocumentAsync : Microsoft.Azure.Documents.Document * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
Public Function ReplaceDocumentAsync (document As Document, Optional options As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResourceResponse(Of Document))

Parameters

document
Document

The updated Document to replace the existing resource with.

options
RequestOptions

(Optional) The request options for the request.

cancellationToken
CancellationToken

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

Returns

A System.Threading.Tasks containing a ResourceResponse<TResource> which wraps a Document containing the updated resource record.

Implements

Exceptions

If document is not set.

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
404NotFound - This means the resource you tried to delete did not exist.

Examples

This example uses Document and takes advantage of the fact that it is a dynamic object and uses SetProperty to dynamically update properties on the document

//Fetch the Document to be updated
Document doc = client.CreateDocumentQuery<Document>(collectionLink)
                            .Where(r => r.Id == "doc id")
                            .AsEnumerable()
                            .SingleOrDefault();

//Update some properties on the found resource
doc.SetPropertyValue("MyProperty", "updated value");

//Now persist these changes to the database by replacing the original resource
Document updated = await client.ReplaceDocumentAsync(doc);

See also

Applies to

ReplaceDocumentAsync(String, Object, RequestOptions, CancellationToken)

Replaces a Document in the Azure Cosmos DB service as an asynchronous operation.

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

Parameters

documentLink
String

The link of the document to be updated. E.g. dbs/db_rid/colls/col_rid/docs/doc_rid/

document
Object

The updated Document to replace the existing resource with.

options
RequestOptions

(Optional) The request options for the request.

cancellationToken
CancellationToken

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

Returns

A System.Threading.Tasks containing a ResourceResponse<TResource> which wraps a Document containing the updated resource record.

Implements

Exceptions

If either documentLink or document is not set.

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
404NotFound - This means the resource you tried to delete did not exist.

Examples

In this example, instead of using a strongly typed Document, we will work with our own POCO object and not rely on the dynamic nature of the Document class.

public class MyPoco
{
    public string Id {get; set;}
    public string MyProperty {get; set;}
}

//Get the doc back as a Document so you have access to doc.SelfLink
Document doc = client.CreateDocumentQuery<Document>(collectionLink)
                       .Where(r => r.Id == "doc id")
                       .AsEnumerable()
                       .SingleOrDefault();

//Now dynamically cast doc back to your MyPoco
MyPoco poco = (dynamic)doc;

//Update some properties of the poco object
poco.MyProperty = "updated value";

//Now persist these changes to the database using doc.SelLink and the update poco object
Document updated = await client.ReplaceDocumentAsync(doc.SelfLink, poco);

See also

Applies to

ReplaceDocumentAsync(Uri, Object, RequestOptions, CancellationToken)

Replaces a document as an asynchronous operation in the Azure Cosmos DB service.

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

Parameters

documentUri
Uri

the URI of the document to be updated.

document
Object

the updated document.

options
RequestOptions

The request options for the request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

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

Implements

Applies to