Power App -Consume data from COSMOS Database - A globally-distributed, multi-model database service

This post will help you to query data from Cosmos Database- A globally-distributed, multi-model database service and display in Power APP.
We need to create Power Automate AKA Flow with Cosmos DB connector which is one of the premium connectors available with Power App premium licence.
check below link for the pricing of Power APP with premium connectors:



So let's open Power APP Home https://make.powerapps.com/ so that we can start creating Flow.
Create a new flow and select cosmos-db connector.







There are various actions which we can perform on CosmosDB like get document,update documents,delete document,call stored procedure etc..(See below screen shot) 


Let's perform Get a document action - Once you select it you need to create connection first to establish connection with your Cosmos DB.
Few details required as below:




Your cosmos DB account id and Access Key to your Azure Cosmos DB account.These details you can get from your azure Portal by accessing Cosmos db Key details if you have access else please contact your Azure administrator to get these details.

Enter all the required details and establish a connection.This is one time activity.Once you set it up  connection..this connection would be available while creating any other new flows using same cosmos DB account.You can see all the connection by clicking on top right three dots.




now enter DatabaseID,collection ID and Document ID (would be the unique identifier for your cosmos DB documents)
You need to enter partition key as well to query Cosmos Collection and get the document.

Make sure to enter partition key in double quotes like "abc" else flow will throw error. 

Continuation Token is not required in this case because we are just fetching a single document.If you use Stored procedure or fetch multiple documents at that time these extra fields would be more relevant in terms to make optimised hits on Cosmos DB.

Now add another action parse JSON because Get a document action will return you the complete Json which you have to parse to return to your Power APP.Just copy your document schema using Generate from sample option.




 Now add response as below to return response to powerAPP.Mention Content-type as application/json and select body to render.



So all done from Flow perspective.Now It's time to test your flow using Flow checker.

Now next step is to call this Flow in PowerAPP and render some of the attributes from your cosmos DB document.

Go to Power App editor and create a Canvas based App..Add a button and Gallery control like below:




Now it's time to call Flow through Power App ob button click which we have added on canvas so to do that click on button and then action and Power Automate like below.You will see the list of Flows at the right side.Choose the one which we have created recently to fetch document from Cosmos DB.




 Now set a variable like below in formula bar so that we can make it source for our Gallery Control.




Now it's time to set Variable CosmosDoc as a source for gallery control.select gallery and put this variable in formula bar.
Now preview the app and click on button you will see the few of the response attributes is getting displayed in the gallery.

You can see all the attributes by typing thistem.(same as below) in formula bar.So now you have all the attributes available within Powerapp..Go ahead and make a beautiful display with the controls in powerapp.




Please like,comment and share if it's useful.

Comments

Popular posts from this blog

Office 365 Suite of products

Calling an Azure AD secured Rest API from Power APP using Power Automate