Sample: Blazor WebAssembly with Global Discovery
This sample shows how to use the Global Discovery service from a Blazor WebAssembly application.
You can download the sample from https://github.com/microsoft/PowerApps-Samples/tree/master/dataverse/webapi/C#/BlazorCDS
This sample is a modification of the steps in the Tutorial: Create an ASP.NET Core Blazor WebAssembly app using Microsoft Dataverse
You can try this sample online here: https://blazorcds.mohsinonxrm.com/
How to run this sample
- Download or clone the Samples repo so that you have a local copy.
- Follow the instructions in Setup to create an Microsoft Entra ID app registration and run the sample.
What this sample does
This sample demonstrates how to provide a select (drop-down) control in a web application to allow users to switch environments that they have access to.
How this sample works
The Pages/EnvironmentChooser.razor
file provides the user interface to allow users to select from available environments based on their credentials after they have logged in.
This page depends on a named definition of an HttpClient (GDSClient
) in Program.cs that is configured to access the Global Discovery Service. Access to the Global Discovery Service is added to the available scopes when the user logs in. When the user selects a different environment, the SelectedEnvUrl
in Models/AppState.cs
changes.
Access to the account records is provided by Pages/FetchAccounts.razor
with an access token that depends on the selected environment.
Setup
To run this sample you must first configure an Microsoft Entra ID application on your tenant and update the BlazorCDS\wwwroot\appsettings.json
file to replace the placeholder ClientId
value with the application (client) Id. Use the following steps:
Create an app registration
Go to Azure.
Select Microsoft Entra ID.
Go To App registrations.
Select New Registration.
Enter the following:
Field Value Name Your choice. You might use: BlazorGlobalDiscoverySample
Supported account types: Accounts in this organization directory only (Single Tenant) Redirect URI (optional) https://localhost:44363/authentication/login-callback
Click Register.
Copy the Application (client) ID value. You will need this when you Update the project.
Configure Authentication
Go to Authentication.
Under Implicit grant and hybrid flows, select both of the following options:
- Access tokens (used for implicit flows)
- ID tokens (used for implicit and hybrid flows)
Click Save.
Configure Permissions
- Go to API permissions.
- Click Add a permission.
- In the Request API permissions fly-out, under theMicrosoft APIs tab, select Dynamics CRM.
- Under Select permissions, select user_impersonation.
- Click Add permissions.
- Under Configured permissions, click Grant admin consent for {Your tenant name.}
- In the Grant admin consent confirmation dialog, click Yes.
Update the project
- Open the BlazorCDS Visual Studio solution file (BlazorCDS.sln) with Visual Studio.
- In Solution Explorer, expand
wwwroot
and open theappsettings.json
file. - Replace the placeholder
ClientId
value (11111111-1111-1111-1111-111111111111
) with the application (client) ID value you copied in Create an app registration.
Run the sample
In Visual Studio, click IIS Express to run the sample.
Demonstrate
- In the browser window that opens with the app, click Log in.
- Enter your credentials and you will see a select control with the label: Choose an Environment. The select control should list all the environments you have access to with the credentials you provided.
- Click Fetch Accounts. This should display a list of account records you have access to view in the selected environment.
- If you have more than one environment to choose from, select a different environment and the list of account records will be refreshed to represent the records available in that environment.
Clean up
This sample makes no changes to data in your environment.
See Also
Discover user organizations
Sample: Global Discovery Service (C#)