Monday 31 July 2023

PowerApps: Get the AppID dynamically in the current environment.

I came across one of the tasks in PowerApps to get the Canvas App ID dynamically.

AppID is a unique GUID assigned to a PowerApp. It changes from environment to environment and tenant to tenant.

In this blog, we will be learning how to set the default value in the Canvas app.

Please follow the below steps:

1. Sign in to Power Apps and choose the environment.

2. Select "Solutions" on the menu to the left.

3. Select the solution for which you'll create your app.

4. Select New > App > Canvas app.


5. Enter an App name and choose the layout for the app.

6. Click "Create" to create a blank canvas app.


7. Once created, the app opens in Power Apps Studio for you to start configuring the app's functionality.

8. In the left menu, select the "Data" option to open the "Data" pane.

9. Select Add data source. Search "Power Apps" and select "Power Apps for Makers".


10. Click "Add a connection" or use an existing connection.


11. Now a data source will be added to the app.


12. Add a new screen, "AppDetails_Screen" and add three Gallery controls with "Title, subtitle, and body" layout.

The screen will look like the one below.


13. In the Get All Apps Gallery control, set the Items property as below. It will return a table/list of records with all the Power App details in descending order based upon the created date and time.

PowerAppsforMakers.GetApps().value

The Get Apps method returns a list of apps. The return type will be an array ResourceArray[PowerApp]

Note: Returns all the apps to which the connection account has access across environments in a tenant.


Select the columns as below to get the "App Display Name", "Environment ID", "App ID".



Note: The GetApp method is used to get app details. It expects PowerApp Name [app: Name field of the PowerApp] as the first required parameter. This is not a friendly name/display name, but a GUID.


14. In the Get App by DisplayName Gallery control, set the Items property as below. It will filter the data based on the displayName property. It will return a table/list of record(s). Sorting is optional, as the details by default are in descending order based on the created date and time.

Sort(

    Filter(

        PowerAppsforMakers.GetApps().value,

        properties.displayName = "Hello Power App"

    ),

    properties.createdTime,

    SortOrder.Descending

)


It will return all the apps with the display name provided across all environments. In my case, it returned two records.

But the task is to get the Power App ID dynamically in the current environment. There is an environment name property on which we can add additional filters. In PowerApps, there is no direct way to get the current environment details. Fortunately, this can be achieved using a Power Automate flow.

15. Create a Power Automate flow by using steps.

a). Choose the Power Automate option in the left menu. Click the "Create new flow" button.


b). Click on the "Create from blank" button. A flow will be created with the default trigger.


c). Delete the flow trigger.


d). Add a new flow trigger, "PowerApps (V2)", a new version.


e). Add the "Compose" action.


f). Add expression to the compose action.

toLower(workflow()?['tags']['environmentName'])


g). Add the "Respond to a PowerApp or flow" action.


h). In this action, define the output fields you want to provide to PowerApps. Choose the type of output as Text.


i). Name the field, and select Add Dynamic Content to pass in data from the above compose action in the flow.


j). Click on the "Save" button to save the flow.


k). Now flow is added to the PowerApp.



16. App OnStart property calls the flow and sets the output of flow in a variable. It will be used later on.

Set(

    varEnvID,

    GetCurrentEnvironment.Run().result

)


17. In the Get App by DisplayName and EnvironmentName Gallery control, set the Items property as below. It will filter the data based on the displayName and environment name properties. It will only return a single record in a table/list.

First(

    Filter(

        (PowerAppsforMakers.GetApps()).value,

        (properties.displayName = "Hello Power App" And properties.environment.name = varEnvID)

    )

)


18. Now we will be discussing how to get the AppID and display it on a label. Add a new screen called "AppID_Screen" and add four labels like below.

In step 17, we can see the AppID (a GUID) value is present in the name property.

Now we will learn how to get it using the Lookup and Filter functions. Both will return the same data; only the syntax is different.

LookUp - Looks up a single record in a table based on one or more criteria.

Filter - Returns a filtered table based on one or more criteria.

Set the below value in the label "lbl_Lookup_Val".

LookUp(

    PowerAppsforMakers.GetApps().value,

    (properties.displayName = "Hello Power App" And properties.environment.name = varEnvID),

    name

)


Set the below value in the label "lbl_Filter_Val".

First(

    Filter(

        (PowerAppsforMakers.GetApps().value),

        (properties.displayName = "Hello Power App" And properties.environment.name = varEnvID)

    )

).name


19. As you can see in the below screen, the output of both functions is the same.



I Hope this will help all of you! 🧲
Please feel free to provide feedback.


🚀 "Happy Coding" 🚀

Friday 14 July 2023

PowerApps: Set the default user value in the Canvas app.

I came across one of the tasks in PowerApps to set the default value of the Person Type column.

In this blog, we will be learning how to set the default value in the Canvas app.

Please follow the below steps:

1. Create a new SharePoint list.

2. Create a Person/User type column called "Requestor Name".


3. Create a blank Canvas App with Format Tablet.

4. Add an Edit form control on screen and add the above created list as the data source for this form control. Set form DefaultMode to New.

5. For "Requestor Name DataCard" set the below value for the Default property.

Switch(
    frm_FileTransferService.Mode,
    FormMode.New,
    {
        '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
        DisplayName:User().FullName,
        Claims:"i:0#.f|membership|" & Lower(User().Email),
        Email:User().Email,
        Picture:User().Image
    },
    FormMode.View,
    ThisItem.'Requestor Name'
)


6. No need to do any changes in the Update property value. Its shown for reference only.


7. Under "Requestor Name DataCard" for DataCardValue.

    a. Comment on the Items property value, as it's not required. The items property is for populating the data in the people picker.


    b. Set the below value for the DisplayMode property. This DisplayMode property is used to configure whether the control inside the card allows user input (Edit), only displays data (View), or is disabled (Disabled).

If(
    frm_FileTransferService.Mode = FormMode.New,
    Parent.DisplayMode.Disabled,
    Parent.DisplayMode
)


9. Save and Publish the app.

10. Play/Run the app. In the New Form, the logged-in user is shown as default, and editing is disabled.



🚀 "Happy Coding" 🚀