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 that we can utilize 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 article, we will see an example wherein we will implement a form in power apps, and change its view mode based on the role that has been configured on the data source. For this example, I am using SharePoint 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

DataSourceInfo(datasourcename,Information,Column name)

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 a specific column or the character length for a specific column.

Column name - Specify the 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 the steps for creating a list in SharePoint. I have already created a list and will utilize the same.

Step 1

Open the PowerApps home page and click on create, and select create from the blank option to create a PowerApp from scratch. You can also click on SharePoint data source to create a prebuilt app that uses SharePoint.

Datasourceinfo function in power apps and displaying forms dynamically based on user role

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.

Datasourceinfo function in power apps and displaying forms dynamically based on user role

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 the below formula

DataSourceInfo(Datasourcename,DataSourceInfo.CreatePermission)

This will return true if the user has create permission on the data source, else this will return false.

Datasourceinfo function in power apps and displaying forms dynamically based on user role

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.

If(DataSourceInfo(Datasourcename,DataSourceInfo.CreatePermission),FormMode.Edit,FormMode.View)

The above formula validates the user email against the data source and if the user has to create permission on the data source then the form mode will be set to edit, else the form mode is set to view.

Datasourceinfo function in power apps and displaying forms dynamically based on user role

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 the power app.

A few more elements 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 the power app.

Sample formula : DataSourceInfo(Datasourcename,DataSourceInfo.MaxLength,"Title")

Retrieve the 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' :)


Similar Articles