Embed Power BI Report in A Power BI Embedded Analytics Application For Your Customers - Power BI - Microsoft Learn
Embed Power BI Report in A Power BI Embedded Analytics Application For Your Customers - Power BI - Microsoft Learn
In this tutorial, you learn how to embed a Power BI report in a .NET 5.0
application, as part of the embed-for-your-customers (also known as an app-
owns-data) solution. In an embed-for-your-customers solution, your app users
don't need to sign in to Power BI or have a Power BI license.
" A Power BI report.
" In an embed-for-your-customers app.
" By using a service principal.
" By using .NET 5.0.
" With the Microsoft.Identity.Web library (this library is also supported in
.NET Core).
7 Note
The full solution used in this tutorial is available from the DOTNET5-
AppOwnsData-Tutorial GitHub repository.
Prerequisites
A Power BI Pro or Premium Per User (PPU) license
Visual Studio .
Resources
In this tutorial, you use:
Power BI REST Reports API, to embed the URL and retrieve the embed
token.
Method
To embed Power BI content in an embed-for-your-customers solution, follow
these steps:
Follow the service principal instructions to create a Microsoft Entra app and
enable the app's service principal to work with your Power BI content.
Domain
Tenant ID
Client ID
Client secret
Workspace ID
Report ID
If you don't know your domain or tenant ID, see Find the Microsoft Entra
tenant ID and primary domain name.
7 Note
To embed content for a user on a different tenant (guest user), you need
to adjust the authorityUri parameter.
Client ID
To get the client ID GUID (also know as application ID), follow these steps:
_. Select the Microsoft Entra app you're using for embedding your Power BI
content.
Client secret
^. Search for App registrations and select the App registrations link.
_. Select the Microsoft Entra app you're using for embedding your Power BI
content.
c. In the Add a client secret pop-up window, provide a description for your
application secret, select when the application secret expires, and select
Add.
g. From the Client secrets section, copy the string in the Value column of
the newly created application secret. The client secret value is your client
ID.
7 Note
Make sure you copy the client secret value when it first appears. After
navigating away from this page, the client secret will be hidden and you'll
not be able to retrieve its value.
Workspace ID
:
To get the workspace ID GUID, follow these steps:
_. Copy the GUID from the URL. The GUID is the number between /groups/
and /reports/.
7 Note
Report ID
To get the report ID GUID, follow these steps:
_. Copy the GUID from the URL. The GUID is the number between /reports/
and /ReportSection.
7 Note
To get the report ID programmatically, use the Get Reports In Group API.
PowerShell
ノ Expand table
File Use
7 Note
:
The code in ConfigureServices accomplishes several important things:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using AppOwnsData.Services;
namespace AppOwnsData
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
services.AddMicrosoftIdentityWebAppAuthentication(Config‐
uration)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();
services.AddScoped(typeof(PowerBiServiceApi));
services.AddControllersWithViews(options => {
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(poli‐
cy));
});
services.AddRazorPages()
.AddMicrosoftIdentityUI();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
:
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=In‐
dex}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
JSON
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "yourtenant.onMicrosoft.com",
"TenantId": "",
"ClientId": "",
"ClientSecret": "",
"CallbackPath": "/signin-oidc",
"SignedOutCallbackPath": "/signout-callback-oidc"
},
"PowerBi": {
"ServiceRootUrl": "https://api.powerbi.com/"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
:
}
_. Fill in the embedding parameter values obtained from Step 2 - Get the
embedding parameter values.
7 Note
Get the Microsoft Entra access token and call the Power BI
service
In order to embed Power BI content like reports and dashboards, your app
needs to get an Microsoft Entra token. To get the token, you need a
configuration object.
The code in this section uses the .NET Core dependency injection pattern.
When your class needs to use a service, you can add a constructor parameter
for that service. The .NET Core runtime takes care of passing the service
instance at run time. In this case, the constructor injects an instance of the .NET
Core configuration service by using the IConfiguration parameter, which is
used to retrieve the PowerBi:ServiceRootUrl configuration value from
appsettings.json. The ITokenAcquisition parameter, which is named
tokenAcquisition , holds a reference to the Microsoft authentication service
:
provided by the Microsoft.Identity.Web library. The ITokenAcquisition
parameter is used to acquire access tokens from Microsoft Entra ID.
The RequiredScopes field holds a string array that contains a set of delegated
permissions supported by the Power BI service API. When your application calls
across the network to acquire a Microsoft Entra token, it passes this set of
delegated permissions so that Microsoft Entra ID can include them in the
access token it returns.
7 Note
Verify that your Microsoft Entra app is configured with the scopes required
by your web app. For more information, see Change your Microsoft Entra
app's permissions.
C#
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Identity.Web;
using Microsoft.Rest;
using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;
using Newtonsoft.Json;
namespace AppOwnsData.Services {
}
}
From the Controllers folder, open the HomeController.cs file and add the
following code to it:
C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using AppOwnsData.Models;
using AppOwnsData.Services;
namespace AppOwnsData.Controllers
{
[Authorize]
public class HomeController : Controller
{
:
private PowerBiServiceApi powerBiServiceApi;
[AllowAnonymous]
public IActionResult Index()
{
return View();
}
return View(viewModel);
}
[AllowAnonymous]
[ResponseCache(Duration = 0, Location = ResponseCach‐
eLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Ac‐
tivity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
File Use
Embed.cshtml Contains your app's document object model (DOM) and a DIV for
embedding the report
In this tutorial, you create the Embed.cshtml file, which has a div element
that's a container for your embedded report, and three scripts.
HTML
@model AppOwnsData.Services.EmbeddedReportViewModel;
@section Scripts {
In this tutorial, you create a JavaScript file named embed.js with a configuration
object for embedding your report that uses the variable models .
JavaScript
$(function () {
var config = {
type: 'report',
id: reportId,
embedUrl: embedUrl,
accessToken: token,
permissions: models.Permissions.All,
tokenType: models.TokenType.Embed,
viewMode: models.ViewMode.View,
settings: {
panes: {
filters: { expanded: false, visible: true },
pageNavigation: { visible: false }
}
}
};
});
) Important
:
If you used free embed trial tokens for development, you must buy a
capacity for production. Until a capacity is purchased, the Free trial version
banner continues to appear at the top of the embedded report.
When your app is ready, you can move your embedded app to production.
Related content
Embedded analytics application tokens
Move your embedded app to production
Capacity and SKUs in Power BI embedded analytics
Capacity planning in Power BI embedded analytics
Feedback
Was this page helpful? Yes No