In my previous article, I explained the steps to fetch the ‘Environment ID’ without the need of cloud flow.

In this article, lets see how to fetch the Environment URL using PowerFx formula. This works for both Canvas App and Custom Pages.

Steps to fetch the Environment URL:

  • On AppStart paste the following formula
// Fetch the 'Environment ID' of current environment
// Store the in the 'varCurrentEnvironmentID' variable
Set(
    varCurrentAppUri,
    First('Canvas Apps').AppOpenUri
);
Set(
    varCurrentEnvironmentID,
    Right(
        Match(
            varCurrentAppUri,
            "/e/.*(?=/a)"
        ).FullMatch,
        36
    )
);
// Use PowerAppsforMakers.GetEnvironments() and fetch all the environments under the tenant
// Store Environments in to 'collAllEnvironments' collection
ClearCollect(
    collAllEnvironments,
    AddColumns(
        Sort(
            PowerAppsforMakers.GetEnvironments().value,
            DataSourceInfo.DisplayName
        ),
        "DisplayName",
        properties.linkedEnvironmentMetadata.friendlyName,
        "Url",
        properties.linkedEnvironmentMetadata.instanceUrl
    )
);
// Filter 'collAllEnvironments' with 'varCurrentEnvironmentID' and fetch the current environment's URL
Set(
    varCurrentEnvironmentURL,
    First(
        Filter(
            collAllEnvironments,
            varCurrentEnvironmentID in id
        )
    ).Url
);
  • In the above formula, we are capturing the ‘Environment URL’ in to ‘varCurrentEnvironmentURL‘ variable.
  • To display the ‘Environment URL’, Add a label and set the text property to ‘varCurrentEnvironmentURL‘.

🙂

Advertisements
Advertisements

2 responses to “Canvas App/Custom Page | Get the environment’s ‘Environment URL’”

  1. indianatul Avatar

    Hi Rajeev,

    Thanks for the above solution.

    Above code always give default environment not the current selected environment. How to get the current selected environment ?

    And if environment gets changes then the value of current selected url/variable should also get update,

    please suggest?

  2. Oob Avatar
    Oob

    there are two typos: CurrentAppUri should be varCurrentAppUri in the Match function, and AddColumns requires no Quotes for the column names

Leave a comment