Using the c# .Net client SDK, I am calling the ExecuteStoredProcedureAsync method as follows:
sproc_response = await client.ExecuteStoredProcedureAsync<Document>(sproc_uri, new RequestOptions { PartitionKey = new PartitionKey( my_partition_key) }, doc_to_create );
The stored procedure code is as follows:
sproc.Body = @"function( doc ) {
var collection = getContext().getCollection();
var response = getContext().getResponse();
function create_doc_callback( err, doc_created, options ) {
if(err) throw new Error('Error creating document: ' + err.message);
response.setBody( doc_created );
}
collection.createDocument( collection.getSelfLink(), doc, {}, create_doc_callback );
}";
This ALWAYS results in the following exception being thrown:
Error creating document: Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted.
Is there any documentation or example that clearly shows how to call a stored procedure using a partition key -- obviously, I am missing something!
When you create a partitioned collection in Cosmos you are required to pick a partition key which is a path in the JSON representation of your documents that will be used to place the document in the correct partition. If you're attempting to insert a document with no partition key it's going to get grouped in a special partition for documents with no partition key. You're specifying that the stored procedure should run within the context of a given partition but then trying to use it to insert a document into a different partition (the undefined one). Make sure that your POCO has a property on it that maps to the value you chose for the partition key on your collection.
You clearly know what partition you'd like to place the document in, as you're passing a value in RequestOptions for PartitionKey. Now ensure that your POCO includes this value on a property that matches the partition key you chose when you created your collection. The document will still have an auto generated Id if you do not provide one. Alternatively, you can just add the partition key to your document inside the Sproc as you're dealing with a dynamic JS object.
doc.partitionKey = my_partition_key
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With