Azure Cosmos DB - .NET SDK - container.PatchItemAsync 404 Resource Not Found

Yaswanth Chandra Duggirala 6 Reputation points Microsoft Employee
2022-03-25T14:19:21.997+00:00

Hello,

I'm updating a JSON item in a Container using****container.PatchItemAsync**** but I'm receiving 404 error - Resource Not Found.

And Partition Key is /id and below is the sample code

List<PatchOperation> patchOperations = new List<PatchOperation>();
patchOperations.Add(PatchOperation.Set("/latest", 100));

ItemResponse<dynamic> item = await container.PatchItemAsync<dynamic>(
"30abb6e0-1e8c-xxxx-xxxx-xxxxxxxxxxxx",
new PartitionKey("id"),
patchOperations
);

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,543 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Saurabh Sharma 23,791 Reputation points Microsoft Employee
    2022-03-28T22:30:45.893+00:00

    Hi anonymous user,

    Thanks for using Microsoft Q&A !!
    If my understanding is correct, you are getting error while trying to update a document using container.PatchItemAsync method using CosmosDB .NET SDK.
    I have tried this in my environment and it worked fine using your code.

    List<PatchOperation> patchOperations = new List<PatchOperation>();  
                patchOperations.Add(PatchOperation.Set("/Property1", "Patched"));  
      
                try  
                {  
                    ItemResponse<dynamic> item = await container.PatchItemAsync<dynamic>(  
                    "order1",  
                    new PartitionKey("partitionKey1"),  
                    patchOperations  
                    );  
                }  
                catch (Exception e)  
                {  
                    Console.WriteLine(e);  
      
                }  
    

    Before Patch:
    187712-image.png

    Result:
    187624-image.png

    I believe you are passing the attribute which is being used as a partition key instead of passing PartitionKey for the item. However, can you please make sure that you are passing the correct Partition Key for the item.

    You could further look into this document to troubleshoot any other potential causes of this error.

    Please let me know if you have any questions.

    Thanks
    Saurabh

    1 person found this answer helpful.