Imagine you’ve created a stunning Power BI report, packed with insights and visualizations that tell a compelling story. But what if you need to share that report with someone who doesn’t have access to Power BI, or who can’t access it across tenant boundaries? Fortunately, Power BI offers a powerful solution: exporting your report to the desired file format.

In this blog post, we’ll explore how to leverage and implement the Export to File functionality via Microsoft.PowerBI.API NuGet package to effortlessly export a single-page Power BI report as a PNG file.

Pre-Requisites

For the below code to work, certain pre-requisites need to be followed:

  • Global Admin Privileges.
  • An Entra ID App. Register an app. Grant application permissions with “Tenant.Read.All” and secure with admin consent. Also, create a new client secret.
  • Workspace Access. Go to Power BI workspace, select manage access, and add a newly created APP ID.
  • Enabled Image Export. In app.powerbi.com, navigate to the admin portal > tenant settings, and activate “Export reports as image files.”
  • Premium Workspace. The report that needs to be exported as an image must reside in a workspace backed by a Premium, Embedded, or Fabric capacity. Check out the Microsoft documentation for the latest updates.
  • Update License to Workspace. Go to workspace settings > select the license mode (Premium/Embedded/Fabric). If you have multiple reports from different workspaces, all the workspaces license modes need to be updated.
  • Publishing License. To publish the report to the app service, the Power BI user should have either Power BI free/pro/PPU license.

Let’s Dive into Code

Here’s the GitHub repository housing my code. Within, you’ll find an ASP.NET core application where my controller method utilizes the Power BI API to transform a report into an image.

AppSettings.json

The below settings should be appended to appsettings.json for authenticating and interacting with Power BI through Microsoft.PowerBI.API NuGet package.

{  
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": ".onMicrosoft,com",
    "TenantId": "",
    "ClientId": "",
    "ClientSecret": "<CLIENT_SECRET",
    "CallbackPath": "/signin-oidc",
    "SignedOutCallbackPath": "/signout-callback-oidc"
  },
  "PowerBi": {
    "ServiceRootUrl": "https://api.powerbi.com/"
  }
}
Inside Controller

When a client sends a POST request containing WorkspaceId and ReportId to your application, it generates a token using the above settings and initiates a call to the exportToFile method. Remember, this API operates asynchronously. While exportToFile is called, it triggers an export job and returns a 202 status code. To monitor the job’s progress, we need to poll at regular intervals using the exportToFileStataus API. Here’s a code snippet illustrating the process:

{  
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": ".onMicrosoft,com",
    "TenantId": "",
    "ClientId": "",
    "ClientSecret": "<CLIENT_SECRET",
    "CallbackPath": "/signin-oidc",
    "SignedOutCallbackPath": "/signout-callback-oidc"
  },
  "PowerBi": {
    "ServiceRootUrl": "https://api.powerbi.com/"
  }
}

Limitations

While the Export to File functionality empowers you to share your Power BI masterpieces, here are certain key considerations:

  • Handling Multi-Page Reports: When exporting reports with multiple pages, the API automatically packages them into a ZIP file, with each file representing an individual page.
  • Maximum File Size: To ensure optimal performance and avoid potential export issues, Power BI enforces a maximum file size of 250 MB for exported reports.

Please refer to the Microsoft documentation for more details.

Closing

With a few lines of code, you can transform your Power BI reports into easily shareable images, reaching a wider audience.