In this article, we continue with the theme of uncovering lesser-know functions that are available in canvas apps. This time, it’s the turn of the Power Apps DataSourceInfo function.
We can use this function to display column/field properties and whether users have access to perform certain actions against a data source.
Table of Contents
ToggleIntroduction / Prep
For reference, the Power Apps DataSourceInfo function is well documented by Microsoft. There’s some good examples here but hoping this article gives a bit more insight.
I’ll start with creating a data source to support the examples in this article. I’ll use SharePoint as I’d like to underline a point made in the documentation. I’ve created a list called Star Wars Movies. Alongside the usual Title column, I’ve added 2 more. I’ve set some additional configuration so we can see how useful the DataSourceInfo function is:
As users of an app, we can’t always see such column configuration. As makers, we need to ensure we provide useful information to users so they know how to interact with our app. The DataSourceInfo function will help us with that.
I’ve also created a canvas app and added the following controls to the screen to manually build a data capture form:
As far as the DataSourceInfo function goes, it’s constructed like so:
// DataSourceInfo function structure
DataSourceInfo(
Data source,
Information,
Column)
Data source is mandatory and will be a SharePoint list, Dataverse table, SQL table etc. The information property is a list of pre-defined options we want to check, this property is also mandatory.Â
Finally, we can include a column within the data source to target information to a single element. This needs to be the internal name of the column, not the display name. It’s a good practice to use internal names anyway, as once set up they never change.
Internal column names
As a quick 101, in Dataverse the internal name of a column is the Name column. You can change the display name as much as you like but the internal name stays static from its original creation:
It’s not so obvious for SharePoint lists. If you want an internal column name you first need to go to your list, then select List Settings from the cog:
Next, select the column you want to find the internal name for. In this example, let’s select the Box Office Takings column:
At the far-right end of the URL, you’ll see &Field= followed by the internal field name. So Box Office Takings is our display name, but any spaces and/or special characters get converted into something more URL friendly:
I may even move this little tip into a separate post as it’s caught me out a couple of times in the past. Anyway, back to the DataSourceInfo function.
Column information
The Power Apps DataSourceInfo function allows us to retrieve information about a column.
1- Max length (text fields)
2- Min and max values (number fields)
3- Whether it’s required
4- Display name
Let’s take a look at each one and use them against the form & list we’ve built.
Max length
In our SharePoint list, our Director column has been set to capture a maximum of 10 characters. We can show the config for this, or any text field using DataSourceInfo:
DataSourceInfo(
'Star Wars Movies',
DataSourceInfo.MaxLength,
"Director"
)
Here is the output for each text-based column, 255 being the default for any single line of text columns:
This is useful for adding boundaries to text input controls in our app. Currently we could enter any number of characters, but we can use the same function to set a limit.Â
Selecting the text input control, we can see there’s a MaxLength property to work with:
We can hard code a value here, but what if the underlying column configuration changes? Make sure it’s dynamic and add the same code into the MaxLength property:
Please note: any changes to column config aren’t picked up by Power Apps real-time. You’ll need to refresh the data source when in edit mode of your app, or ask users to start a new app session to see changes.
Min and max value
In our SharePoint list, our Box Office Takings column has been set to allow a number between the range of 0 and 500. We can use the Power Apps DataSourceInfo function to show this information to our users; the MinValue or MaxValue information property is what we’ll need. Note here we are using the internal name of the column in the Power Fx, not the display name.
Minimum value:
DataSourceInfo(
'Star Wars Movies',
DataSourceInfo.MinValue,
"BoxOfficeTakings_x0028__x00a3_m_"
)
Maximum value:
DataSourceInfo(
'Star Wars Movies',
DataSourceInfo.MaxValue,
"BoxOfficeTakings_x0028__x00a3_m_"
)
Outputs for our list as follows:
We are able to format text input controls to number in Power Apps. However, to the best of my knowledge there isn’t a property to define min or max values; we’d have to do that bit of validation manually. We can use the same formulas for that to ensure we are comparing directly against the data source configuration.
In the example below, I capture the required values as part of a With function, then perform the calculation:
With(
{
// Get min value setting for Box Office number column
tvMinValue: DataSourceInfo(
'Star Wars Movies',
DataSourceInfo.MinValue,
"BoxOfficeTakings_x0028__x00a3_m_"
),
// Get max value setting for Box Office number column
tvMaxValue: DataSourceInfo(
'Star Wars Movies',
DataSourceInfo.MaxValue,
"BoxOfficeTakings_x0028__x00a3_m_"
),
// Ensure number entered by user is stored as a value
tvEnteredValue: Value(txtTakings.Text)
},
// Perform logic
If(
tvMinValue > tvEnteredValue || tvEnteredValue > tvMaxValue,
"Computer says no",
"Within range"
)
)
In theory, we can then use the outputs to drive other properties. This could be for disabling a submit button or changing the border colour of the offending control.
Required
In our SharePoint list, the Title column is the only one that’s mandatory. The Power Apps DataSourceInfo function can also capture this for us, using the Required information property:
DataSourceInfo(
'Star Wars Movies',
DataSourceInfo.Required,
"Title"
)
The values returned are boolean in nature:
Display name
If you’re making custom data collection experiences in Power Apps, chances are you’re adding labels to show the columns purpose. In my example, I’ve added labels for Title, Director and Box Office Takings, but these values are hard coded. What if the display name of the column changes in my data source? Would I have to open my app and manually edit any relevant labels?
Using DataSourceInfo, we can retrieve the display name of columns in the SharePoint list. Here, we are using DisplayName information parameter and referencing the internal name of the column:
DataSourceInfo(
'Star Wars Movies',
DataSourceInfo.DisplayName,
"BoxOfficeTakings_x0028__x00a3_m_"
)
This is great, meaning if we choose to, we can have parity of display names between our data source and canvas app.
However, please note this doesn’t seem to work for the default Title field in a SharePoint list. I’ve changed the display name of this column multiple times, but the formula still returns Title. Changes to the display name of custom columns are correct though. As per the image below, on the left we have hard-coded, on the right the values from the DataSourceInfo function:
Data source permissions
Another awesome use for the Power Apps DataSourceInfo function is checking if the current user has access to the underlying data source(s). At time of writing, Dataverse is the only data source we can use DataSourceInfo to check permissions for, as per Microsoft’s documentation:
It can be an annoying experience for users to spend time filling out a form, only to get an error when trying to submit the data. Therefore, we can check for four levels of permission for the current user. These four levels correspond with the acronym CRUD – Create, Read, Update, Delete; four things a typical user will need to do with data.
Each of the four uses for DataSourceInfo will return a boolean true/false value.
Create
To check if a user has the ability to create records in the relevant Dataverse table, we can use the following DataSourceInfo formula:
DataSourceInfo(
MyDataverseTable,
DataSourceInfo.CreatePermission
)
Read
To check if a user has the ability to read records in the relevant Dataverse table, we can use the following:
DataSourceInfo(
MyDataverseTable,
DataSourceInfo.ReadPermission
)
Update
To check a users ability to update existing records, we can use the following:
DataSourceInfo(
MyDataverseTable,
DataSourceInfo.EditPermission
)
Delete
Finally, to see if a user can delete existing records, we can use the following:
DataSourceInfo(
MyDataverseTable,
DataSourceInfo.DeletePermission
)
Analyse up front
Recently I talked about Power Apps named formulas and how useful they are. Storing users CRUD permissions to a data source would be another great use case.
As a named formula, we can construct the following table that we can then reference in our app:
nfUserCRUDOps = Table(
{
Ability: "Create",
Access: DataSourceInfo(
MyDataverseTable,
DataSourceInfo.CreatePermission
)
},
{
Ability: "Read",
Access: DataSourceInfo(
MyDataverseTable,
DataSourceInfo.ReadPermission
)
},
{
Ability: "Update",
Access: DataSourceInfo(
MyDataverseTable,
DataSourceInfo.EditPermission
)
},
{
Ability: "Delete",
Access: DataSourceInfo(
MyDataverseTable,
DataSourceInfo.DeletePermission
)
}
);
We can then work with these values in galleries or other controls. Having this information on app launch would be great to drive a nicer user experience.
For example, if the user doesn’t have permissions to create an item (formula returns ‘false’), navigate to a screen to let them know to speak to the relevant administrator. You could even build in some automated logic to send an email or log a ticket.
Did you know?
The above is very useful if you’re adding input controls to your app to create a data capture experience. However, did you know the DataSourceInfo functionality is automatically baked into form controls?
Form controls don’t get the highest of praise at times and they have their limitations. However, for creating individual records they have a lot going for them. Whilst the Patch function is a go-to for many, there’s the overhead of Power Fx configuration. Form controls will take care of data source related validation and errors for you, with Patch you need to configure them manually.
By way of a couple of quick examples, I’ve added a form control to my app and set the data source as the same SharePoint list. The MaxLength property for the Director(s) column is already configured for me and it uses the DataSourceInfo function:
The same for column display name:
Thanks for reading! If you liked this article and want to receive more helpful tips about Power Platform every week, don’t forget to subscribe or follow me on socials 😊
Great article – always appreciate Star Wars references too :p
Very insightful, Craig. I have seen it popping up in Apps but never paid much attention to it, or instance, with the Form Control as you mentioned but wasn’t clear of their use… until now. Thanks for sharing. Appreciated
Loved this article! Thanks Craig for your time spent on helping others!
Interesting… definitely uncovering functionality that I was not aware of, but can see how I could have applied it to some apps I have built in the past.