Azure Cosmos DB: .NET examples for the SQL API
The Azure Cosmos DB DocumentDB API or SQL (DocumentDB) API is now known as Azure Cosmos DB SQL API. You don't need to change anything to continue running your apps built with DocumentDB API. The functionality remains the same.
Latest sample solutions that perform CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-documentdb-dotnet GitHub repository. This article provides:
- Links to the tasks in each of the example C# project files.
- Links to the related API reference content.
If you don't have an Azure subscription, create a free account before you begin.
- You can activate Visual Studio subscriber benefits: Your Visual Studio subscription gives you credits every month that you can use for paid Azure services.
Alternatively, you can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments. Or you can use the Azure Cosmos DB Emulator for this tutorial with a URI of https://localhost:8081 and a key of
You also need the Microsoft.Azure.DocumentDB NuGet package.
Each sample is self-contained, it sets itself up and cleans up after itself. As such, the samples issue multiple calls to CreateDocumentCollectionAsync(). Each time this is done your subscription is billed for 1 hour of usage per the performance tier of the collection being created.
The RunDatabaseDemo method of the sample of the DatabaseManagement project shows how to perform the following tasks.
The RunCollectionDemo method of the sample CollectionManagement project shows how to do the following tasks.
The RunDocumentsDemo method of the sample DocumentManagement project shows how to do the following tasks.
The RunIndexDemo method of the sample IndexManagement project shows how to perform the following tasks.
For more information about indexing, see Azure Cosmos DB indexing policies.
The geospatial sample file, azure-documentdb-dotnet/samples/code-samples/Geospatial/Program.cs, shows how to do the following tasks.
For more information about working with Geospatial data, see Working with Geospatial data in Azure Cosmos DB.
The query document file, azure-documentdb-dotnet/samples/code-samples/Queries/Program.cs, shows how to do each of the following tasks using the SQL query grammar, the LINQ provider with query, and with Lambda.
For more information about writing queries, see SQL query within Azure Cosmos DB.
Change feed examples
The change feed sample, azure-documentdb-dotnet/samples/code-samples/ChangeFeed/Program.cs shows how to do the following tasks.
|Read change feed||DocumentClient.CreateDocumentChangeFeedQuery|
|Read partition key ranges||DocumentClient.ReadPartitionKeyRangeFeedAsync|
The change feed processor sample: Change Feed Migration Tool demonstrates how to use change feed processor library to replicate data to another Cosmos DB collection.
Server-side programming examples
The server-side programming file, azure-documentdb-dotnet/samples/code-samples/ServerSideScripts/Program.cs, shows how to do the following tasks.
For more information about server-side programming, see Azure Cosmos DB server-side programming: Stored procedures, database triggers, and UDFs.
User management examples
The user management file, azure-documentdb-dotnet/samples/code-samples/UserManagement/Program.cs, shows how to do the following tasks.
|Create a user||DocumentClient.CreateUserAsync|
|Set permissions on a collection or document||DocumentClient.CreatePermissionAsync|
|Get a list of a user's permissions||DocumentClient.ReadUserAsync