Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon

Using Azure Cosmos DB with C#

In this lab, you will gain experience using C# and Visual Studio to connect to and use Cosmos DB. You will RDP into a Windows VM set up with Visual Studio and update a pre-built solution with the appropriate Cosmos DB C# code to connect to Cosmos DB, upload documents, and use the SQL API to query documents. When this is complete, you will run a console application to see everything working and can go back to the Azure portal to verify the documents were saved. Upon completion of the lab, you will have gained the experience required to work with Cosmos DB using C#.

Azure icon

Path Info

Clock icon Intermediate
Clock icon 1h 0m
Clock icon Jan 28, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    RDP into the VM and open the Visual Studio solution.

    RDP login:

    See credentials in the credentials section of the lab window.

    PowerShell to run:

    $url = ""
    $zipfile = "C:\Users\cloud_user\Desktop\Azure-LearningActivity-Cosmos"
    $folder = "C:\Users\cloud_user\Desktop"
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest -UseBasicParsing -OutFile $zipfile $url
    Expand-Archive -LiteralPath $zipfile -DestinationPath $folder
    Remove-Item -Path $zipfile
  2. Challenge

    Update the Visual Studio solution and run the console app.

    Create a New Container in the Azure Portal

    1. In the Azure portal, click All resources.
    2. Select the Azure Cosmos DB account resource in the list.
    3. Click Add Container.
    4. In the Add Container pane, set the following values:
      • Database id: Create new, and name it db1
      • Container id: collection1
      • Partition key: /Partition
      • Throughput: 400
    5. Click OK.

    Update the Visual Studio Code on VM

    1. Back on the virtual machine, in the Visual Studio window, double-click Program.cs in the right-hand menu to open it.
    2. In the Program class, we need to enter a couple pieces of missing information: EndpointUrl and PrimaryKey. In the Azure Portal, head to the Overview section for our Cosmos DB account.
    3. Copy the listed URI.
    4. Back in Visual Studio, paste it in as the EndpointUrl value.
    5. In the Azure portal, scroll down and click Keys (under the Settings heading).
    6. Copy the listed primary key.
    7. Back in Visual Studio, paste it in as the PrimaryKey value.
    8. Click the disk icon to save the code.
    9. Click the play button icon to run the console app.
    10. Back in the Azure portal, scroll down and click Document Explorer (under the Containers heading).
    11. Click Open Data Explorer.
    12. Click collection1.
    13. Click Documents. We should see two documents listed, which means we were successful.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans