ToDo Bot | Part 9 | Coding the View Task Dialog | READ Data from Azure Cosmos DB | Microsoft Bot Framework

This is the bot series on ToDo Bot where we will be creating a Chatbot to create, view, and delete tasks. In this part, we will be coding the View Task Dialog to view all the tasks in descending order.

The Azure Cosmos DB Emulator provides a local environment that emulates the Azure Cosmos DB service for development purposes. Using the Azure Cosmos DB Emulator, you can develop and test your application locally, without creating an Azure subscription or incurring any costs.

For more information, refer Microsoft Documentation.


  1. Azure Cosmos DB Local Emulator
  2. Visual Studio
  3. Bot Emulator

Second and third requirement link is available on the Downloads page.


Create Database and Container

We have already created the database and container in our last part. Refer to my post on Working with Azure Cosmos DB Local Emulator.

Authenticate the User

We have covered this as well by generating a new user id or authenticating the existing one. Refer to my post on Connecting Bot with Azure Cosmos DB.

Coding View Task Dialog

Before starting with View Task Dialog. Let us create a method in CosmosDBClient.cs to query the result based on the user id.

public async Task<List<ToDoTask>> QueryItemsAsync(string userId, string EndpointUri, string PrimaryKey, string databaseId, string containerId, string partitionKey)
            await GetStartedAsync(EndpointUri, PrimaryKey, databaseId, containerId, partitionKey);

            var sqlQueryText = $"SELECT * FROM c WHERE = '{userId}' ORDER BY c._ts DESC";

            Console.WriteLine("Running query: {0}\n", sqlQueryText);

            QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);
            FeedIterator<ToDoTask> queryResultSetIterator = this.container.GetItemQueryIterator<ToDoTask>(queryDefinition);

            List<ToDoTask> todoTasks = new List<ToDoTask>();

            while (queryResultSetIterator.HasMoreResults)
                FeedResponse<ToDoTask> currentResultSet = await queryResultSetIterator.ReadNextAsync();
                foreach (ToDoTask todoTask in currentResultSet)
                    Console.WriteLine("\tRead {0}\n", todoTask);
            return todoTasks;

This method will create a connection to the database and container. Next, it will query the database based on the user id and will return the result in descending order. We return a list of type ToDoTask.

We want to pass the cosmos DB client object the IConfiguration object to the ViewTaskDialog so that we can call the cosmos DB client methods and get the creds from the appsettings.json respectively.

Modify the Initialization of ViewTaskDialog in MainDialog constructor.

AddDialog(new ViewTaskDialog(Configuration, _cosmosDBClient));

Create two properties at the class level of the ViewTaskDialog and initialize them in the constructor.

protected readonly IConfiguration Configuration;
private readonly CosmosDBClient _cosmosDBClient;

        public ViewTaskDialog(IConfiguration configuration, CosmosDBClient cosmosDBClient) : base(nameof(ViewTaskDialog))
            Configuration = configuration;
            _cosmosDBClient = cosmosDBClient;

            var waterfallSteps = new WaterfallStep[]

            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
            AddDialog(new TextPrompt(nameof(TextPrompt)));

            InitialDialogId = nameof(WaterfallDialog);

We have created a single-step ShowTasksStepAsync. Create a method for it and add the below code. This will call the QueryItems from Cosmos DB Client and save the tasks in a list. Thereafter, it will show all the tasks to the user.

private async Task<DialogTurnResult> ShowTasksStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
            List<ToDoTask> toDoTasks = await _cosmosDBClient.QueryItemsAsync(User.UserID, Configuration["CosmosEndPointURI"], Configuration["CosmosPrimaryKey"], Configuration["CosmosDatabaseId"], Configuration["CosmosContainerId"], Configuration["CosmosPartitionKey"]);

            if (toDoTasks.Count == 0)
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("You don't have any tasks added."), cancellationToken);
                return await stepContext.EndDialogAsync(null, cancellationToken);

            await stepContext.Context.SendActivityAsync(MessageFactory.Text("Please find the below task you provided."), cancellationToken);

            for (int i = 0; i < toDoTasks.Count; i++)
                await stepContext.Context.SendActivityAsync(MessageFactory.Text(toDoTasks[i].Task), cancellationToken);

            return await stepContext.EndDialogAsync(null, cancellationToken);

Run the project and test the output in the Bot Emulator.

In the next part, we will delete the tasks selected by the user.

Thank you All!!! Hope you find this useful.

One thought on “ToDo Bot | Part 9 | Coding the View Task Dialog | READ Data from Azure Cosmos DB | Microsoft Bot Framework

Add yours

Leave a Reply

Up ↑

%d bloggers like this: