Datasourceinfo function in power apps and displaying forms dynamically based on user role
When designing power apps many of us utilize forms to enable users to add data dynamically to the data source and also view the information at the same. A form helps the user to see data that has been submitted dynamically so that he can validate the same.
There is a function in power apps namely datasourceinfo, we can utilize this function to read information from the data source dynamically and change the information as per the user role that has been configured on the data source.
In this blog post, we will see an example wherein we will implement a form in power apps, and change its view mode based on the role of the that has been configured on the data source. For this example, I am using share point as a data source.
When we use datasourceinfo function it takes the logged-in user email address and validates it against the data source to retrieve the information.
Prototype of datasourceinfo function
Datasource name - The name of the data source that you are connected to.
Information - Information that you want to retrieve from the data source, whether you want to retrieve the permission details or the required values for specific column or the character length for a specific column
Column name - Specify column name, if the formula has to be applied only on a specific column and not on the complete data source.
In this post, I am not sharing steps on creating a list in SharePoint. I have already created a list and will utilize the same.
Step 1: Open PowerApps home page and click on create, select create from blank option to create a PowerApp from scratch. You can also click on SharePoint data source to create a prebuilt app that uses SharePoint.
Step 2: Add a data source to the app by clicking on the cylindrical icon present in the left-hand side menu, search for SharePoint data source and click on that to add it to the app.
Step 3: After adding the data source insert a label on the PowerApp screen, we will use this to check the information that is being retrieved when we use the datasourceinfo function.
Step 4: Set the text property of the label to below formula
This will return true if the user has create permission on the data source, else this will return false.
We will use this information to dynamically change the form mode based on the permission that the user has on the data source.
Step 5: Insert an edit form on the PowerApp screen and connect it to the SharePoint data source.
Select the form and switch to the Default Mode property of the form, this property helps us to set the form mode. We will implement the formula on the Default Mode property.
The above formula validates the user email against the data source and if the user has create permission on the data source then the form mode will be set to edit, else the form mode is set to view.
Once this has been set up, whenever a user opens the PowerApp the form mode will change based on the permission that he has on the data source. This will help app-makers to implement more dynamic functionality on to the power app.
Few more elements that can be implemented using the datasourceinfo function.
Retrieve the maximum number of characters that can be inserted in a column.
We have many properties that we can make use of when implementing the datasourceinfo function, we can have datasourceinfo.Maxlength to check the maximum number of characters that are allowed for a specific column, utilize this to implement dynamic character counting in power app.
Sample formula : DataSourceInfo(Datasourcename,DataSourceInfo.MaxLength,"Title")
Retrieve display name of the data source
We can also retrieve the display name of the connected data source using the datasourceinfo function, this will be helpful when we want to insert the data source name in any formula when we have multiple data sources connected to the app.
Sample formula : DataSourceInfo(Datasourcename,DataSourceInfo.DisplayName)
Please leave feedback, if you find the information shared helpful. Happy Powerapp'ing' :)