Showing posts with label Power Automate. Show all posts
Showing posts with label Power Automate. Show all posts

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" 🚀

Sunday 2 January 2022

Power Automate: Update file metadata and preserve version history.

In this blog, we will be updating file properties/metadata whenever a new file is uploaded into the library.

In this approach, we will use the REST API and wrap it into a Power Automate flow.

Create Flow

Complete flow with all actions


Flow URL - https://flow.microsoft.com/

1. Open the flow, click on the Create option, and select "Automated Cloud Flow".


Enter Flow name.
Choose the flow trigger as "When an item is created".
Click the Create button.



2. Enter the "Site Address" SharePoint site URL and the "List Name" library title where flow needs to be associated.

Add a new action step, "Compose" and rename it "Compose-user".

In input, add an object as shown below. Created By Claims is value; here we will get the claims value of the user who is uploading the file.



3. Add a new action step again, "Compose" and rename it as "Compose-update".

In input, add an object as shown below.

  {

    "FieldName": "Internal column name",

    "FieldValue": "desired value"

  },

  {

    ............

  },

  .....

]

The input value will be an array of objects, and each object defines the configuration of the column to be updated. The FieldName is the internal column name, and the FieldValue is the desired value that you want to set in that column.

In my example, the Editor (Modified by) and Title columns are updated.

In the Editor FieldValue output of Compose-user will be set.

Note: Flow is running under the service account. If the Editor field is updated, then we can also preserve the Modified By column. Otherwise, the service account value will be updated in the Modified By column.



4. Add a new action step: Send an HTTP request to SharePoint.

Here we will compose a REST API request call.

  • Site Address: SharePoint site URL where the library is present
  • Method: POST
  • Uri: _api/web/lists/getbytitle('<List Title>')/items(<ID>)/validateUpdateListItem
  • Body: 

{
   "formValues": outputs('Compose_-_update'),
   "bNewDocumentUpdate":true
}


        validateUpdateListItem: REST API ValidateUpdateListItem() function to update listitem metadata

        formValues: The value must be the output of the second Compose action.

        bNewDocumentUpdate: true (Not to create any version on update) or false (Create a version on the update)

        For details refer: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/jj246412(v=office.15)



        Upload the file to the Documents library and test the flow.


        🚀 "Happy Coding" 🚀

        Tuesday 19 November 2019

        Export PDF in Power Apps and Power Automate (MS Flows): Approach 1

        In this blog, we will be exporting the List Item details into the PDF document with a button click. This approach is based on the use of both PowerApps and PowerAutomate.

        In this scenario, a customized PowerApps form is there that is used to display data coming from the SharePoint list, and a button will be present on click of which users can export content into the PDF.

        Create Flow

        Complete flow with all actions


        1. Open the flow, click on the Create option, search for "PowerApps", and select the option "PowerApps button" which triggers the flow on button click.


        2. Add the "Initialize variable" action.
        Enter the required values: – Name, Type and Value. To initialize the value, click on "See more".

        Now click on the option "Ask in PowerApps"; it means the value will come from PowerApps form when flow is called as a parameter.

        Select and add the initialized values. Similarly, create a new variable. Filename and initialize.

        Now create the FileFullname variable. To initialize the value, select the Expression tab and concatenate the value of the filename variable with the .html file extension.

        Similarly, create a new variable, CurrentUser and initialize.

        3. Add "Create File" (OneDrive for Business) action.
        Click on the Folder icon present in the Folder Path textbox. Browse the library and select the folder under which the HTML file will be saved and later converted to PDF.

        Set the variables initialized above for File Name and File Content.

        4. Add the "Create file using path" (OneDrive for Business) action.
        Click inside the File Path textbox, and a callout will open. Select Path value from the Create file in OneDrive group under Dynamic content. Set PDF as the Target type.
        Note: Create file OneDrive is the previous action, and these are all the outputs of it.

        5. Add the "Send an email (V2)" (Office 365 Outlook) action.
        Provide the required value for this as shown below.


        Modify PowerApps Form

        1. Open the list where Export PDF functionality needs to be implemented. Click on "Customize forms" to open PowerApps.

        2. Add a new screen and add an HTML text control to it.

        3. Add the HTML content with dynamic values as shown. The content will be added in double quotes.


        4. Add a button for "Export to PDF" functionality. On the button, attach the flow.
            a. Select the button.
            b. On the Action tab, select Flows.
            c. All available flows will appear; select the required one.
            d. Choose the "OnSelect" option to RUN the flow and pass the required parameters.
            e. Save and Publish the PowerApps.

        5. Open the display form and click on the "Export PDF" button.

        6. The exported PDF will be sent to the current logged-in user's email..

        Check the final exported PDF here


        🚀 "Happy Coding" 🚀