Madhur Maurya
Madhur Maurya

Reputation: 1070

Unable to update a document in Azure CosmosDB using microsoft.azure.documentdb.core

I am trying to update a document in Azure CosmosDB collection using ReplaceDocumentAsyc method provided by microsoft.azure.documentdb.core package. I get a 200 response when ReplaceDocumentAsyc is called but the document in collection itself is not getting updated. Here is the brief setup (relevent part of code) for reference.

    private DocumentClient Client
    {
        get
        {
            if (_client == null)
            {
                _client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
            }

            return _client;
        }
    }

    Client.ReplaceDocumentAsync(documentSelfLink, doc).ContinueWith(response =>
    {
        return new QueryResponse { Success = response.Result.StatusCode == HttpStatusCode.OK, LastChanged = lastChanged };
    });

The code seems fine to me and I believe it was able to update the collection before. Has anyone else faced similar issue?

Upvotes: 0

Views: 261

Answers (1)

Jay Gong
Jay Gong

Reputation: 23792

Please refer to my working code:

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using System;

namespace JayTestDocumentDB
{
    class Program
    {
        private static DocumentClient client;
        private static string EndpointUrl = "https://***.documents.azure.com:443/";
        private static string AuthorizationKey = "***";
        private static string databaseId = "db";
        private static string collectionId = "coll";

        static void Main(string[] args)
        {
            client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
            var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

            var options = new FeedOptions
            {
                MaxItemCount = 100
            };      
            FeedResponse<Document> doc = client.CreateDocumentQuery<Document>(uri, options).AsDocumentQuery().ExecuteNextAsync<Document>().Result;

            foreach (Document d in doc)
            {
                Console.WriteLine(d);
                d.SetPropertyValue("name","jay");

                client.ReplaceDocumentAsync(d.SelfLink, d).ContinueWith(response =>
                {
                    Console.WriteLine(response.Result.StatusCode);
                    //return new QueryResponse { Success = response.Result.StatusCode == HttpStatusCode.OK, LastChanged = lastChanged };
                });
                break;
            }
            Console.ReadLine();
        }
    }                    
}

My package version of microsoft.azure.documentdb.core is 2.0.0-preview2.

Any concern,please let me know.

Upvotes: 2

Related Questions